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;
using System.IO;
using System.Text.RegularExpressions;

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

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FileStream fileStream = new FileStream("oscar.csv", FileMode.Open, FileAccess.Read);
            StreamReader streamReader = new StreamReader(fileStream);

            string line = streamReader.ReadLine();
            line = streamReader.ReadLine();

            while (line != null)
            {
                string[] content = line.Split(';');

                Film film = new Film(content[0], content[1], Convert.ToInt32(content[2]), Convert.ToInt32(content[3]), Convert.ToInt32(content[4]));

                Data.films.Add(film);

                line = streamReader.ReadLine();
            }

            streamReader.Close();
            fileStream.Close();

            foreach (var item in Data.films)
            {
                Data.filmnames.Add(item.cim);
            }

            Data.filmnames.Sort();

            foreach (var item in Data.filmnames)
            {
                listBox1.Items.Add(item);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Cim.Text != null && Ev.Text != null && Jelolesek.Text != null  && Dijak.Text != null)
            {
                char[] azon = new char[6];
                for (int i = Cim.Text.Length - 1, j = 0; i > Cim.Text.Length - 4; i--, j++)
                {
                    azon[j] = Cim.Text[i];
                }
                azon[4] = Ev.Text[Ev.Text.Length - 1];
                azon[5] = Ev.Text[Ev.Text.Length - 2];

                string azonosito = azon.ToString();

                Film film = new Film(azonosito, Cim.Text, Convert.ToInt32(Ev.Text), Convert.ToInt32(Jelolesek.Text), Convert.ToInt32(Dijak.Text));
                Data.films.Add(film);
                return;
            }

            MessageBox.Show("ERROR");
            return;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Film_cime.Text = $"Film címe: {Data.films[Filmname_index()].cim}";
        }

        private int Filmname_index()
        {
            int max = Data.films[0].dij;
            foreach (var item in Data.films)
            {
                if (item.dij > max)
                {
                    max = item.dij;
                }
            }

            return max;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string cim = Search(Searched.Text);
            string talalat = $"Találat kiírása: {cim}";

            Talalat.Text = talalat;
        }

        private string Search(string searched_word)
        {
            string pattrern = $"{searched_word}".ToLower();
            Regex regex = new Regex(pattrern);
            if (searched_word == "")
            {
                MessageBox.Show("ERROR");
                return "";
            }

            foreach (var item in Data.filmnames)
            {
                if (regex.IsMatch(item.ToLower()))
                {
                    return item;
                }
            }
            return "";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox6.Text = Searched.Text;
            Search2(textBox6.Text);
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private void Search2(string searched_word)
        {
            string pattrern = $"{searched_word}".ToLower();
            Regex regex = new Regex(pattrern);
            if (searched_word == "")
            {
                MessageBox.Show("ERROR");
                return;
            }

            foreach (var item in Data.filmnames)
            {
                if (regex.IsMatch(item.ToLower()))
                {
                    listBox2.Items.Add(item);
                }
            }
            return;
        }
    }

    public static class Data
    {
        public static List<Film> films = new List<Film>();
        public static List<string> filmnames = new List<string>();
    }

    public class Film
    {
        public string azonosito;
        public string cim;
        public int ev;
        public int dij;
        public int jelolt;

        public Film(string azonosito, string cim, int ev, int dij, int jelolt)
        {
            this.azonosito = azonosito;
            this.cim = cim;
            this.ev = ev;
            this.dij = dij;
            this.jelolt = jelolt;
        }
    }
}
