using System.Windows.Forms;

namespace _2024._04._28
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void megnyitásToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Szöveges fájlok (*.txt)|*.txt|All files (*.*)|*.*";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                StreamReader fileContent = new StreamReader(openFileDialog.FileName);
                string line = fileContent.ReadLine();

                while (line != null)
                {
                    string[] currentLine = line.Split(" ");
                    listBox1.Items.Add($"{currentLine[2]} {currentLine[3]}");

                    line = fileContent.ReadLine();
                }

                fileContent.Close();
            }
        }

        private void kilépésToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                List<string> lines = new List<string>();
                StreamReader file = new StreamReader("szavazatok.txt");
                string line = file.ReadLine();

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

                file.Close();

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

                    if ($"{currentLine[2]} {currentLine[3]}".Equals(listBox1.SelectedItem))
                    {
                        NameTextBox.Text = $"{currentLine[2]} {currentLine[3]}";
                        NumberPlaceTextBox.Text = currentLine[0];

                        if (currentLine[4].Equals("GYEP"))
                        {
                            PartTextBox.Text = "Gyümölcsevők Pártja";
                        }

                        else if (currentLine[4].Equals("HEP"))
                        {
                            PartTextBox.Text = "Húsevők Pártja";
                        }

                        else if (currentLine[4].Equals("TISZ"))
                        {
                            PartTextBox.Text = "Tejivók szövetsége";
                        }

                        else if (currentLine[4].Equals("ZEP"))
                        {
                            PartTextBox.Text = "Zöldségevők Pártja";
                        }

                        else
                        {
                            PartTextBox.Text = currentLine[4];
                        }

                        NumberSzavazatTextBox.Text = currentLine[1];
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
            {
                MessageBox.Show("Nincs kiválasztott képviselő!", "Hiba", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else
            {
                listBox2.Items.Clear();

                List<string> lines = new List<string>();
                StreamReader file = new StreamReader("szavazatok.txt");
                string line = file.ReadLine();

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

                file.Close();

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

                    if (currentLine[0].Equals(NumberPlaceTextBox.Text) && !string.Equals($"{currentLine[2]} {currentLine[3]}", NameTextBox.Text))
                    {
                        listBox2.Items.Add($"{currentLine[2]} {currentLine[3]} ({currentLine[1]} szavazat)");
                    }
                }
            }
        }

        private void statisztikaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count == 0)
            {
                MessageBox.Show("Nincsenek képviselő adatok!", "Hiba", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else
            {
                Form2 form2 = new Form2();
                form2.Show();
            }
        }

        private void nyertesekToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<string> lines = new List<string>();
            StreamReader file = new StreamReader("szavazatok.txt");
            string line = file.ReadLine();
            Dictionary<int, string> winners = new Dictionary<int, string>();

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

            file.Close();

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

                int district = Convert.ToInt32(currentLine[0]);
                string name = $"{currentLine[2]} {currentLine[3]}";
                int votes = Convert.ToInt32(currentLine[1]);
                string party = currentLine[4];

                if (!winners.ContainsKey(district) || votes > Convert.ToInt32(winners[district].Split(" ")[0]))
                {
                    winners[district] = $"{votes} {name} {party}";
                }
            }

            StreamWriter w = new StreamWriter("nyertesek.txt");

            for (int i = 1; i <= winners.Count; i++)
            {
                string[] currentWinner = winners[i].Split(" ");
                w.WriteLine($"{i} {currentWinner[1]} {currentWinner[2]} {currentWinner[3]}");
            }

            w.Close();

            MessageBox.Show("A választókerületek nyertesei sikeresen meghatározva!", "Értesítés", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void névjegyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Helyhatósági választásokat elemző program", "Névjegy", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
