using System;
using System.Collections.Generic;
using System.Windows.Forms;
using CRVM.Entity;
using System.Text.RegularExpressions;
using System.IO;
using CRVM.CIDExcuter;

namespace CRVM
{
    public partial class SetAlarmForm : Form
    {
        public EntityAlarmSpeed[][] eass;
        bool dataChanged = false;
        public SetAlarmForm()
        {
            InitializeComponent();
        }

        private void Parameterforalarm_new_Load(object sender, EventArgs e)
        {
            dataGridView1.Columns[0].ValueType = typeof(Int32);
            eass = new EntityAlarmSpeed[SysParam.Instance.channelCount][];
            comboBox1.SelectedIndex = 0;
            dataChanged = false;
            SetDgvSort();
        }
        private void SetDgvSort()
        {
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
        }
        private void InitialDisplay()
        {
            dataGridView1.Rows.Clear();

            EntityAlarmSpeed[] aat = SysParam.Instance.channel[comboBox1.SelectedIndex].alarmConfig;
            textBox1.Text = SysParam.Instance.channel[comboBox1.SelectedIndex].alarmTimes_l.ToString();
            textBox2.Text = SysParam.Instance.channel[comboBox1.SelectedIndex].alarmTimes_m.ToString();
            textBox3.Text = SysParam.Instance.channel[comboBox1.SelectedIndex].alarmTimes_h.ToString();

            for (int i = 0; i < aat.Length; i++)
            {
                if (aat[i] != null)
                {
                    dataGridView1.Rows.Add(1);
                    dataGridView1.Rows[i].Cells[0].Value = aat[i].alarmSpeed;
                    dataGridView1.Rows[i].Cells[1].Value = aat[i].lowPassL;
                    dataGridView1.Rows[i].Cells[2].Value = aat[i].lowPassU;
                    dataGridView1.Rows[i].Cells[3].Value = aat[i].lowAlarmValue;
                    dataGridView1.Rows[i].Cells[4].Value = aat[i].lowDefectValue;
                    dataGridView1.Rows[i].Cells[5].Value = aat[i].midPassL;
                    dataGridView1.Rows[i].Cells[6].Value = aat[i].midPassU;
                    dataGridView1.Rows[i].Cells[7].Value = aat[i].midAlarmValue;
                    dataGridView1.Rows[i].Cells[8].Value = aat[i].midDefectValue;
                    dataGridView1.Rows[i].Cells[9].Value = aat[i].highPassL;
                    dataGridView1.Rows[i].Cells[10].Value = aat[i].highPassU;
                    dataGridView1.Rows[i].Cells[11].Value = aat[i].highAlarmValue;
                    dataGridView1.Rows[i].Cells[12].Value = aat[i].highDefectValue;
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Add();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count <= 0)
            {
                MessageBox.Show("请选择要删除的行！");
                return;
            }
            else
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
            }
        }



        private void btnClose_Click(object sender, EventArgs e)
        {
            //RefreshOneChannelData();
            if (dataChanged)
            {
                ConfigHelper.GetInstance("").SaveConfig(Application.StartupPath);
                if (!Directory.Exists(Application.StartupPath + "\\AlarmConfig\\"))
                {
                    Directory.CreateDirectory(Application.StartupPath + "\\AlarmConfig\\");
                }
                string path = Application.StartupPath + "\\AlarmConfig\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".config";
                ConfigHelper.GetInstance("").SaveAlarmConfig(path);
                ConfigHelper.GetInstance("").SaveConfig(Application.StartupPath + "\\AppSetting.config");
            }
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitialDisplay();
        }

        private void btnStore_Click(object sender, EventArgs e)
        {
            dataGridView1.EndEdit();
            RefreshOneChannelData();
        }

