using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace Truffles
{
    public partial class ColMultiplier : Form
    {
        frmMain mainfrm;
        public ColMultiplier(frmMain form)
        {
            mainfrm = form;
            InitializeComponent();
        }

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

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

        private void btnGo_Click(object sender, EventArgs e)
        {
            int colIndex = mainfrm.file.getColIndex(comboBox1.Items[comboBox1.SelectedIndex].ToString());
            if (colIndex < 0) return;
            double factor = 0;
            try
            {
                factor = double.Parse(txtFactor.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            string type = mainfrm.file.table.Columns[colIndex].DataType.ToString();
            for (int i = 0; i < mainfrm.file.table.Rows.Count; i++)
            {
                try
                {
                    switch (type)
                    {
                        case "System.UInt16":
                            mainfrm.file.table.Rows[i][colIndex] = (UInt16)((UInt16)mainfrm.file.table.Rows[i][colIndex] * factor);
                            break;
                        case "System.UInt32":
                            mainfrm.file.table.Rows[i][colIndex] = (UInt32)((UInt32)mainfrm.file.table.Rows[i][colIndex] * factor);
                            break;
                        case "System.SByte":
                            mainfrm.file.table.Rows[i][colIndex] = (SByte)((SByte)mainfrm.file.table.Rows[i][colIndex] * factor);
                            break;
                        case "System.Byte":
                            mainfrm.file.table.Rows[i][colIndex] = (Byte)((Byte)mainfrm.file.table.Rows[i][colIndex] * factor);
                            break;
                        default:
                            mainfrm.file.table.Rows[i][colIndex] = (int)((int)mainfrm.file.table.Rows[i][colIndex] * factor);
                            break;
                    }
                }
                catch (Exception ex) { Debug.WriteLine(ex.Message); } //wrong item conv
            }
            this.Close();
            mainfrm.SQLStatus.Text = "Column has been multiplied with " + factor.ToString();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            groupBox2.Enabled = !radioButton1.Checked;
            groupBox1.Enabled = radioButton1.Checked;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            groupBox2.Enabled = radioButton2.Checked;
            groupBox1.Enabled = !radioButton2.Checked;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int colIndex = mainfrm.file.getColIndex(comboBox1.Items[comboBox1.SelectedIndex].ToString());
            if (colIndex < 0) return;
            for (int i = 0; i < mainfrm.file.table.Rows.Count; i++)
            {
                try
                {
                    mainfrm.file.table.Rows[i][colIndex] = txtBulk.Text;
                }
                catch { }
            }
            this.Close();
            mainfrm.SQLStatus.Text = colIndex.ToString() + " set to \"" + txtBulk.Text + "\".";
        }
    }
}