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

namespace Truffles
{
    public partial class ColReplace : Form
    {
        frmMain main;

        public ColReplace(frmMain frm)
        {
            InitializeComponent();

            main = frm;
        }

        private void ColReplace_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++;
            }

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

        private void button1_Click(object sender, EventArgs e)
        {
            Int32 EditCount = 0;
            DataTable serach = new DataTable();
            serach = main.file.table;
            int colId = Convert.ToInt32(comboBox1.Text.Substring(0, 2));
            for (int i = 0; i < main.file.table.Rows.Count; i++)
            {
                EditCount++;

                DataRow row = main.file.table.Rows[i];

                if (checkBox1.Checked)
                {
                    if (row[colId].ToString().StartsWith(textBox1.Text))
                    {
                        row[colId] = row[colId].ToString().Replace(textBox1.Text, textBox2.Text);
                    }
                }
                else { row[colId] = row[colId].ToString().Replace(textBox1.Text, textBox2.Text); }
            }

            main.dataGrid.DataSource = serach;

            MessageBox.Show("Edited " + EditCount + " Rows!");
        }
    }
}
