using System;
using PseudoRandomGenerator;


namespace Mastermind
{
    /// <summary>
    /// Manages the game mechanics. It includes methods for generating the code, providing feedback after each guess, and checking the win condition
    /// </summary>
    public class MastermindGame
    {
        private int[] code;
        private readonly int code_length;
        private readonly bool duplicates;

        /// <summary>
        /// Initializes a new game with the specified difficulty level
        /// </summary>
        /// <param name="difficulty">
        /// The difficulty level of the game,determines the length of the code and the number of attempts
        /// </param>
        public MastermindGame(string difficulty)
        {
            // Set parameters based on difficulty level
            switch(difficulty.ToLower())
            {
                case "easy":
                    code_length = 4;
                    duplicates = false;
                    break;
                case "medium":
                    code_length = 6;
                    duplicates = false;
                    break;
                case "hard":
                    code_length = 8;
                    duplicates = true;
                    break;
                default:
                    throw new ArgumentException("Invalid difficulty level");
            }

            GenerateCode();
            foreach(int zahl in code)
            {
                Console.WriteLine($"{zahl} ");
            }
        }

        /// <summary>
        /// Generates the code
        /// </summary>
        public void GenerateCode()
        {
            LCG lcg = new LCG(DateTime.Now.Ticks);
            code = new int[code_length];

            if(duplicates)
            {
                // Allow duplicates
                for(int i = 0; i < code_length; i++)
                {
                    code[i] = lcg.Next(0, 10);
                }
            } else
            {
                // No duplicates allowed
                for(int i = 0; i < code_length; i++)
                {
                    int newNumber;
                    bool isDuplicate;

                    do
                    {
                        isDuplicate = false;
                        newNumber = lcg.Next(0, 10); // Generates a number between 0 and 9

                        //Check if the generated number is already in the code
                        for (int j = 0; j < i; j++)
                        {
                            if (code[j] == newNumber)
                            {
                                isDuplicate = true;
                            }
                        }
                    } while (isDuplicate); // Repeat if duplicate was found

                    code[i] = newNumber; // Assign the unique number
                }
            }

            // For Testing this code:
            // code = new int[] { 1, 3, 4, 2 };
        }

        public void GetHint()
        {

        }

        public bool CheckWin(int[] input)
        {
            for(int i = 0; i < code.Length; i++)
            {
                if (input[i] != code[i])
                {
                    return false;
                }
            }
            return true;
        }

        public void DisplayBoard()
        {

        }

    }
}