require_relative "board"
require_relative "computer"

module Mastermind
  class Game
    attr_accessor :board, :guess, :turn

    def initialize(board = Board.new)
      @board = board
      @turn = 0
    end

    def welcome_message
      puts "\n"
      puts "Welcome to Mastermind!"
      puts "\n"
      board.display.each { |x| add_color(x) }
      puts "\n"
    end

    def show_instructions
      puts "these are the instructions"
    end

    def solicit_move
      puts "Please select 4 of the following colors, first letters only, no duplicates: [r]ed [g]reen [y]ellow [b]lue [p]urple [c]yan"
    end

    def get_move(input = gets.chomp.downcase)
      permissible_letters = %w[r g y b p c]
      until input.length == 4 && input.chars.all? { |x| permissible_letters.include? x } && input.chars.uniq.length == 4
        puts "you must enter exactly 4 guesses, as letters only, no duplicates; try again"
        input = gets.chomp.downcase
      end
      @guess = input
    end

    def add_color(arr)
      str = ""
      arr.each do |x|
        str << if x == "r"
          "#{"0".red} "
        elsif x == "g"
          "#{"0".green} "
        elsif x == "b"
          "#{"0".blue} "
        elsif x == "y"
          "#{"0".yellow} "
        elsif x == "p"
          "#{"0".purple} "
        elsif x == "c"
          "#{"0".neon} "
        elsif x == "0"
          "#{"0".gray} "
        elsif x == "|"
          "| "
        elsif x == "\e[31mx\e[0m"
          "#{"x".red} "
        elsif x == "\e[32mx\e[0m"
          "#{"x".green} "
        else
          "#{"x".gray} "
        end
      end
      puts str
    end

    def compare_guess_to_solution(guess, solution)
      guess_to_array = guess.chars
      solution_to_array = solution.chars
      board.hints = []
      i = 0
      while i < guess.chars.length
        if guess_to_array[i] == solution[i]
          board.hints << "x".green
          i += 1
        elsif solution_to_array.include?(guess_to_array[i])
          board.hints << "x".red
          i += 1
        else
          i += 1
        end
      end
      board.hints
    end

    def play
      welcome_message
      show_instructions
      if board.make_code? != true
        until @turn == 12
          solicit_move
          get_move
          arr1 = guess.chars
          arr1 << "|"
          compare_guess_to_solution(@guess, board.solution)
          arr1.concat(board.hints).flatten!
          board.display << arr1
          board.display.slice! 0
          board.display.each do |turn_guess|
            add_color(turn_guess)
          end
          puts ""
          @turn += 1
          if guess != board.solution && @turn < 12
            puts "guess again"
          elsif guess != board.solution && @turn >= 12
            puts "you lose"
            puts "the solution was: #{board.solution}"
          else
            puts "you win"
            break
          end
        end
      else
        board.solution = board.user_set_solution
        puts "the solution you changed it to is: #{board.solution}"
        computer = Mastermind::Computer.new
        puts "there is now a computer called #{computer.name} ready to make guesses"
        guess = computer.make_a_guess
        until @turn == 12
          arr1 = guess.split ""
          arr1 << "|"
          compare_guess_to_solution(guess, board.solution)
          arr1.concat(board.hints).flatten!
          board.display << arr1
          board.display.slice! 0
          board.display.each do |turn_guess|
            add_color(turn_guess)
          end
          puts ""
          @turn += 1
          if guess != board.solution && @turn < 12
            puts "Computer guesses again"
            guess = computer.make_a_guess
          elsif guess != board.solution && @turn >= 12
            puts "Computer loses"
            puts "the solution was: #{board.solution}"
          else
            puts "The Computer win"
            break
          end
        end
      end
    end
  end
end

# game = Mastermind::Game.new
# game.play