#!/usr/bin/env ruby
# rubocop:disable Metrics/AbcSize

$LOAD_PATH.push File.expand_path('../../lib', __FILE__)

require 'fastlane'
require 'commander'
require 'fastlane/new_action'

HighLine.track_eof = false

class FastlaneApplication
  include Commander::Methods

  def run
    program :version, Fastlane::VERSION
    program :description, [
      "CLI for 'fastlane' - Connect all iOS deployment tools into one streamlined workflow.\n",
      "\tRun using `fastlane [platform] [lane_name]`",
      "\tTo pass values to the lanes use `fastlane [platform] [lane_name] key:value key2:value2`"].join("\n")
    program :help, 'Author', 'Felix Krause <fastlane@krausefx.com>'
    program :help, 'Website', 'https://fastlane.tools'
    program :help, 'GitHub', 'https://github.com/krausefx/fastlane'
    program :help_formatter, :compact

    command :run do |c|
      c.syntax = 'fastlane run [lane]'
      c.description = 'Drive the fastlane for a specific environment. Pass the lane name and optionally the platform first'
      c.option '--env STRING', String, 'Add environment to use with `dotenv`'

      c.action do |args, options|
        if Fastlane::FastlaneFolder.path

          Fastlane::CommandLineHandler.handle(args, options)
        else
          create = agree('Could not find fastlane in current directory. Would you like to set it up? (y/n)'.yellow, true)
          Fastlane::Setup.new.run if create
        end
      end
    end

    command :init do |c|
      c.syntax = 'fastlane init'
      c.description = 'Helps you setting up fastlane based on your existing tools.'

      c.action do |args, options|
        Fastlane::Setup.new.run
      end
    end

    command :new_action do |c|
      c.syntax = 'fastlane new_action'
      c.description = 'Create a new custom action for fastlane.'

      c.action do |args, options|
        Fastlane::NewAction.run
      end
    end

    command :lanes do |c|
      c.syntax = 'fastlane lanes'
      c.description = 'Lists all available lanes and shows their description'

      c.action do |args, options|
        require 'fastlane/lane_list'
        path = File.join(Fastlane::FastlaneFolder.path || '.', 'Fastfile')
        Fastlane::LaneList.output(path)
      end
    end

    command :list do |c|
      c.syntax = 'fastlane list'
      c.description = 'Lists all available lanes without description'
      c.action do |args, options|
        ff = Fastlane::FastFile.new(File.join(Fastlane::FastlaneFolder.path || '.', 'Fastfile'))
        puts "\nAvailable lanes:".green
        ff.runner.available_lanes.each do |lane|
          puts "- #{lane}"
        end
        puts "\nExecute using `fastlane [lane_name]`".yellow
      end
    end

    command :docs do |c|
      c.syntax = 'fastlane docs'
      c.description = 'Generate a markdown based documentation based on the Fastfile'
      c.option '-f', '--force', 'Overwrite the existing README.md in the ./fastlane folder'

      c.action do |args, options|
        path = File.join(Fastlane::FastlaneFolder.path || '.', 'README.md')
        if File.exist? path and !options.force
          return nil unless agree('Found existing ./fastlane/README.md. Do you want to overwrite it? (y/n)', true)
        end

        ff = Fastlane::FastFile.new(File.join(Fastlane::FastlaneFolder.path || '.', 'Fastfile'))

        require 'fastlane/documentation/docs_generator'
        Fastlane::DocsGenerator.run(path, ff)
      end
    end

    command :actions do |c|
      c.syntax = 'fastlane actions'
      c.description = 'Lists all available fastlane actions'

      c.option '--platform STRING', String, 'Only show actions available on the given platform'

      c.action do |args, options|
        require 'fastlane/documentation/actions_list'
        Fastlane::ActionsList.run(filter: args.first, platform: options.platform)
      end
    end

    command :action do |c|
      c.syntax = 'fastlane action [tool_name]'
      c.description = 'Shows more information for a specific command'
      c.action do |args, options|
        require 'fastlane/documentation/actions_list'
        Fastlane::ActionsList.run(filter: args.first)
      end
    end

    default_command :run

    run!
  end
end

begin
  FastlaneCore::UpdateChecker.start_looking_for_update('fastlane')
  FastlaneApplication.new.run
ensure
  FastlaneCore::UpdateChecker.show_update_status('fastlane', Fastlane::VERSION)
end
# rubocop:enable Metrics/AbcSize