require 'thor'
require 'fileutils'
require 'fwtoolkit/config'
require 'fwtoolkit/projectfile'
require 'fwtoolkit/cli/fw_actions'
require 'fwtoolkit/cli/thorutils'

module FWToolkit
    class Bitrise < Thor
      include Thor::Actions
      include FWToolkit::ThorUtils

      source_root_templates!

      desc 'genereate [PROJECT NAME] [PLATFORM]', "Create a new bitrise.yml file for the project, based on a blank file. Platform needs to be either ios or android. ios is used by default"
      def generate(project_name, platform='ios')
        destination_root = Dir.pwd

        if platform == 'android'
          invoke FWToolkit::Bitrise, 'android', [destination_root, project_name]
        else
          invoke FWToolkit::Bitrise, 'ios', [destination_root, project_name]
        end
      end

      desc 'android [PROJECT ROOT PATH] [PACKAGE_NAME]', 'Creates a new Android bitrise YML configuration file'
      def android(project_root, package_name)
        say "Creating android bitrise.yml file in: #{File.expand_path project_root}/bitrise.yml"
        destination_root = project_root
        bitrise_file = File.join(project_root, 'bitrise.yml')

        Projectfile.load_with_config! :project_name => package_name

        template_config = { :target_platform => Config.target_platform,
            :organization_name => Config.organization_name,
            :project_creator => Config.developer_name,
            :package_name => package_name } 
        template_config.merge! Projectfile.config

        template_directory "templates/bitrise/android", destination_root, template_config
      end

      desc 'ios [PROJECT ROOT PATH] [PROJECT NAME]', 'Creates a new iOS bitrise YML configuration file'
      def ios(project_root, project_name)
        say "Creating ios bitrise.yml file in: #{File.expand_path project_root}/bitrise.yml"
        destination_root = project_root
        bitrise_file = File.join(project_root, 'bitrise.yml')

        Projectfile.load_with_config! :project_name => project_name

        template_config = { :target_platform => Config.target_platform,
            :organization_name => Config.organization_name,
            :project_creator => Config.developer_name } 
        template_config.merge! Projectfile.config 

        template_directory "templates/bitrise/ios", destination_root, template_config
      end

    end
end