require 'optparse'

# rubocop:disable Metrics/ClassLength
class ArgumentHandler
  attr_reader :keychain,
              :keychain_password,
              :code_signing_identities,
              :provisioning_profiles,
              :dropbox_sources,
              :dropbox_destination,
              :dropbox_flatten,
              :dropbox_root,
              :kill_simulators,
              :xcode_project_path,
              :xcode_export_plist,
              :xcode_target,
              :xcode_build_configuration,
              :xcode_bundle_identifier,
              :xcode_build_number,
              :xcode_set_manual_provisioning_style,
              :xcode_archive_plist,
              :zip_sources,
              :zip_archive,
              :zip_destination,
              :verbose,
              :version

  def remove_quotes(file_path)
    file_path = file_path.to_s
    file_path[0] = '' if file_path.to_s.start_with?('\'', '"')
    file_path[file_path.length - 1] = '' if file_path.to_s.end_with?('\'', '"')
    file_path
  end

  # rubocop:disable Metrics/AbcSize
  # rubocop:disable Metrics/MethodLength
  def initialize
    @keychain = nil
    @keychain_password = nil
    @code_signing_identities = []
    @provisioning_profiles = []
    @dropbox_sources = []
    @dropbox_destination = nil
    @dropbox_flatten = true
    @dropbox_root = Dir.pwd
    @kill_simulators = false
    @xcode_project_path = nil
    @xcode_export_plist = nil
    @xcode_target = nil
    @xcode_build_configuration = nil
    @xcode_bundle_identifier = nil
    @xcode_build_number = nil
    @xcode_set_manual_provisioning_style = nil
    @xcode_archive_plist = nil
    @zip_sources = []
    @zip_archive = nil
    @zip_destination = nil
    @verbose = false
    @version = false

    ARGV.push('-h') if ARGV.empty?

    # rubocop:disable Metrics/BlockLength
    parser = OptionParser.new do |opts|
      opts.banner = 'Usage: jenkins_util'

      # keychain_util
      opts.on('-k', '--keychain [KEYCHAIN_PATH]', 'Path to Keychain to be unlocked') do |keychain|
        @keychain = keychain
      end

      # keychain_util
      opts.on('-p', '--keychain-password [KEYCHAIN_PASSWORD]', 'Password for Keychain to be unlocked') do |password|
        @keychain_password = password
      end

      # keychain_util
      opts.on('-c', '--code-signing-identities c1,c2', Array, 'Provisioning Profiles to check after keychain is unlocked') do |identities|
        @code_signing_identities = identities
      end

      # keychain_util
      opts.on('-P', '--provisioning-profiles p1,p2', Array, 'Provisioning Profiles to check after keychain is unlocked') do |profiles|
        @provisioning_profiles = profiles
      end

      # dropbox_util
      opts.on('-s', '--dropbox-sources s1,s2', Array, 'A set of paths/matchers for uploading to dropbox') do |sources|
        sources.each do |source|
          @dropbox_sources.push(remove_quotes(source))
        end
      end

      # dropbox_util
      opts.on('-d', '--dropbox-destination x', 'The dropbox destination usually /L4Builds/Path') do |destination|
        @dropbox_destination = remove_quotes(destination)
      end

      # dropbox_util
      opts.on('-f', '--dropbox-keep-folders', 'If passed file folders will be preserved') do
        @dropbox_flatten = false
      end

      # dropbox_util
      opts.on('-r', '--dropbox-root x', 'The path to remove when uploading defaults to pwd') do |root|
        @dropbox_root = remove_quotes(root)
      end

      # simulator_util
      opts.on('-K', '--kill-simulators', 'Shutdown and reset all iOS simulators') do
        @kill_simulators = true
      end

      # xcode_util
      opts.on('--xcode-project project', 'Path to an Xcode project file') do |project_path|
        @xcode_project_path = project_path
      end

      opts.on('--xcode-get-team-id plist', 'Path to an Xcode export plist') do |export_plist|
        @xcode_export_plist = export_plist
      end

      opts.on('--xcode-target target', 'Target to update in the Xcode project specified with --xcode-project') do |target|
        @xcode_target = target
      end

      opts.on('--xcode-build-configuration build_configuration',
              'Build configuration to update in the Xcode project specified with --xcode-project') do |build_configuration|
        @xcode_build_configuration = build_configuration
      end

      opts.on('--xcode-bundle-identifier identifer',
              'Bundle identifier to set in the Xcode project specified with --xcode-project') do |bundle_identifier|
        @xcode_bundle_identifier = bundle_identifier
      end

      opts.on('--xcode-append-bundle-version version',
              'the version passed is appended to the build version Xcode project specified with --xcode-project') do |build_number|
        @xcode_build_number = build_number
      end

      opts.on('--xcode-set-manual-provisioning-style', 'Set project to automatically sign if true and Manual if false') do
        @xcode_set_manual_provisioning_style = true
      end

      opts.on('--xcode-get-build-name archive-plist', 'Gets the build name "NAME_v_VERSION_b_BUILD"') do |archive_plist|
        @xcode_archive_plist = archive_plist
      end

      # zip_util
      opts.on('--zip-compress s1,s2,s3', Array, 'Compress files into zip archive') do |sources|
        sources.each do |source|
          @zip_sources.push(remove_quotes(source))
        end
      end

      opts.on('--zip-uncompress zip_archive', 'Uncompress zip archive') do |zip_archive|
        @zip_archive = remove_quotes(zip_archive)
      end

      opts.on('--zip-destination destination',
              'The path to save a new zip archive for a --zip-compress',
              'The path to a directory to uncompress a zip archive for --zip-uncompress') do |destination|
        @zip_destination = remove_quotes(destination)
      end

      # logger_util
      opts.on('--verbose', 'Output more information') do
        @verbose = true
      end

      opts.on_tail('-h', '--help', 'Show this message') do
        puts opts
        abort
      end

      opts.on_tail('-v', '--version', 'Display Current version') do
        @version = true
      end
    end

    parser.parse!
  end
end