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.DevParam;
using GcDevicePc.IniParam;

namespace GcDevicePc.ConfigDlg
{
    public partial class InjValveDlg : Form
    {
        public InjValveDlg()
        {
            InitializeComponent();

            //TextBox m_textbox;
            //int channel = 0;
            //for (int i = 0; i < 6; i++)
            //{
            //    channel = i + 1;
            //    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start1", true)[0]);
            //    m_textbox.Enabled = false;
              
            //    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start2", true)[0]);
            //    m_textbox.Enabled = false;
             
            //    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start3", true)[0]);
            //    m_textbox.Enabled = false;
              
            //    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start4", true)[0]);
            //    m_textbox.Enabled = false;
             
            //    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end1", true)[0]);
            //    m_textbox.Enabled = false;

            //    //m_textbox.TextChanged += new EventHandler(textboxtext_TextChanged);
            //    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end2", true)[0]);
            //    m_textbox.Enabled = false;

            //    //m_textbox.TextChanged += new EventHandler(textboxtext_TextChanged);
            //    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end3", true)[0]);
            //    m_textbox.Enabled = false;

            //   // m_textbox.TextChanged += new EventHandler(textboxtext_TextChanged);
            //    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end4", true)[0]);
            //    m_textbox.Enabled = false;
            //}

            for(int i = 0; i < 6; i++)
            {
                disabletextboxbyid(i+1);
            }

            this.valvesw1.CheckedChanged += new EventHandler(valvesw_Changed);
            this.valvesw2.CheckedChanged += new EventHandler(valvesw_Changed);
            this.valvesw3.CheckedChanged += new EventHandler(valvesw_Changed);
            this.valvesw4.CheckedChanged += new EventHandler(valvesw_Changed);
            this.valvesw5.CheckedChanged += new EventHandler(valvesw_Changed);
            this.valvesw6.CheckedChanged += new EventHandler(valvesw_Changed);

        }

        private void textboxtext_TextChanged(object sender, EventArgs e)
        {
            TextBox mytextbox = (TextBox)sender;
            TextBox mytextbox2;
            TextBox mytextbox3;
            float one = new float();
            float two = new float();
            float three = new float();

            string[] sArray = (mytextbox.Name.ToString()).Split('_');
            string str1 = sArray[2].Substring(0, sArray[2].Length - 1);
            string str2 = sArray[2].Substring(sArray[2].Length - 1);

            int endnum =int.Parse(str2);

            mytextbox2 = (TextBox)(this.Controls.Find("valve_" + sArray[1] + "_start" + endnum.ToString(), true)[0]);
            if(endnum > 1 && endnum < 4)
            {
                 mytextbox3 = (TextBox)(this.Controls.Find("valve_" + sArray[1] + "_end" + (endnum-1).ToString(), true)[0]);  
                 try
                 {
                     three = float.Parse(mytextbox3.Text);
                 }
                 catch
                 {
                     return;
                 } 
            }

            try
            {
                two = float.Parse(mytextbox.Text);
                one = float.Parse(mytextbox2.Text);
            }
            catch
            {
                return;
            }
            
            if (mytextbox.Text != "0" && mytextbox.Text != "")
            {
              
                 if(one <= two && one != 0.0f)
                 {
                     if(endnum > 1 && endnum < 4)
                     {
                         if (str1 == "end" && (one > three))
                         {
                             mytextbox2 = (TextBox)(this.Controls.Find("valve_" + sArray[1] + "_start" + (endnum + 1).ToString(), true)[0]);
                             mytextbox2.Enabled = true;
                             mytextbox2 = (TextBox)(this.Controls.Find("valve_" + sArray[1] + "_end" + (endnum + 1).ToString(), true)[0]);
                             mytextbox2.Enabled = true;
                         }
                         else if (one <= three)
                         {
                             MessageBox.Show("请检查阀开始和结束数据的大小关系");
                         }
                     }
                     else
                     {
                         mytextbox2 = (TextBox)(this.Controls.Find("valve_" + sArray[1] + "_start" + (endnum + 1).ToString(), true)[0]);
                         mytextbox2.Enabled = true;
                         mytextbox2 = (TextBox)(this.Controls.Find("valve_" + sArray[1] + "_end" + (endnum + 1).ToString(), true)[0]);
                         mytextbox2.Enabled = true;
                     }               
                 }
                 else
                 {
                     if(two != 0.0f)
                     {
                         MessageBox.Show("请检查阀开始和结束数据的大小关系");
                         mytextbox.Text = "0";
                     }
                 }
            }     
        }

