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

namespace Truffles
{
    public partial class ColumnFilter : Form
    {
        frmMain main;
        public ColumnFilter(frmMain frm)
        {
            InitializeComponent();

            main = frm;
        }

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

        private void ColumnFilter_Load(object sender, EventArgs e)
        {
            if (main.file == null) return;
            int columnCount = 0;
            List<string> columnsWithID = new List<string>();
            foreach (var column in main.file.table.Columns)
            {
                columnsWithID.Add(columnCount.ToString("00") + ": " + column);
                columnCount++;
            }

            cmbIn.Items.AddRange(columnsWithID.ToArray());
            cmbIn.SelectedIndex = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            main.file.table.DefaultView.RowFilter = null;

            if (!checkBox1.Checked)
            {
                main.file.table.DefaultView.RowFilter = string.Format("Convert({0}, 'System.String') like '%{1}%'", cmbIn.SelectedItem.ToString().Split(new String[] { ": " }, StringSplitOptions.None)[1], txtFor.Text);
            }
            else
            {
                main.file.table.DefaultView.RowFilter = string.Format("Convert({0}, 'System.String') like '{1}*'", cmbIn.SelectedItem.ToString().Split(new String[] { ": " }, StringSplitOptions.None)[1], txtFor.Text);
            }

            Close();
        }
    }
}