        private void RefreshOneChannelData()
        {
            dataGridView1.Sort(dataGridView1.Columns["Speed"], System.ComponentModel.ListSortDirection.Ascending);
            IList<EntityAlarmSpeed> ll = new List<EntityAlarmSpeed>();
            EntityAlarmSpeed[] ieass = new EntityAlarmSpeed[dataGridView1.Rows.Count];
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                EntityAlarmSpeed eas = new EntityAlarmSpeed();
                eas.alarmSpeed = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value);
                eas.lowPassL = Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value);
                eas.lowPassU = Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
                eas.lowAlarmValue = Convert.ToSingle(dataGridView1.Rows[i].Cells[3].Value);
                eas.lowDefectValue = Convert.ToSingle(dataGridView1.Rows[i].Cells[4].Value);
                if (eas.lowAlarmValue > eas.lowDefectValue)
                {
                    MessageBox.Show("第" + (i + 1) + "行！\n\r不允许中频报警值低于中频缺陷值！\n\r请重新设定！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                eas.midPassL = Convert.ToInt32(dataGridView1.Rows[i].Cells[5].Value);
                eas.midPassU = Convert.ToInt32(dataGridView1.Rows[i].Cells[6].Value);
                eas.midAlarmValue = Convert.ToSingle(dataGridView1.Rows[i].Cells[7].Value);
                eas.midDefectValue = Convert.ToSingle(dataGridView1.Rows[i].Cells[8].Value);
                if (eas.midAlarmValue > eas.midDefectValue)
                {
                    MessageBox.Show("第" + (i + 1) + "行！\n\r不允许中频报警值低于中频缺陷值！\n\r请重新设定！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                eas.highPassL = Convert.ToInt32(dataGridView1.Rows[i].Cells[9].Value);
                eas.highPassU = Convert.ToInt32(dataGridView1.Rows[i].Cells[10].Value);
                eas.highAlarmValue = Convert.ToSingle(dataGridView1.Rows[i].Cells[11].Value);
                eas.highDefectValue = Convert.ToSingle(dataGridView1.Rows[i].Cells[12].Value);
                if (eas.highAlarmValue > eas.highDefectValue)
                {
                    MessageBox.Show("第" + (i + 1) + "行！\n\r不允许中频报警值低于中频缺陷值！\n\r请重新设定！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                ieass[i] = eas;
                ll.Add(eas);
            }
            eass[comboBox1.SelectedIndex] = ieass;

            ConfigHelper.GetInstance("").SaveBaseData(comboBox1.SelectedIndex, ll);
            DbHelper.GetInstance("").UpdateAlarmParamByStandNo(ll, comboBox1.SelectedIndex + 1);//更新数据库
            SysParam.Instance.channel[comboBox1.SelectedIndex].alarmConfig = ieass;

            string pattern = @"^[1-9]\d*$";
            if (Regex.IsMatch(textBox1.Text, pattern))
            {
                SysParam.Instance.channel[comboBox1.SelectedIndex].alarmTimes_l = Convert.ToInt32(textBox1.Text.Trim());
            }
            else
            {
                textBox1.Text = SysParam.Instance.channel[comboBox1.SelectedIndex].alarmTimes_l.ToString();
                MessageBox.Show("参数：低频报警触发次数，必须为正整数！");
                return;
            }

            if (Regex.IsMatch(textBox2.Text, pattern))
            {
                SysParam.Instance.channel[comboBox1.SelectedIndex].alarmTimes_m = Convert.ToInt32(textBox2.Text.Trim());
            }
            else
            {
                textBox2.Text = SysParam.Instance.channel[comboBox1.SelectedIndex].alarmTimes_m.ToString();
                MessageBox.Show("参数：中频报警触发次数，必须为正整数！");
                return;
            }

            if (Regex.IsMatch(textBox3.Text, pattern))
            {
                SysParam.Instance.channel[comboBox1.SelectedIndex].alarmTimes_h = Convert.ToInt32(textBox3.Text.Trim());
            }
            else
            {
                textBox3.Text = SysParam.Instance.channel[comboBox1.SelectedIndex].alarmTimes_h.ToString();
                MessageBox.Show("参数：高频报警触发次数，必须为正整数！");
                return;
            }
            DbHelper.GetInstance("").UpdateAlarmTimes(comboBox1.SelectedIndex + 1,
                SysParam.Instance.channel[comboBox1.SelectedIndex].alarmTimes_l,
                SysParam.Instance.channel[comboBox1.SelectedIndex].alarmTimes_m,
                SysParam.Instance.channel[comboBox1.SelectedIndex].alarmTimes_h);
            dataChanged = true;
        }

        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                DataGridView grid = (DataGridView)sender;
                grid.Rows[e.RowIndex].ErrorText = "";
                if (e.ColumnIndex == 3 || e.ColumnIndex == 4 || e.ColumnIndex == 7 || e.ColumnIndex == 8 || e.ColumnIndex == 11 || e.ColumnIndex == 12)
                {
                    float newFloat = 0;
                    if (!float.TryParse(e.FormattedValue.ToString(), out newFloat))
                    {
                        e.Cancel = true;
                        grid.Rows[e.RowIndex].ErrorText = "请输入一个数字!";
                        MessageBox.Show("输入的值不是一个数字，请重新输入!");
                        return;
                    }
                }
                else
                {
                    Int32 newInt32 = 0;
                    if (!Int32.TryParse(e.FormattedValue.ToString(), out newInt32))
                    {
                        e.Cancel = true;
                        grid.Rows[e.RowIndex].ErrorText = "请输入一个正整数!";
                        MessageBox.Show("输入的值不是一个正整数，请重新输入!");
                        return;
                    }
                }
            }
        }

        private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (this.dataGridView1.IsCurrentCellDirty)
            {
                this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

    }
}