        private void textboxenable_Changed(object sender, EventArgs e)
        {
            TextBox mytextbox = (TextBox)sender;
            TextBox mytextbox2;
            string[] sArray = (mytextbox.Name.ToString()).Split('_');
            string str1 = sArray[2].Substring(0, sArray[2].Length - 1);
            string str2 = sArray[2].Substring(sArray[2].Length - 1);
            if(str1 == "start")
            {
                mytextbox2 = (TextBox)(this.Controls.Find("valve_" + sArray[1] + "_start1", true)[0]);
                mytextbox2.Enabled = true;
                mytextbox2 = (TextBox)(this.Controls.Find("valve_" + sArray[1] + "_end1", true)[0]);
                mytextbox2.Enabled = true;
            }
                

        }

        /// <summary>
        /// 归零禁用所有
        /// </summary>
        /// <param name="id"></param>
        public void disabletextboxbyid(int id)
        {
            TextBox m_textbox;
            int channel = id;
            m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start1", true)[0]);
            m_textbox.Enabled = false;
            m_textbox.Text = "0";
            m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start2", true)[0]);
            m_textbox.Enabled = false;
            m_textbox.Text = "0";
            m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start3", true)[0]);
            m_textbox.Enabled = false;
            m_textbox.Text = "0";
            m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start4", true)[0]);
            m_textbox.Enabled = false;
            m_textbox.Text = "0";
            m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end1", true)[0]);
            m_textbox.Enabled = false;
            m_textbox.Text = "0";
            m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end2", true)[0]);
            m_textbox.Enabled = false;
            m_textbox.Text = "0";
            m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end3", true)[0]);
            m_textbox.Enabled = false;
            m_textbox.Text = "0";
            m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end4", true)[0]);
            m_textbox.Enabled = false;
            m_textbox.Text = "0";
        }



        public void enabletextboxonebyone(int id)
        {
            TextBox m_textbox;
            int channel = id;
            m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start1", true)[0]);
            m_textbox.Enabled = true;
           
            m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start2", true)[0]);
            m_textbox.Enabled = true;
   
            m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start3", true)[0]);
            m_textbox.Enabled = true;
      
            m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start4", true)[0]);
            m_textbox.Enabled = true;
     
            m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end1", true)[0]);
            m_textbox.Enabled = true;
     
            m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end2", true)[0]);
            m_textbox.Enabled = true;
      
            m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end3", true)[0]);
            m_textbox.Enabled = true;
      
            m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end4", true)[0]);
            m_textbox.Enabled = true;
 
        }

