using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            //Feladat1();
            Feladat2();
            //Feladat3();
        }

        static void Feladat3()
        {
            Dictionary<char, int> lookUpTable = new Dictionary<char, int>();

            //csökkentett értékek, mert programozásban ugye nem egytől számolunk
            lookUpTable.Add('A', 0);
            lookUpTable.Add('E', 1);
            lookUpTable.Add('I', 2);
            lookUpTable.Add('O', 3);
            lookUpTable.Add('U', 4);

            char[,] tablazat = CreateAlphabetMatrix();

            PrintMatrix(tablazat);

            string path = @"..\..\..\Sources\szoveg.txt";
            var data = File.ReadAllLines(path);

            List<string> secretWords = new List<string>();

            foreach (var line in data)
            {
                var currentWords = line.Split(' ');
                foreach (var item in currentWords)
                {
                    secretWords.Add(item);
                }
            }

            List<string> solvedWords = new List<string>();

            bool coordsShouldSwitch = false;

            foreach (var secret in secretWords)
            {
                List<char> solvedChars = new List<char>();
                for (int i = 0; i < secret.Length; i+=2)
                {
                    if (coordsShouldSwitch)
                    {
                        int row = lookUpTable[secret[i + 1]];
                        int col = lookUpTable[secret[i]];

                        solvedChars.Add(tablazat[row, col]);
                    } 
                    else
                    {
                        int row = lookUpTable[secret[i]];
                        int col = lookUpTable[secret[i + 1]];

                        solvedChars.Add(tablazat[row, col]);
                    }

                    coordsShouldSwitch = !coordsShouldSwitch;
                }

                solvedWords.Add(new string(solvedChars.ToArray()));
            }

            // a feladat

            foreach (var solved in solvedWords)
            {
                Console.WriteLine(solved);
            }

            int indexOfFirstWordWithQInIt = solvedWords.IndexOf(solvedWords.First(x => x.Contains('Q')));
            int indexOfLastWordWithQInIt = solvedWords.IndexOf(solvedWords.Last(x => x.Contains('Q')));



            //Console.WriteLine(solvedWords[indexOfFirstWordWithQInIt]);
            //Console.WriteLine(solvedWords[indexOfLastWordWithQInIt]);

            Console.WriteLine($"A szövegben lévő Q karakteres szavak közötti max távolság: {indexOfLastWordWithQInIt - indexOfFirstWordWithQInIt - 1}");

            // b feladat

            string pathToSzoveg2 = @"..\..\..\Sources\szoveg2.txt";
            var szoveg2Data = File.ReadAllText(pathToSzoveg2);

            char[] vowelsLookUp = { 'A', 'E', 'I', 'O', 'U' };

            List<char> vowels = new List<char>();

            foreach (char c in szoveg2Data)
            {
                if (vowelsLookUp.Contains(c))
                {
                    vowels.Add(c);
                }
            }

            string feladatBString = new string(vowels.ToArray());

            
            
            List<char> solvedCharsForFeladatB = new List<char>();
            for (int i = 0; i < feladatBString.Length; i += 2)
            {                
                int row = lookUpTable[feladatBString[i]];
                int col = lookUpTable[feladatBString[i + 1]];

                solvedCharsForFeladatB.Add(tablazat[row, col]);
                
            }

            Console.WriteLine($"B feladat megoldás: {new string(solvedCharsForFeladatB.ToArray())}");


            // c feladat
            string atirando = "EZTKELLATIRNI";
            string pathToSzavak = @"..\..\..\Sources\szavak.txt";

            var szavak = File.ReadAllLines(pathToSzavak);

            Dictionary<int, char> reversedLookUpTable = new Dictionary<int, char>();
            reversedLookUpTable.Add(0, 'A');
            reversedLookUpTable.Add(1, 'E');
            reversedLookUpTable.Add(2, 'I');
            reversedLookUpTable.Add(3, 'O');
            reversedLookUpTable.Add(4, 'U');

            List<char> titkositottKarakterek = new List<char>();

            foreach (char c in atirando)
            {
                for (int row = 0; row < tablazat.GetLength(0); row++)
                {
                    for (int col = 0; col < tablazat.GetLength(1); col++)
                    {
                        if (tablazat[row, col] == c)
                        {
                            titkositottKarakterek.Add(reversedLookUpTable[row]);
                            titkositottKarakterek.Add(reversedLookUpTable[col]);
                        }
                    }
                }
            }

            string feladatCString = new string(titkositottKarakterek.ToArray());
            Console.WriteLine(feladatCString);

            foreach (var item in szavak)
            {
                Console.WriteLine(item);
            }

            List<string> feladatCMegoldas = new List<string>();

            for (int i = 0; i < feladatCString.Length; i+=2)
            {
                char firstChar = feladatCString[i];
                char secondChar = feladatCString[i + 1];

                string pattern;
                if (firstChar == secondChar)
                {
                    pattern = $@"^(?=.*{firstChar}.*{firstChar}).*{firstChar}.*{firstChar}.*$";
                }
                else
                {
                    pattern = $@"^(?!.*{firstChar}.*{firstChar})(?!.*{secondChar}.*{secondChar}).*{firstChar}.*{secondChar}.*$";
                }
                Regex regex = new Regex(pattern);

                foreach (string szo in szavak)
                {
                    if (regex.IsMatch(szo))
                    {
                        feladatCMegoldas.Add(szo);
                        //Console.WriteLine($"{firstChar} - {secondChar} - {szo}");
                        break;
                    }
                }
            }

            // c megoldás

            string cmegoldasString = "";
            foreach (var item in feladatCMegoldas)
            {
                Console.Write(item + " ");
                cmegoldasString += item + " ";
            }
            Console.WriteLine();

            

            //Ellenőrzés a kapott kódra
            /*
            
            List<char> solvedCharsForFeladatC = new List<char>();
            for (int i = 0; i < feladatCString.Length; i += 2)
            {
                int row = lookUpTable[feladatCString[i]];
                int col = lookUpTable[feladatCString[i + 1]];

                solvedCharsForFeladatC.Add(tablazat[row, col]);

            }

            Console.WriteLine(  new string(solvedCharsForFeladatC.ToArray()));
            */

            // d feladat bizonyítás
            
            /*
            List<char> maganhangzok = new List<char>();

            foreach (char c in cmegoldasString)
            {
                if (vowelsLookUp.Contains(c))
                {
                    maganhangzok.Add(c);
                }
            }

            string asd = new string(vowels.ToArray());



            List<char> megtalatkarakterek = new List<char>();
            for (int i = 0; i < feladatCString.Length; i += 2)
            {
                int row = lookUpTable[feladatCString[i]];
                int col = lookUpTable[feladatCString[i + 1]];

                megtalatkarakterek.Add(tablazat[row, col]);

            }

            Console.WriteLine(new string(megtalatkarakterek.ToArray()));
            */
            
        }

        static char[,] CreateAlphabetMatrix()
        {
            char[,] matrix = new char[5, 5];
            List<char> alphabet = new List<char>();

            // Add letters A-Z except for 'W'
            for (char c = 'A'; c <= 'Z'; c++)
            {
                if (c != 'W')
                {
                    alphabet.Add(c);
                }
            }

            // Fill the matrix with the alphabet
            int index = 0;
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    matrix[i, j] = alphabet[index];
                    index++;
                }
            }

            return matrix;
        }

        static void PrintMatrix(char[,] matrix)
        {
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write(matrix[i, j] + " ");
                }
                Console.WriteLine();
            }
        }


        static void Feladat2()
        {
            string path = @"..\..\..\Sources\dobasok.txt";
            string path2 = @"..\..\..\Sources\dontesek.txt";

            var dobasok = new List<int>();
            var dontesek = new List<int>();

            //d feladathoz
            int pokermaxNum = 0;


            //dobasok

            var data = File.ReadAllText(path).Trim();
            foreach (char c in data)
            {
                dobasok.Add(Convert.ToInt32($"{c}"));
            }



            //dontesek

            var data2 = File.ReadAllText(path2).Trim();

            foreach (char c in data2)
            {
                dontesek.Add(Convert.ToInt32($"{c}"));
            }

            
            Jatekos alfa = new Jatekos();
            alfa.Nev = "Alfa";
            Jatekos beta = new Jatekos();
            beta.Nev = "Beta";
            Jatekos gamma = new Jatekos();
            gamma.Nev = "Gamma";

            List<Jatekos> jatekosok = new List<Jatekos>();
            jatekosok.Add(alfa);
            jatekosok.Add(beta);
            jatekosok.Add(gamma);

            List<Kor> korok = new List<Kor>();

            

            List<int> dobasokSmallerScope = new List<int>();

            int jatekosCounter = 0;

            List<int> dobasokFull = new List<int>();
            List<int> dontesekFull = new List<int>();

            for (int i = 0; i < dobasok.Count; i++)
            {
                int dobas = dobasok[i];
                int dontes = dontesek[i];

                dobasokFull.Add(dobas);
                dontesekFull.Add(dontes);

                if (dontes == 1)
                {
                    dobasokSmallerScope.Add(dobas);

                    if (dobasokSmallerScope.Count == 5)
                    {
                        Kor kor = new Kor();
                        kor.dobasok = dobasokSmallerScope.ToArray();
                        kor.jatekos = jatekosok[jatekosCounter];

                        kor.dobasokFull = dobasokFull.ToArray();
                        kor.dontesekFull = dontesekFull.ToArray();

                        dobasokFull.Clear();
                        dontesekFull.Clear();

                        korok.Add(kor);

                        dobasokSmallerScope.Clear();

                        if (jatekosCounter == 2)
                        {
                            jatekosCounter = 0;
                        } else
                        {
                            jatekosCounter++;
                        }
                       
                        continue;
                    }
                }

            }

            // a feladat
            Console.WriteLine($"Teljes körök száma: {korok.Count / 3}");

            // b feladat

            

            foreach (var kor in korok)
            {
                foreach (var jatekos in jatekosok)
                {
                    if (jatekos.Nev == kor.jatekos.Nev)
                    {
                        int[] dobasokAKorben = kor.dobasok;

                        bool sorCondition = dobasokAKorben.Distinct().Count() == 5;

                        //nagysor
                        if (sorCondition && dobasokAKorben.Contains(6))
                        {
                            jatekos.Minta.Nagysor = true;
                            continue;
                        }

                        //kissor
                        if (sorCondition && dobasokAKorben.Contains(1))
                        {
                            jatekos.Minta.Kissor = true;
                            continue;
                        }

                        //poker
                        var pokerGroup = dobasokAKorben.GroupBy(x => x).FirstOrDefault(g => g.Count() == 4);
                        if (pokerGroup != null)
                        {
                            int pokerValue = pokerGroup.Key;
                            if (pokerValue > pokermaxNum)
                            {
                                pokermaxNum = pokerValue;
                            }
                            jatekos.Minta.Poker = true;                            
                            continue;
                        }

                        //full
                        if (dobasokAKorben.Count(x => x == 3) == 1 && dobasokAKorben.Count(x => x == 2) == 1)
                        {
                            jatekos.Minta.Full = true;
                            continue;
                        }

                        //terc
                        if (dobasokAKorben.Count(x => x == 3) == 1)
                        {
                            jatekos.Minta.Terc = true;
                            continue;
                        }

                        //ketpar
                        if (dobasokAKorben.Count(x => x == 2) == 2)
                        {
                            jatekos.Minta.KetPar = true;
                            continue;
                        }

                        //egypar
                        if (dobasokAKorben.Count(x => x == 2) == 1)
                        {
                            jatekos.Minta.EgyPar = true;
                            continue;
                        }
                    }
                }
            }

            foreach (var jatekos in jatekosok)
            {
                if (jatekos.Minta.AllTrue())
                {
                    Console.WriteLine($"{jatekos.Nev} nyerte meg a játékot");
                }
            }

            // c feladat

            int counter = 0;

            foreach (var kor in korok)
            {
                if (kor.jatekos.Nev == "Gamma")
                {
                    int[] dobasokAKorben = kor.dobasok;
                    counter++;
                    if (dobasokAKorben.Count(x => x == 3) == 1 && dobasokAKorben.Count(x => x == 2) == 1)
                    {
                            
                        break;
                    }
                }
                
            }

            Console.WriteLine($"Gamma ebbe a körbe dobott full-t: {counter + 1}");

            // d feladat

            Console.WriteLine($"A legnagyobb poker értéke: {pokermaxNum}");


            // e feladat
            foreach (var kor in korok)
            {
                List<int[]> asztalok = new List<int[]>();
                List<int> currentAsztal = new List<int>();
                List<int> currentDontesek = new List<int>();

                int iter = 0;

                while (iter < kor.dobasokFull.Length)
                {
                    while (currentAsztal.Count != 5)
                    {
                        currentAsztal.Add(kor.dobasokFull[iter]);
                        currentDontesek.Add(kor.dontesekFull[iter]);

                        iter++;
                    }

                    if (currentAsztal.Count == 5)
                    {
                        asztalok.Add(currentAsztal.ToArray());
                        List<int> tempAsztal = new List<int>(currentAsztal);
                        List<int> tempDontesek = new List<int>(currentDontesek);
                        currentAsztal.Clear();
                        currentDontesek.Clear();

                        for (int i = 0; i < tempAsztal.Count; i++)
                        {
                            if (tempDontesek[i] == 1)
                            {
                                currentAsztal.Add((int)tempAsztal[i]);
                                currentDontesek.Add(1);
                            }
                        }
                    }
                }

                kor.asztalok = asztalok;
            }

            int pairs = 0;

            foreach (var kor in korok)
            {
                foreach (var asztal in kor.asztalok)
                {
                    foreach (var item in asztal)
                    {
                        Console.Write($"{item} ");
                    }
                    Console.WriteLine();

                    pairs += CountPairs(asztal);
                }
                Console.WriteLine("-----");
            }

            Console.WriteLine($"Párok száma: {pairs}");

            
        }

        static void PopulateAsztal(Kor kor)
        {
            List<int[]> asztalok = new List<int[]>();
            List<int> currentAsztal = new List<int>();
            List<int> currentDontesek = new List<int>();

            int iter = 0;

            while (iter != kor.dobasokFull.Length - 1)
            {
                while (currentAsztal.Count != 5)
                {
                    currentAsztal.Add(kor.dobasokFull[iter]);
                    currentDontesek.Add(kor.dontesekFull[iter]);

                    iter++;
                }

                if (currentAsztal.Count == 5)
                {
                    asztalok.Add(currentAsztal.ToArray());
                    List<int> tempAsztal = currentAsztal;
                    List<int> tempDontesek = currentDontesek;
                    currentAsztal.Clear();
                    currentDontesek.Clear();

                    for (int i = 0; i < tempAsztal.Count; i++)
                    {
                        if (tempDontesek[i] == 1)
                        {
                            currentAsztal.Add((int)tempAsztal[i]);
                            currentDontesek.Add(1);
                        }
                    }
                }
            }
        }


        static int CountPairs(int[] dobasok)
        {
            int pairs = 0;

            //terc
            if (dobasok.Count(x => x == 3) == 1)
            {
                return 0;
            }

            var pokerGroup = dobasok.GroupBy(x => x).FirstOrDefault(g => g.Count() == 4);
            if (pokerGroup != null)
            {
                return 0;
            }


            for (int i = 0; i < dobasok.Length; i++)
            {
                for (int j = i + 1; j < dobasok.Length; j++)
                {
                    if (dobasok[i] == dobasok[j])
                    {
                        pairs++;
                    }
                }
            }
            return pairs;
        }
        static void IntArrKiir(int[] arr)
        {
            foreach (var item in arr)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
        }

        static void Feladat1()
        {
            string path = @"..\..\..\Sources\idopontok.txt";

            var idopontok = new List<Idopont>();

            var data = File.ReadAllLines(path);
            foreach (var line in data)
            {
                var parts = line.Split(' ');
                Idopont idopont = new Idopont(int.Parse(parts[0]), int.Parse(parts[1]));
                idopontok.Add(idopont);
            }


            foreach (var idopont in idopontok)
            {
                Console.WriteLine($"{idopont.Name}, {idopont.ClockAngle()}°");
            }

            Console.WriteLine($"Legnagyobb szögű időpont: {idopontok.OrderByDescending(x => x.Angle).First().Name}");


            // nem biztos, hogy jó
            List<double> angleDifferences = new List<double>();
            for (int i = 0; i < idopontok.Count - 1; i++)
            {
                angleDifferences.Add(Math.Abs(idopontok[i].Angle - idopontok[i + 1].Angle));
            }

            Console.WriteLine($"Legkisebb változás: {angleDifferences.Min()}°");

            var idopontok2 = new List<Idopont>();
            int napok = 5;

            for (int i = 0; i < napok; i++)
            {
                for (int orak = 0; orak < 24; orak++)
                {
                    for (int percek = 0; percek < 60; percek++)
                    {
                        Idopont idopont = new Idopont(orak, percek);
                        idopont.ClockAngle();
                        idopontok2.Add(idopont);
                    }
                }
            }

            string path2 = @"..\..\..\Sources\szogek.txt";

            var data2 = File.ReadAllText(path2).Split(' ');
            List<double> anglesToLookFor = new List<double>();


            foreach (var item in data2)
            {
                string formattedItem = item.Replace('.', ',');
                anglesToLookFor.Add(Double.Parse(formattedItem));
            }

            int counter = 0;
            int iterations = 0;
            int napokCounter = 1;

            foreach (var item in idopontok2)
            {
                if (item.Angle == anglesToLookFor[counter])
                {
                    counter++;
                }

                if (counter == anglesToLookFor.Count)
                {
                    break;
                }

                iterations++;

                if (iterations == 1440)
                {
                    napokCounter++;
                    iterations = 0;
                }
            }

            Console.WriteLine($"{napokCounter}|{idopontok2[iterations].Name}");

        }
    }
}
