﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Util;

namespace UI.Consola
{
    static class Game
    {
        enum dif { easy, normal, hard, very_hard }
        static dif Difficulty { get; set; }
        static List<string> Words { get; set; }
        static string Password { get; set; }
        static List<string> WordSet { get; set; }

        //---------------------------------------------------------------------------------
        static void Main(string[] args)
        {
            //Menu();

            //temporary until implementation of difficulty method
            Console.WriteLine("Enter password length");
            int length = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter number of words");
            int wordAmount = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter minimum number of characters in common");
            int lowerLimit = Convert.ToInt32(Console.ReadLine());                         

            Words = GetWords(length);
            Password = GetPassword(Words);

            WordSet = GetWordSet(Words, wordAmount, lowerLimit, Password);

            foreach(string line in WordSet)
            {
                Console.WriteLine(line);
            }

            Console.WriteLine("The password is {0} " , Password);
            Console.ReadKey();

        }

        //----------------------------------------------------------------------------------

        static void Menu()
        {
            Console.WriteLine("1 - Play");
            Console.WriteLine("2 - Help");
            Console.WriteLine("3 - Exit");

            bool exit = false;
            do
            {
                var key = Console.ReadKey();
                switch (key.Key)
                {
                    case (ConsoleKey.D1):
                        Console.Clear();
                        StartGame();
                        break;
                    case (ConsoleKey.D2):
                        ShowHelp();
                        break;
                    case (ConsoleKey.D3):
                        exit = true;
                        break;
                }
            } while (exit == false);
        }



        static void StartGame( List<string> playset, string password, int attempts, int difficulty)
        {
            Console.WriteLine("Fallout Hacking Game");
            foreach( string word in playset )
            {
                Console.WriteLine(word);
            }

            bool winCondition = false;

            while(winCondition)
            {
                Console.WriteLine("Enter your guess");
                string guess = Console.ReadLine();
                //repeat
                WinCondition(guess, password);
            }
        }
        static bool WinCondition(string guess, string password)
        {
            if (guess.Length == password.Length)
            {
                if (guess == password)
                {
                    return true;
                }
                else
                {
                    for (int i = 0; i < password.Length) ;
                }
            }
            else
            {

            }
        }
        static void ShowHelp()
        { }


        static List<string> GetWords(int wordLength)
        {

           
            
            var aux = File.ReadLines("C:\\Text\\enable1.txt");

            var words =
                from line in aux
                where line.Length == wordLength
                select line;
            List<string> list = new List<string>(words.Count());
            list = words.ToList();
            return list;      
        }

        static string GetPassword(IEnumerable<string> wordArray)
        {
            Random rand = new Random();
            string pass = Randomizer.RandomElement<string>(wordArray, rand);
            return pass;
        }

        static List<string> GetWordSet(List<string> wordArray,
            int wordAmount, int lowerLimit, string pass )
        {
            List<string> matching = (from line in wordArray
                           where MatchingWord(line, lowerLimit, pass)
                           select line).ToList();

            //Reduce list to playset amount. Insert password and shuffle.
            Random randing = new Random();

            List<string> playset = new List<string>(wordAmount);


            for (int i = 0; i < wordAmount - 1; i++)
            {
                playset.Add(matching.ElementAt(randing.Next(matching.Count)));
            }
            playset.Add(pass);

            Randomizer.Shuffle<string>(playset);

            return playset;
        }

        static bool MatchingWord(string word, int lowerLimit, string pass)
        {
            int occurrences = 0;

            for (int j = 0; j < word.Length; j++)
            {
                if (word[j] == pass[j])
                {
                    occurrences++;
                }
            }
            if ((occurrences >= lowerLimit) && (word != pass))
            {
                return true;
            }
            else return false;
        }
        static int SetDifficulty()
        {
            Console.Clear();
            Console.WriteLine("Difficulty: ");
            Console.WriteLine("1 - Easy");
            Console.WriteLine("2 - Normal");
            Console.WriteLine("3 - Hard");
            Console.WriteLine("4 - Very hard");

            var key = Console.ReadKey();
            switch (key.Key)
            {
                case (ConsoleKey.D1):
                    return 1;
                case (ConsoleKey.D2):
                    return 2;
                case (ConsoleKey.D3):
                    return 3;
                case (ConsoleKey.D4):
                    return 4;
                default: return 2;
            }
        }


    }
}