        /// <summary>
        /// 选中checkbox使能输入框，否则归零禁用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void valvesw_Changed(object sender, EventArgs e)
        {
            CheckBox mycheckbox = (CheckBox)sender;
            int num = int.Parse(mycheckbox.Tag.ToString());

            if (mycheckbox.Checked)
            {
                enabletextboxonebyone(num);
            }
            else
            {
                disabletextboxbyid(num);
            }

            //if (this.valvesw1.Checked)
            //{
            //    //this.valve_1_start1.Enabled = true;
            //    //this.valve_1_end1.Enabled = true;
            //    enabletextboxonebyone(1);
            //}              
            //else
            //    disabletextboxbyid(1);

            //if (this.valvesw2.Checked)
            //{
            //    //this.valve_2_start1.Enabled = true;
            //    //this.valve_2_end1.Enabled = true;
            //    enabletextboxonebyone(2);
            //}  
            //else
            //    disabletextboxbyid(2);

            //if (this.valvesw3.Checked)
            //{
            //    //this.valve_3_start1.Enabled = true;
            //    //this.valve_3_end1.Enabled = true;
            //    enabletextboxonebyone(3);
            //}       
            //else
            //    disabletextboxbyid(3);

            //if (this.valvesw4.Checked)
            //{
            //    //this.valve_4_start1.Enabled = true;
            //    //this.valve_4_end1.Enabled = true;
            //    enabletextboxonebyone(4);
            //}    
            //else
            //    disabletextboxbyid(4);

            //if (this.valvesw5.Checked)
            //{
            //    //this.valve_5_start1.Enabled = true;
            //    //this.valve_5_end1.Enabled = true;
            //    enabletextboxonebyone(5);
            //}        
            //else
            //    disabletextboxbyid(5);

            //if (this.valvesw6.Checked)
            //{
            //    //this.valve_6_start1.Enabled = true;
            //    //this.valve_6_end1.Enabled = true;
            //    enabletextboxonebyone(6);
            //}             
            //else
            //    disabletextboxbyid(6);
        }

        public void reflashContent()
        {
            const int MAX_VALVE_NUM = 6;
            CheckBox m_checkbox;
            TextBox m_textbox;
            int channel = 0;

            globaldata.m_profileMethod.GetDevState();
            globaldata.m_profileMethod.GetInjValveAllPara();

            this.Valvenum.Text = CProfileDevice.m_DevParam.syspara.u16ValveBox.ToString();
          
            try
            {
                for (int i = 0; i < MAX_VALVE_NUM; i++)
                {                   
                    channel = i + 1;
                    m_checkbox = (CheckBox)(this.Controls.Find("valvesw" + channel.ToString(), true)[0]);
                    if (CProfileDevice.m_DevParam.valve.bHeaterKey[i] == true)
                    {
                        m_checkbox.CheckState = CheckState.Checked;
                    }
                    else
                    {
                        m_checkbox.CheckState = CheckState.Unchecked;
                    }

                    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start1", true)[0]);
                    m_textbox.Text = (CProfileDevice.m_DevParam.valve.u16ValveSetTime1[i] / 100.0f).ToString();
                    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start2", true)[0]);
                    m_textbox.Text = (CProfileDevice.m_DevParam.valve.u16ValveSetTime3[i] / 100.0f).ToString();
                    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start3", true)[0]);
                    m_textbox.Text = (CProfileDevice.m_DevParam.valve.u16ValveSetTime5[i] / 100.0f).ToString();
                    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start4", true)[0]);
                    m_textbox.Text = (CProfileDevice.m_DevParam.valve.u16ValveSetTime7[i] / 100.0f).ToString();
                    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end1", true)[0]);
                    m_textbox.Text = (CProfileDevice.m_DevParam.valve.u16ValveSetTime2[i] / 100.0f).ToString();
                    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end2", true)[0]);
                    m_textbox.Text = (CProfileDevice.m_DevParam.valve.u16ValveSetTime4[i] / 100.0f).ToString();
                    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end3", true)[0]);
                    m_textbox.Text = (CProfileDevice.m_DevParam.valve.u16ValveSetTime6[i] / 100.0f).ToString();
                    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end4", true)[0]);
                    m_textbox.Text = (CProfileDevice.m_DevParam.valve.u16ValveSetTime8[i] / 100.0f).ToString();
                }
            }catch
            {

            }

            if (CProfileDevice.m_DevParam.syspara.u16ValveOne == 1)
            {
                this.groupBox1.Enabled = true;
                CProfileDevice.m_DevParam.valve.bValveKey[0] = true;
            }
            else
            {
                this.groupBox1.Enabled = false;
            }

            if (CProfileDevice.m_DevParam.syspara.u16ValveTwo == 1)
            {
                this.groupBox2.Enabled = true;
                CProfileDevice.m_DevParam.valve.bValveKey[1] = true;
            }
            else
            {
                this.groupBox2.Enabled = false;
            }

            if (CProfileDevice.m_DevParam.syspara.u16ValveThree == 1)
            {
                this.groupBox3.Enabled = true;
                CProfileDevice.m_DevParam.valve.bValveKey[2] = true;
            }
            else
            {
                this.groupBox3.Enabled = false;
            }

            if (CProfileDevice.m_DevParam.syspara.u16ValveFour == 1)
            {
                this.groupBox4.Enabled = true;
                CProfileDevice.m_DevParam.valve.bValveKey[3] = true;
            }
            else
            {
                this.groupBox4.Enabled = false;
            }

            if (CProfileDevice.m_DevParam.syspara.u16ValveFive == 1)
            {
                this.groupBox5.Enabled = true;
                CProfileDevice.m_DevParam.valve.bValveKey[4] = true;
            }
            else
            {
                this.groupBox5.Enabled = false;
            }

            if (CProfileDevice.m_DevParam.syspara.u16ValveSix == 1)
            {
                this.groupBox6.Enabled = true;
                CProfileDevice.m_DevParam.valve.bValveKey[5] = true;
            }
            else
            {
                this.groupBox6.Enabled = false;
            }
        }

