using System;
using System.Data;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Truffles
{
    public partial class SearchForm : Form
    {
        frmMain main;
        DataTable search;
        Dictionary<int, int> originIndex = new Dictionary<int, int>();
        int selectedRow = 0;

        public SearchForm(frmMain main)
        {
            this.main = main;
            InitializeComponent();          
        }

        public bool Contains(string input, string Filter)
        {
            Regex tester = new Regex(Filter);
            return tester.IsMatch(input);
        }

        public void Init()
        {
            if (main.file == null) return; 
            cmbIn.Items.Clear();
            originIndex.Clear();
            for (int i = 0; i < main.file.table.Columns.Count; i++)
            {
                DataColumn lol = main.file.table.Columns[i];
                cmbIn.Items.Add(i.ToString("00") + ": " + lol.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Init();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGrid.Columns.Count > 1)
            {
                dataGrid.DataSource = null;
                Init();
            }
            try
            {
                if (main.file == null) return;
                search = new DataTable();
                search = ((DataTable)main.dataGrid.DataSource).Copy();
                search.DefaultView.RowFilter = string.Format("Convert({0}, 'System.String') like '%{1}%'", cmbIn.Text.Split(new String[] { ": " }, StringSplitOptions.None)[1], txtFor.Text);

                DataTable DisposeTable = new DataTable();
                DisposeTable = main.file.table.Clone();

                for (int i = 0; i < main.file.table.Rows.Count; i++)
                {
                    int colId = Convert.ToInt32(cmbIn.Text.Substring(0, 2));
                    DataRow row = main.file.table.Rows[i];
                    if (Contains(row[colId].ToString().ToLower(), txtFor.Text.ToLower()))
                    {
                        originIndex.Add(DisposeTable.Rows.Count, i);
                        DisposeTable.ImportRow(row);
                    }
                }

                dataGrid.DataSource = search;
                dataGrid.DoubleBuffered(true);
                dataGrid.Dock = DockStyle.Fill;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dataGrid_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (!originIndex.ContainsKey(e.RowIndex)) return;
            main.dataGrid.FirstDisplayedCell = main.dataGrid[0, originIndex[e.RowIndex]];
            selectedRow = e.RowIndex;
        }

        private void SearchForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                search.DefaultView.RowFilter = null;
                main.file.table = search;
                main.dataGrid.DataSource = search;
                main.dataGrid.FirstDisplayedCell = main.dataGrid[0, originIndex[selectedRow]];

                e.Cancel = true;
                this.Hide();
            }
            catch
            {
                e.Cancel = true;
                this.Hide();
            }
        }
    }
}