require 'fastlane/action'
require_relative '../helper/jira_assign_helper'

module Fastlane
  module Actions
    class JiraAssignTestStatusesAction < Action
      def self.run(params)
        Actions.verify_gem!('jira-ruby')
        require 'jira-ruby'

        site         = params[:url]
        auth_type    = :basic
        context_path = params[:context_path]
        username     = params[:username]
        password     = params[:password]
        ticket_ids   = params[:ticket_ids]
        
        options = {
                    site: site,
                    context_path: context_path,
                    auth_type: auth_type,
                    username: username,
                    password: password
                  }

        begin
          client = JIRA::Client.new(options)
          client.Field.map_fields

          tickets_list = ticket_ids.split(",")

          for ticket_id in tickets_list do
            begin
              issue = client.Issue.find(ticket_id)

              begin
                reviewer_id = issue.Reviewer['accountId']
                UI.message("Reviewr is #{reviewer_id}")
              rescue => ex
                UI.message("Task has no reviewer")
              end

              available_transitions = client.Transition.all(:issue => issue)
              available_transitions.each {|ea| UI.success("#{ea.name} (id #{ea.id})")}
            rescue => ex
              UI.message("Fail to get task #{ex}")
            end
          end
          
          return 1
        rescue => exception
          message = "Received exception: #{exception}"
          if params[:fail_on_error]
            UI.user_error!(message)
          else
            UI.error(message)
          end
        end


      end

      def self.description
        "Simple plugin to get possible jira issue statuses"
      end

      def self.authors
        ["Roma Bakenbard"]
      end

      def self.return_value
        # If your method provides a return value, you can describe here what it does
      end

      def self.details
        # Optional:
        "Simple plugin to get possible jira issue statuses"
      end

      def self.available_options
        [
          FastlaneCore::ConfigItem.new(key: :url,
                                      env_name: "FL_JIRA_SITE",
                                      description: "URL for Jira instance",
                                       verify_block: proc do |value|
                                         UI.user_error!("No url for Jira given, pass using `url: 'url'`") if value.to_s.length == 0
                                       end),
          FastlaneCore::ConfigItem.new(key: :context_path,
                                      env_name: "FL_JIRA_CONTEXT_PATH",
                                      description: "Appends to the url (ex: \"/jira\")",
                                      optional: true,
                                      default_value: ""),
          FastlaneCore::ConfigItem.new(key: :username,
                                       env_name: "FL_JIRA_USERNAME",
                                       description: "Username for Jira instance",
                                       verify_block: proc do |value|
                                         UI.user_error!("No username") if value.to_s.length == 0
                                       end),
          FastlaneCore::ConfigItem.new(key: :password,
                                       env_name: "FL_JIRA_PASSWORD",
                                       description: "Password or API token for Jira",
                                       sensitive: true,
                                       verify_block: proc do |value|
                                         UI.user_error!("No password") if value.to_s.length == 0
                                       end),
          FastlaneCore::ConfigItem.new(key: :ticket_ids,
                                       env_name: "FL_JIRA_TICKET_IDS",
                                       description: "Ticket IDs for Jira, i.e. IOS-123",
                                       verify_block: proc do |value|
                                         UI.user_error!("No Tickets specified") if value.to_s.length == 0
                                       end),
          FastlaneCore::ConfigItem.new(key: :fail_on_error,
                                       env_name: "FL_JIRA_FAIL_ON_ERROR",
                                       description: "Should an error adding the Jira comment cause a failure?",
                                       type: Boolean,
                                       optional: true,
                                       default_value: true) # Default value is true for 'Backward compatibility'
        ]
      end

      def self.is_supported?(platform)
        # Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
        # See: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
        #
        # [:ios, :mac, :android].include?(platform)
        true
      end
    end
  end
end
