using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;

namespace UniversalEditor
{
    public partial class rowReplace : XtraForm
    {
        frmMain mainfrm;
        public rowReplace(frmMain form)
        {
            mainfrm = form;
            InitializeComponent();
        }

        private void ColMultiplier_Load(object sender, EventArgs e)
        {
            if (mainfrm.file == null)
            {
                this.Close();
                return;
            }
            init();
        }

        public void init()
        {
            if (mainfrm.file.table == null) return;
            comboBox1.Properties.Items.Clear();
            cmbSearcher.Properties.Items.Clear();
            for (int i = 0; i < mainfrm.file.table.Columns.Count; i++)
            {
                comboBox1.Properties.Items.Add(mainfrm.file.table.Columns[i].ColumnName);
                cmbSearcher.Properties.Items.Add(mainfrm.file.table.Columns[i].ColumnName);
            }
            comboBox1.SelectedIndex = 0;
            cmbSearcher.SelectedIndex = 0;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            int colIndexToChange = mainfrm.file.getColIndex(comboBox1.Properties.Items[comboBox1.SelectedIndex].ToString());
            int colIndexSearch = mainfrm.file.getColIndex(cmbSearcher.Properties.Items[cmbSearcher.SelectedIndex].ToString());
            if (colIndexToChange < 0 || colIndexSearch < 0) return;
            int changed = 0;
            for (int i = 0; i < txtForItems.Lines.Length; i++)
            {
                int rowIndex = mainfrm.file.GetRowByIndex(colIndexSearch, txtForItems.Lines[i].ToLower().Replace(" ", ""));
                if (rowIndex >= 0)
                {
                    mainfrm.file.table.Rows[rowIndex][colIndexToChange] = txtValue.Text;
                    changed++;
                }
            }
            string t = mainfrm.FileTabs.SelectedTabPage.Text;
            string n = t.Replace("*", String.Empty);
            mainfrm.FileTabs.SelectedTabPage.Text = n;
            mainfrm.FileTabs.SelectedTabPage.Text = mainfrm.FileTabs.SelectedTabPage.Text + "*";
            mainfrm.SQLStatus.Caption = changed.ToString() + " value(s) changed to " + txtValue.Text;
        }


    }
}