        private void TextBoxDec_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                if (e.KeyChar == '.')
                {
                    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                        e.Handled = true;
                }
                else
                    e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        public void recordCurData()
        {
            const int MAX_VALVE_NUM = 6;

            CheckBox m_checkbox;
            TextBox m_textbox;

            try
            {
                for (int i = 0; i < MAX_VALVE_NUM; i++)
                {
                    int channel = i + 1;
                    m_checkbox = (CheckBox)(this.Controls.Find("valvesw" + channel.ToString(), true)[0]);
                    CProfileDevice.m_DevParam.valve.bHeaterKey[i] = (m_checkbox.CheckState == CheckState.Checked) ? true : false;

                    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start1", true)[0]);
                    if (m_textbox.Text != "")
                        CProfileDevice.m_DevParam.valve.u16ValveSetTime1[i] = (ushort)(Convert.ToDecimal(m_textbox.Text) * 100);

                    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start2", true)[0]);
                    if (m_textbox.Text != "")
                        CProfileDevice.m_DevParam.valve.u16ValveSetTime3[i] = (ushort)(Convert.ToDecimal(m_textbox.Text) * 100);

                    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start3", true)[0]);
                    if (m_textbox.Text != "")
                        CProfileDevice.m_DevParam.valve.u16ValveSetTime5[i] = (ushort)(Convert.ToDecimal(m_textbox.Text) * 100);

                    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_start4", true)[0]);
                    if (m_textbox.Text != "")
                        CProfileDevice.m_DevParam.valve.u16ValveSetTime7[i] = (ushort)(Convert.ToDecimal(m_textbox.Text) * 100);

                    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end1", true)[0]);
                    if (m_textbox.Text != "")
                        CProfileDevice.m_DevParam.valve.u16ValveSetTime2[i] = (ushort)(Convert.ToDecimal(m_textbox.Text) * 100);

                    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end2", true)[0]);
                    if (m_textbox.Text != "")
                        CProfileDevice.m_DevParam.valve.u16ValveSetTime4[i] = (ushort)(Convert.ToDecimal(m_textbox.Text) * 100);

                    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end3", true)[0]);
                    if (m_textbox.Text != "")
                        CProfileDevice.m_DevParam.valve.u16ValveSetTime6[i] = (ushort)(Convert.ToDecimal(m_textbox.Text) * 100);

                    m_textbox = (TextBox)(this.Controls.Find("valve_" + channel.ToString() + "_end4", true)[0]);
                    if (m_textbox.Text != "")
                        CProfileDevice.m_DevParam.valve.u16ValveSetTime8[i] = (ushort)(Convert.ToDecimal(m_textbox.Text) * 100);

                }
            }
            catch
            {
                MessageBox.Show("阀数据出错！");
            }

           
        }

        private void InjValveDlg_Load(object sender, EventArgs e)
        {
            
        }
    }
}
