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 GcDevicePc.Common;
using GcDevicePc.GCBuffer;

namespace GcDevicePc.ConfigMethod
{
    public partial class RealColTab : Form
    {
        HWConfig hwconfig;
        GCMethodConfig methodconfig;

        public RealColTab(HWConfig hwconfig, ref GCMethodConfig methodconfig)
        {
            InitializeComponent();

            this.hwconfig = hwconfig;
            this.methodconfig = methodconfig;

            int width = this.ProUpTab.Width;
            int avgWidth = width / 5;//求出每一列的header宽度
            for (int i = 0; i < this.ProUpTab.Columns.Count; i++)
            {
                this.ProUpTab.Columns[i].Width = avgWidth;//设置每一列的宽度
            }
        }

        public void reflashContent()
        {

            string[] sStage = new string[5];
            sStage[0] = "Init";
            sStage[1] = "1st";
            sStage[2] = "2nd";
            sStage[3] = "3rd";

            try
            {
                ProUpTab.Columns[0].ReadOnly = true;
                ProUpTab[2, 0].ReadOnly = true;
            }
            catch
            {

            }

            GCMethodConfig.stRunTableVal stRunTable = new GCMethodConfig.stRunTableVal();


            this.methodconfig.GetColTableElseAllPara();


            int ProNum = this.methodconfig.col.u16ColProNum;
            OSetTemp.Text = this.methodconfig.col.fColTempSet.ToString();
            //OActTemp.Text = this.methodconfig.col.fColTempActual.ToString();
            OMAXTemp.Text = (this.methodconfig.col.fHigestTemp == 0.0f)
                ? this.hwconfig.hwconfiginfo.u16ColPTemp.ToString()
                : this.methodconfig.col.fHigestTemp.ToString();
            if (this.hwconfig.hwconfiginfo.u16ColPTemp == 0 && this.methodconfig.col.fHigestTemp == 0)
            {
                OMAXTemp.Text = "250";
            }

            OBalTime.Text = this.methodconfig.col.fBalanceTime.ToString();
            tb后运行温度.Text = this.methodconfig.col.fBehindRunTemp.ToString();
            tb后运行时间.Text = this.methodconfig.col.fBehindRunTime.ToString();
            ColProNum.Text = this.methodconfig.col.u16ColProNum.ToString();

            if (this.methodconfig.col.u16KeyCol == 1)
            {
                OvenOpen.CheckState = CheckState.Checked;
            }
            else
            {
                OvenOpen.CheckState = CheckState.Unchecked;
            }

            if (this.methodconfig.col.u16FastColdTrap == 1)
            {
                cBox快速冷陷.CheckState = CheckState.Checked;
            }
            else
            {
                cBox快速冷陷.CheckState = CheckState.Unchecked;
            }

            if (this.methodconfig.col.u16FastColdTrap == 1)
            {
                cBox启动风扇冷陷.CheckState = CheckState.Checked;
            }
            else
            {
                cBox启动风扇冷陷.CheckState = CheckState.Unchecked;
            }

            if (this.methodconfig.col.u16FastColdTrap == 1)
            {
                cBox启动制冷剂冷陷.CheckState = CheckState.Checked;
            }
            else
            {
                cBox启动制冷剂冷陷.CheckState = CheckState.Unchecked;
            }

            try
            {
                ProUpTab.Rows.Clear();
                for (ushort i = 0; i < ProNum; i++)
                {
                    int index;

                    this.methodconfig.GetColTableStageVal(ref stRunTable, i);

                    if (stRunTable.TargetTemp == 0)
                    {
                        break;
                    }
                    else
                    {
                        index = this.ProUpTab.Rows.Add();
                        if (i >= 4)
                        {
                            ProUpTab.Rows[index].Cells[0].Value = i.ToString() + "th";
                        }
                        else //if (u16Stage == 0)
                        {
                            ProUpTab.Rows[index].Cells[0].Value = sStage[i];

                        }
                        ProUpTab.Rows[index].Cells[1].Value = (stRunTable.HeatSpeed).ToString();
                        ProUpTab.Rows[index].Cells[2].Value = (stRunTable.TargetTemp / 10.0f).ToString();
                        ProUpTab.Rows[index].Cells[3].Value = (stRunTable.BalanceTime).ToString();
                    }
                }
            }
            catch
            {

            }       
        }

