using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Data.words.Add("alma");
            //Data.words.Add("számítógép");

            for (int i = 0; i < Data.word.Count(); i++)
            {
                label1.Text += "-";
            }

            WordToChars(Data.word);
            Data.wordchar.Sort();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            char guess = Convert.ToChar(textBox1.Text);
            if (Isin(guess, Data.word))
            {
                if (CharNotInList(guess, Data.charfound))
                {
                    Data.charfound.Insert(0, guess);
                    Data.charfound.Sort();
                    label1.Text = labelTextChange(label1.Text);
                }

                if (Samelist(Data.charfound, Data.wordchar))
                {
                    MessageBox.Show("GRATULÁLOK!");
                }
                
                
                
            } else
            {
                Data.hiba++;
                if (Data.hiba > 11)
                {
                    MessageBox.Show("Sajnos vesztettél.");
                    Data.hiba = 0;
                }
                pictureBox1.Image = Image.FromFile($@"images\{Data.hiba}.png");
            }
        }


        public static bool Isin(char x, string word)
        {
            foreach (char c in word)
            {
                if (c == x)
                {
                    return true;
                }
            }
            return false;
        }

        public static void WordToChars(string word)
        {
            foreach (char c in word)
            {
                if (CharNotInList(c, Data.wordchar))
                {
                    Data.wordchar.Add(c);
                }
            }
        }

        public static bool CharNotInList(char c, List<char> charList)
        {
            foreach (char x in charList)
            {
                if (x == c)
                {
                    return false;
                }
            }
            return true;
        }

        public static bool Samelist(List<char> a, List<char> b)
        {
            if (a.Count != b.Count)
            {
                return false;
            }

            for (int i = 0; i < a.Count; i++)
            {
                if (a[i] != b[i])
                {
                    return false;
                }
            }

            return true;
        }


        public string labelTextChange(string current)
        {
            char[] temp = Data.word.ToCharArray();
            char[] sol = current.ToCharArray();
            while (Indexofchar(temp) != -1)
            {
                int a = Indexofchar(temp);
                sol[a] = Data.charfound[0];
                temp[a] = '_';
            }

            string asd = sol.ToString();

            return asd;
        }

        public int Indexofchar(char[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] == Data.wordchar[0])
                {
                    return i;
                }
            }
            return -1;
        }
    }

    public static class Data
    {
        //public static List<string> words = new List<string>();
        public static string word = "alma";
        public static List<char> wordchar = new List<char>();
        public static List<char> charfound = new List<char>();
        public static int hiba = 0;
    }
}
