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

namespace garment_factory
{
    public partial class fLevenshtein : Form
    {
        public fLevenshtein()
        {
            InitializeComponent();
        }
        List<string> LstTkani = new List<string>();
        static int Lev(string t1, string t2)
        {
            int CntLev = Math.Abs(t1.Length - t2.Length);
            for (int i = 0; i <= Math.Min(t1.Length, t2.Length) - 1; i++)
            {
                if (t1[i] != t2[i])
                    CntLev++;
            }
            return CntLev;
        }
        private void fLevenshtein_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(Properties.Settings.Default.Fabrika2024ConnectionString);
            con.Open();
            string TxtQuery = "select Nazvanie from Tkani";

            SqlCommand query1 = new SqlCommand(TxtQuery, con);
            SqlDataReader res = query1.ExecuteReader();

            while (res.Read())
                LstTkani.Add(res["Nazvanie"].ToString());
            con.Close();

            BtnShowAll_Click(sender, e);
        }

        private void BtnFind_Click(object sender, EventArgs e)
        {
            DgvTkani.Rows.Clear();
            foreach (string Tkan in LstTkani)
                if (Lev(Tkan, TbxFind.Text) <= 3)
                    DgvTkani.Rows.Add(Tkan);
        }

        private void BtnShowAll_Click(object sender, EventArgs e)
        {
            DgvTkani.Rows.Clear();
            foreach (string Tkan in LstTkani)
                DgvTkani.Rows.Add(Tkan);
        }
    }
}
