using System.Xml.Linq;

namespace _2024._05._13
{
    public partial class Form1 : Form
    {
        private List<string> lines = new List<string>();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader("cuki.txt");
            string line = sr.ReadLine();

            while (line != null)
            {
                lines.Add(line);
                line = sr.ReadLine();
            }

            sr.Close();

            List<string> expensive = new List<string>() { "Hello", "0", "db" };
            List<string> cheap = new List<string>() { "Hello", "18400", "db" };

            int db = 0;

            Random rng = new Random();
            string[] randomLine = lines[rng.Next(0, lines.Count)].Split(";");
            SpecialTextBox.Text = $"Mai ajánlatunk: {randomLine[0]}";

            foreach (var i in lines)
            {
                string[] currentLine = i.Split(";");

                if (currentLine[2].Equals("true"))
                {
                    db += 1;
                }

                if (Convert.ToInt32(currentLine[3]) > Convert.ToInt32(expensive[1]))
                {
                    expensive[0] = currentLine[0];
                    expensive[1] = currentLine[3];
                    expensive[2] = currentLine[4];
                }

                if (Convert.ToInt32(currentLine[3]) < Convert.ToInt32(cheap[1]))
                {
                    cheap[0] = currentLine[0];
                    cheap[1] = currentLine[3];
                    cheap[2] = currentLine[4];
                }
            }

            WinnerTextBox.Text = $"{db} féle díjnyertes édességből választhat.";

            ExpensiveTextBox.Text = expensive[0].ToString();
            ValueExpensiveTextBox.Text = $"{expensive[1]} Ft/{expensive[2]}";
            CheapTextBox.Text = cheap[0].ToString();
            ValueCheapTextBox.Text = $"{cheap[1]} Ft/{cheap[2]}";
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            double count = 0;
            double value = 0;

            if (TypeTextBox.Text.Equals(""))
            {
                MessageBox.Show("Nem írtál be süteménynevet!");
            }

            else
            {
                lines.Clear();
                StreamReader sr = new StreamReader("cuki.txt");
                string line = sr.ReadLine();

                while (line != null)
                {
                    lines.Add(line);
                    line = sr.ReadLine();
                }

                sr.Close();

                StreamWriter sw = new StreamWriter("ajanlat.txt");
                foreach (var i in lines)
                {
                    string[] currentLine = i.Split(";");

                    if (currentLine[1].Equals(TypeTextBox.Text))
                    {
                        sw.WriteLine($"{currentLine[0]};{currentLine[3]};{currentLine[4]}");
                        count += 1;
                        value += int.Parse(currentLine[3]);
                    }
                }

                sw.Close();

                if (count == 0)
                {
                    MessageBox.Show("Nincs megfelelő sütink. Kérjük, válassz mást!");
                }

                else
                {
                    MessageBox.Show($"{count} db sütit írtam az ajanlat.txt-be\nátlagár: {value / count} Ft");
                }
            }
        }

        private void CreateButton_Click(object sender, EventArgs e)
        {
            if (NameTextBox.Text.Equals("") || TypeCreateTextBox.Text.Equals("") || UnitTextBox.Text.Equals("") || ValueTextBox.Text.Equals(""))
            {
                MessageBox.Show("Nem adtál meg minden adatot!");
            }

            else
            {
                try
                {
                    int value = int.Parse(ValueTextBox.Text);

                    using (StreamWriter sw = File.AppendText("cuki.txt"))
                    {
                        sw.WriteLine($"{NameTextBox.Text};{TypeCreateTextBox.Text};{WinnerCheckBox.Checked};{ValueTextBox.Text};{UnitTextBox.Text}");
                    }

                    MessageBox.Show("Az állomány bővítése sikeres volt!");

                }

                catch
                {
                    MessageBox.Show("Az új sütemény ára nem szám!");
                }
            }
        }
    }
}