require 'fileutils'
require 'colored2'

require_relative 'MessageBank'
require_relative 'ConfigureIOS'
require_relative 'ConfigureSwift'
require_relative 'ProjectManipulator'

module Pod
  class TemplateConfigurator

    attr_reader :pod_name, :pods_for_podfile, :prefix,:prefixes, :test_example_file, :username, :email,:temp_path

    def initialize(pod_name,framework,prefix,author,temp_path,is_simple)
      @temp_path = temp_path
      @pod_name = pod_name
      @framework = framework
      @prefix = prefix
      @author = author
      @is_simple = is_simple
      @pods_for_podfile = []
      @prefixes = []
      @message_bank = MessageBank.new(self)
    end

    def ask(question)
      answer = ""
      loop do
        puts "\n#{question}?"

        @message_bank.show_prompt
        answer = gets.chomp

        break if answer.length > 0

        print "\nYou need to provide an answer."
      end
      answer
    end

    def ask_with_answers(question, possible_answers)

      print "\n#{question}? ["

      print_info = Proc.new {

        possible_answers_string = possible_answers.each_with_index do |answer, i|
           _answer = (i == 0) ? answer.underlined : answer
           print " " + _answer
           print(" /") if i != possible_answers.length-1
        end
        print " ]\n"
      }
      print_info.call

      answer = ""

      loop do
        @message_bank.show_prompt
        answer = STDIN.gets.downcase.chomp

        answer = "yes" if answer == "y"
        answer = "no" if answer == "n"

        # default to first answer
        if answer == ""
          answer = possible_answers[0].downcase
          print answer.yellow
        end

        break if possible_answers.map { |a| a.downcase }.include? answer

        print "\nPossible answers are ["
        print_info.call
      end

      answer
    end

    def run
      # @message_bank.welcome_message

      if @framework == "swift"
        ConfigureSwift.perform(configurator: self)
      else
        ConfigureIOS.perform(configurator: self)
      end

      replace_variables_in_files
      clean_template_files
      rename_template_files

      if @is_simple != true
        add_yk_pods
      end


      add_pods_to_podfile
      
      customise_prefix
      rename_classes_folder
      ensure_carthage_compatibility
      reinitialize_git_repo
      run_pod_install

      # @message_bank.farewell_message
    end

    def add_yk_pods
      self.add_pod_to_podfile "YKCategoryComponent"
      self.add_pod_to_podfile "YKModuleLifeCircleComponent"
      self.add_pod_to_podfile "YKRouterComponent"
      self.add_pod_to_podfile "YKModuleServiceComponent"
      
      if @framework == "swift"
        self.add_pod_to_podfile "SnapKit"
      else
        self.add_pod_to_podfile "YKModuleServiceComponent"
      end

    end
    #----------------------------------------#
    
    def ensure_carthage_compatibility
      # FileUtils.ln_s("#{$current_dir}/Example/Pods/Pods.xcodeproj", "#{$current_dir}/_Pods.xcodeproj")
    end

    def run_pod_install
      puts "\nRunning " + "pod install".magenta + " on your new library."
      puts ""

      Dir.chdir("#{$current_dir}/Example") do
        # system "pod install"
      end

      # `git add #{$current_dir}/Example/#{pod_name}.xcodeproj/project.pbxproj`
      # `git commit -m "Initial commit"`
    end

    def clean_template_files
      ["./**/.gitkeep", "configure", "_CONFIGURE.rb", "README.md", "LICENSE", "templates", "setup", "CODE_OF_CONDUCT.md"].each do |asset|
        `rm -rf #{$current_dir}/#{asset}`
      end
    end

    def replace_variables_in_files
      # file_names = ['POD_LICENSE', 'POD_README.md', 'NAME.podspec', '.travis.yml', podfile_path]
      file_names = ['POD_LICENSE', 'POD_README.md', 'NAME.podspec', podfile_path]
      file_names.each do |file_name|
        # text = File.read(file_name)
        text = File.read("#{$current_dir}/#{file_name}")
        # text = File.read("./#{file_name}")
        text.gsub!("${POD_NAME}", @pod_name)
        text.gsub!("${REPO_NAME}", @pod_name.gsub('+', '-'))
        text.gsub!("${USER_NAME}", user_name)
        text.gsub!("${USER_EMAIL}", user_email)
        text.gsub!("${YEAR}", year)
        text.gsub!("${DATE}", date)
        # File.open(file_name, "w") { |file| file.puts text }
        File.open("#{$current_dir}/#{file_name}", "w") { |file| file.puts text }
      end
    end

    def add_pod_to_podfile podname
      @pods_for_podfile << podname
    end

    def add_pods_to_podfile
      podfile = File.read "#{$current_dir}/#{podfile_path}"
      podfile_content = @pods_for_podfile.map do |pod|
        "pod '" + pod + "'"
      end.join("\n    ")
      podfile.gsub!("${INCLUDED_PODS}", podfile_content)
      File.open("#{$current_dir}/#{podfile_path}", "w") { |file| file.puts podfile }
      
    end

    def add_line_to_pch line
      @prefixes << line
    end

    def customise_prefix
      prefix_path = "Example/Tests/Tests-Prefix.pch"
      return unless File.exists? prefix_path

      pch = File.read prefix_path
      pch.gsub!("${INCLUDED_PREFIXES}", @prefixes.join("\n  ") )
      File.open(prefix_path, "w") { |file| file.puts pch }
    end

    def set_test_framework(test_type, extension, folder)
      content_path = "setup/test_examples/" + test_type + "." + extension
      tests_path = "templates/" + folder + "/Example/Tests/Tests." + extension
      tests = File.read tests_path
      tests.gsub!("${TEST_EXAMPLE}", File.read(content_path) )
      File.open(tests_path, "w") { |file| file.puts tests }
    end

    def rename_template_files
      FileUtils.mv "#{$current_dir}/POD_README.md", "#{$current_dir}/README.md"
      FileUtils.mv "#{$current_dir}/POD_LICENSE", "#{$current_dir}/LICENSE"
      FileUtils.mv "#{$current_dir}/NAME.podspec", "#{$current_dir}/#{pod_name}.podspec"
    end

    def rename_classes_folder
      # FileUtils.mv "#{@pod_name}/Pod", @pod_name
      FileUtils.mv "#{$current_dir}/Pod", "#{$current_dir}/#{@pod_name}"
    end


    def reinitialize_git_repo
      Dir.chdir("#{$current_dir}") do
      `rm -rf .git`
      `git init`
      `git add -A`
      end

      
    end

    def validate_user_details
        return (user_email.length > 0) && (user_name.length > 0)
    end

    #----------------------------------------#

    def user_name
      # (ENV['GIT_COMMITTER_NAME'] || github_user_name || `git config user.name` || `<GITHUB_USERNAME>` ).strip
      (ENV['GIT_COMMITTER_NAME']  || `git config user.name` || `<GITHUB_USERNAME>` ).strip

    end


    def github_user_name
      github_user_name = `security find-internet-password -s github.com | grep acct | sed 's/"acct"<blob>="//g' | sed 's/"//g'`.strip
      is_valid = github_user_name.empty? or github_user_name.include? '@'
      return is_valid ? nil : github_user_name
    end

    def user_email
      (ENV['GIT_COMMITTER_EMAIL'] || `git config user.email`).strip
    end

    def year
      Time.now.year.to_s
    end

    def date
      Time.now.strftime "%m/%d/%Y"
    end

    def podfile_path
      'Example/Podfile'
    end

    #----------------------------------------#
  end
end