        private void TextBoxDec_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }
        }

        private void dgv程升表_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {


        }

        private void dgv程升表_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            e.Control.KeyPress += new KeyPressEventHandler(TextBoxDec_KeyPress);
        }

        public void recordCurData()
        {
            int ProNum = 0;
            if (ProUpTab.Rows.Count > 1)
            {
                ProNum = ProUpTab.Rows.Count - 1;
            }
            else
            {
                ProNum = ProUpTab.Rows.Count;
            }

            int oldProNum = this.methodconfig.col.u16ColProNum;

            this.methodconfig.col.fColTempSet = (String.IsNullOrEmpty(OSetTemp.Text))? 0 : float.Parse(OSetTemp.Text);
            this.methodconfig.col.fHigestTemp = (String.IsNullOrEmpty(OMAXTemp.Text)) ? 0 : float.Parse(OMAXTemp.Text);
            this.methodconfig.col.fBalanceTime = (String.IsNullOrEmpty(OBalTime.Text)) ? 0 : float.Parse(OBalTime.Text);
            this.methodconfig.col.fBehindRunTemp = (String.IsNullOrEmpty(tb后运行温度.Text)) ? 0 : float.Parse(tb后运行温度.Text);
            this.methodconfig.col.fBehindRunTime = (String.IsNullOrEmpty(tb后运行时间.Text)) ? 0 : float.Parse(tb后运行时间.Text);

            if (ProUpTab.Rows[0].Cells[3].Value != null && ProUpTab.Rows[0].Cells[2].Value != null &&
                ProUpTab.Rows[0].Cells[1].Value != null && ProUpTab.Rows[0].Cells[4].Value != null && ProUpTab.Rows.Count == 1)
            {
                ColProNum.Text = (ProUpTab.Rows.Count).ToString();
            }

            this.methodconfig.col.u16ColProNum = (String.IsNullOrEmpty(ColProNum.Text)) ? (ushort)0 : ushort.Parse(ColProNum.Text);
            
            this.methodconfig.col.u16KeyCol = (OvenOpen.CheckState == CheckState.Checked )?(ushort)1:(ushort)0;
            this.methodconfig.col.u16FastColdTrap = (cBox快速冷陷.CheckState == CheckState.Checked) ? (ushort)1 : (ushort)0;
            this.methodconfig.col.u16FastColdTrap = (cBox启动风扇冷陷.CheckState == CheckState.Checked) ? (ushort)1 : (ushort)0;
            this.methodconfig.col.u16FastColdTrap = (cBox启动制冷剂冷陷.CheckState == CheckState.Checked) ? (ushort)1 : (ushort)0;


            GCMethodConfig.stRunTableVal stRunTable = new GCMethodConfig.stRunTableVal();
            this.methodconfig.EraseColTable(oldProNum);

            for (ushort i = 0; i < ProNum; i++)
            {

                if (ProUpTab.Rows[i].Cells[0].Value == null || ProUpTab.Rows[i].Cells[1].Value == null || ProUpTab.Rows[i].Cells[2].Value == null || ProUpTab.Rows[i].Cells[3].Value == null)
                {
                    break;
                }
                else
                {
                    stRunTable.HeatSpeed = (float)Convert.ToDecimal(ProUpTab.Rows[i].Cells[1].Value.ToString());
                    stRunTable.TargetTemp = (float)Convert.ToDecimal(ProUpTab.Rows[i].Cells[2].Value.ToString()) * 10;
                    stRunTable.BalanceTime = (float)Convert.ToDecimal(ProUpTab.Rows[i].Cells[3].Value.ToString());
                }
                this.methodconfig.SetColTableStageVal(stRunTable, i);
            }
        }

        private void OSetTemp_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(OSetTemp.Text))
            {
                try
                {
                    if (float.Parse(OSetTemp.Text) > 0.0f)
                    {
                        ProUpTab.Rows[0].Cells[2].Value = OSetTemp.Text;
                        ProUpTab.Rows[0].Cells[1].Value = "0";

                        if (ProUpTab.Rows[0].Cells[3].Value == null)
                        {
                            ProUpTab.Rows[0].Cells[3].Value = "5";
                    }
                        
                    }

                    if (float.Parse(OSetTemp.Text) > float.Parse(OMAXTemp.Text))
                    {
                        MessageBox.Show("设置温度不能大于最高温度");
                        OSetTemp.Text = "0";
                    }else if (float.Parse(OSetTemp.Text) > 400.0f)
                    {
                        MessageBox.Show("设置温度需要小于400");
                        OSetTemp.Text = "0";
                    }


                   // int line = ProUpTab.Rows.Count;

                    //if (line == 1)
                    //{
                    //    ProUpTab.Rows.Add();
                    //    ProUpTab.Rows[1].Cells[0].Value = "1st";
                    //}
                }
                catch (Exception ex)
                {
                    Log.Error(ex.Message);
                }

            }
            
            if(OSetTemp.Text != "")
            {
                 ProUpTab.Rows[0].Cells[2].Value = OSetTemp.Text;
                 ProUpTab.Rows[0].Cells[1].Value = "0";
            }
        }

        private void ProUpTab_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            string[] sStage = new string[5];
            sStage[0] = "Init";
            sStage[1] = "1st";
            sStage[2] = "2nd";
            sStage[3] = "3rd";
            int line = ProUpTab.Rows.Count;
            if (ProUpTab.Rows.Count >= 1 && ProUpTab.Rows.Count < 4)
            {
                ProUpTab.Rows[line - 1].Cells[0].Value = sStage[line - 1];
                ColProNum.Text = (ProUpTab.Rows.Count - 1).ToString();
            }
            if (ProUpTab.Rows.Count >= 4)
            {
                ProUpTab.Rows[line - 1].Cells[0].Value = (line - 1).ToString() + "st";
                ColProNum.Text = (ProUpTab.Rows.Count - 1).ToString();
            }       
        }

        private void ProUpTab_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                string[] sStage = new string[5];
                sStage[0] = "Init";
                sStage[1] = "1st";
                sStage[2] = "2nd";
                sStage[3] = "3rd";

                if (e.RowIndex < 4)
                {
                    if (e.RowIndex == 0)
                    {
                        if (e.ColumnIndex == 0)
                            e.Value = "Init";
                        if (e.ColumnIndex == 1)
                            e.Value = "N/A";
                    }

                    if (e.RowIndex == 1 && e.ColumnIndex == 0)
                        e.Value = "1st";
                    if (e.RowIndex == 2 && e.ColumnIndex == 0)
                        e.Value = "2nd";
                    if (e.RowIndex == 3 && e.ColumnIndex == 0)
                        e.Value = "3rd";
                }
                else
                {
                    if (e.ColumnIndex == 0)
                    {
                        e.Value = e.RowIndex.ToString() + "st";
                    }
                }
            }
        }

        private void ProUpTab_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int line = ProUpTab.Rows.Count;
            int i = 0;
            float run_time = 0.0f;

            for (i = 0; i < line; i++)
            {
                try
                {
                    if (ProUpTab.Rows[i].Cells[3].Value != null && ProUpTab.Rows[i].Cells[2].Value != null && ProUpTab.Rows[i].Cells[1].Value != null)
                    {
                        if (i == 0)
                        {
                            ProUpTab.Rows[i].Cells[4].Value = (float)Convert.ToDecimal(ProUpTab.Rows[i].Cells[3].Value.ToString());
                            run_time = (float)Convert.ToDecimal(ProUpTab.Rows[i].Cells[4].Value.ToString());
                            continue;
                        }


                        if (i > 0 && Convert.ToSingle(ProUpTab.Rows[i].Cells[1].Value) > 0.0f && (float)Convert.ToSingle(ProUpTab.Rows[i].Cells[2].Value) > (float)Convert.ToSingle(ProUpTab.Rows[i - 1].Cells[2].Value))
                        {
                            ProUpTab.Rows[i].Cells[4].Value = ((float)Convert.ToDecimal(ProUpTab.Rows[i].Cells[2].Value.ToString()) - (float)Convert.ToDecimal(ProUpTab.Rows[i - 1].Cells[2].Value.ToString())) / (float)Convert.ToDecimal(ProUpTab.Rows[i].Cells[1].Value.ToString()) + (float)Convert.ToDecimal(ProUpTab.Rows[i].Cells[3].Value.ToString());
                        }
                        else
                        {
                            if (Convert.ToSingle(ProUpTab.Rows[i].Cells[1].Value) <= 0.0f)
                            {
                                ProUpTab.Rows.RemoveAt(i);
                            }
                            else
                            {
                                if ((float)Convert.ToDecimal(ProUpTab.Rows[i].Cells[1].Value.ToString()) <= 0.0f)
                                {
                                    ProUpTab.Rows[i].Cells[1].Value = "10";
                                }

                                if ((float)Convert.ToDecimal(ProUpTab.Rows[i].Cells[2].Value.ToString()) < (float)Convert.ToDecimal(ProUpTab.Rows[i - 1].Cells[2].Value.ToString()))
                                {
                                    ProUpTab.Rows[i].Cells[2].Value = ProUpTab.Rows[i - 1].Cells[2].Value;
                                }
                            }

                        }
                    }
                    else
                    {
                        //  MessageBox.Show("请检查程升表中的数据是否正确！");
                            }

                }
                catch
                {
                    MessageBox.Show("请检查程升表中的数据是否正确！");
                }

                try
                {
                    if (ProUpTab.Rows[i].Cells[4].Value != null)
                        run_time += (float)Convert.ToDecimal(ProUpTab.Rows[i].Cells[4].Value.ToString());
                }
                catch
                {
                    MessageBox.Show("请检查程升表中的数据是否正确！");
                }
            }

           

            this.label12.Text = run_time.ToString("0.00") + " min";
        }

        private void ProUpTab_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    //若行已是选中状态就不再进行设置
                    if (ProUpTab.Rows[e.RowIndex].Selected == false)
                    {
                        ProUpTab.ClearSelection();
                        ProUpTab.Rows[e.RowIndex].Selected = true;
                    }
                    //只选中一行时设置活动单元格
                    if (ProUpTab.SelectedRows.Count == 1)
                    {
                        ProUpTab.CurrentCell = ProUpTab.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    }
                    //弹出操作菜单
                    delMenu.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }

        private void delMenudel_Click(object sender, EventArgs e)
        {
            int index = ProUpTab.SelectedRows[0].Index;
            DataGridViewRow row = ProUpTab.Rows[index];

            if (index > 0)
            {
                try
                {
                    if (ProUpTab.Rows[index].Cells[3].Value != null || ProUpTab.Rows[index].Cells[2].Value != null || ProUpTab.Rows[index].Cells[1].Value != null)
                    {
                        ProUpTab.Rows.Remove(row);
                    }

                    ProUpTab_CellValueChanged(sender, (DataGridViewCellEventArgs)e);


                }
                catch
                {

                }

            }
            else
            {
                MessageBox.Show("删除失败，不能删除第一行！");
            }

            string[] sStage = new string[5];
            sStage[0] = "Init";
            sStage[1] = "1st";
            sStage[2] = "2nd";
            sStage[3] = "3rd";

            for (int i = 1; i <= ProUpTab.Rows.Count; i++)
            {
                if (i >= 4)
                {
                    ProUpTab.Rows[i - 1].Cells[0].Value = (i - 1).ToString() + "st";
                }
                else
                {
                    ProUpTab.Rows[i - 1].Cells[0].Value = sStage[i - 1];
                }
            }

            ColProNum.Text = (ProUpTab.Rows.Count - 1).ToString();
            
                
        }

        private void ProUpTab_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            //判断是否可以编辑  
            if ((e.ColumnIndex == 1 && e.RowIndex == 0) || (e.ColumnIndex == 2 && e.RowIndex == 0))
            {
                //编辑不能  
                e.Cancel = true;
            }  
        }

        private void OMAXTemp_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.OMAXTemp.Text))
            {
                if (globaldata.m_hwconfig.hwconfiginfo.u16ColPTemp < Convert.ToInt32(this.OMAXTemp.Text))
                {
                    MessageBox.Show("最高温度不能大于保护温度");
                    this.OMAXTemp.Text = "0";
                }
            }
        }
    }
}
