using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using GcDevicePc.IniParam;

namespace GcDevicePc
{
    public delegate void AUXTool(int flag, ushort futime, ushort hvtime, ushort ffdeley);
    //public delegate void AUXTool(int flag, ushort futime, ushort hvtime);
    public delegate void StopForUpdateFirm();
    public delegate void UpdateFirmCMD();
    public delegate void SetPos(int ipos, string vinfo);
    public delegate void TCD_SW(ushort port);
    public delegate void TCD_SetDAC(ushort port, ushort val);

    public delegate void HAir_SW(int val);
    public delegate void Adjust_SW(int val);
    public partial class auxtool : Form
    {
        //private string HmiIP = null;
        //private ushort HmiPort = 911;
        private ArrayList SearchRet = new ArrayList();

        public auxtool()
        {
            InitializeComponent();
            fidfire.Start();
        }

        public auxtool(ushort time1, ushort time2, ushort time3)
        {
            InitializeComponent();

            this.fireuptime.Text = time1.ToString();
            this.hvalvetime.Text = time2.ToString();

            this.firstfiredelay.Text = time3.ToString();
            fidfire.Start();

        }

        public void setfireuptime(ushort time1, ushort time2, ushort time3)
        {
            this.fireuptime.Text = time1.ToString();
            this.hvalvetime.Text = time2.ToString();
            this.firstfiredelay.Text = time3.ToString();

        }

        private void hvalvetime_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }
        }

        private void fireuptime_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }
        }

        public event AUXTool auxtoolforfireup;
        private void fireupbtn_Click(object sender, EventArgs e)
        {
            //if(String.IsNullOrEmpty(fireuptime.Text) || String.IsNullOrEmpty(hvalvetime.Text))
            //{
            //    MessageBox.Show("点火时间 和 氢气阀时间不能为空！！！");
            //}
            //else
            //{
            //    ushort time1 = ushort.Parse(fireuptime.Text);
            //    ushort time2 = ushort.Parse(hvalvetime.Text);

            //    if(time1 >= time2)
            //    {
            //        auxtoolforfireup(0, time1, time2);
            //    }else
            //    {
            //        MessageBox.Show("点火时间需大于等于氢气阀时间！！！");
            //    }  
            //}

            //if (min >= 30)
            try
            {
                if (String.IsNullOrEmpty(fireuptime.Text) || String.IsNullOrEmpty(hvalvetime.Text) || string.IsNullOrEmpty(firstfiredelay.Text))
                {
                    MessageBox.Show("点火时间 第一次点火延时 和 氢气阀时间不能为空！！！");
                }
                else
                {

                    ushort time1 = ushort.Parse(fireuptime.Text);
                    ushort time2 = ushort.Parse(hvalvetime.Text);
                    ushort time3 = ushort.Parse(firstfiredelay.Text);
                    if (time1 >= time2)
                    {
                        auxtoolforfireup(0, time1, time2, time3);
                        this.fireupbtnI.Enabled = false;

                    }
                    else
                    {
                        MessageBox.Show("点火时间需大于等于氢气阀时间！！！");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //else
            //{
            //    MessageBox.Show("请在三十分钟后在点火！");
            //}
        }

        private void auxtool_Load(object sender, EventArgs e)
        {

        }

        public event HAir_SW hairsw;
        private void hair_h1_Click(object sender, EventArgs e)
        {
            hairsw(1);
        }

        private void hair_a1_Click(object sender, EventArgs e)
        {
            hairsw(2);
        }

        private void hair_h2_Click(object sender, EventArgs e)
        {
            hairsw(3);
        }

        private void hair_a2_Click(object sender, EventArgs e)
        {
            hairsw(4);
        }

        public event TCD_SetDAC gctcd_setdac;
        public event TCD_SW gctcd_sw;
        private void tcdset1_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(tcddac1.Text))
            {
                MessageBox.Show("设置值不能为空！！！");
            }
            else
            {
                ushort val = ushort.Parse(tcddac1.Text);


                if (val >= 0 && val < 150)
                {
                    gctcd_setdac(0, val);
                }
                else
                {
                    MessageBox.Show("超过设置值(0~150)！！！");
                }
            }
        }

        private void tcdset2_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(tcddac2.Text))
            {
                MessageBox.Show("设置值不能为空！！！");
            }
            else
            {
                ushort val = ushort.Parse(tcddac2.Text);


                if (val >= 0 && val < 150)
                {
                    gctcd_setdac(1, val);
                }
                else
                {
                    MessageBox.Show("超过设置值(0~150)！！！");
                }
            }
        }

        private void tcdset3_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(tcddac3.Text))
            {
                MessageBox.Show("设置值不能为空！！！");
            }
            else
            {
                ushort val = ushort.Parse(tcddac3.Text);


                if (val >= 0 && val < 150)
                {
                    gctcd_setdac(2, val);
                }
                else
                {
                    MessageBox.Show("超过设置值(0~150)！！！");
                }
            }
        }

        private void tcd1_sw_Click(object sender, EventArgs e)
        {

            if (String.IsNullOrEmpty(tcddac1.Text))
            {
                MessageBox.Show("设置值不能为空！！！");
            }
            else
            {
                ushort val = ushort.Parse(tcddac1.Text);


                if (val >= 0 && val < 150)
                {
                    gctcd_setdac(0, val);
                }
                else
                {
                    MessageBox.Show("超过设置值(0~150)！！！");
                }
            }

            String tcd1_flag = tcd1_sw.Text;
            string[] sArray = tcd1_flag.Split(new char[2] { '(', ')' });
            if (sArray[1] == "关")
            {
                tcd1_sw.Text = "前TCD(开)";
            }
            else if (sArray[1] == "开")
            {
                tcd1_sw.Text = "前TCD(关)";
            }
            Console.WriteLine(sArray[1]);
            gctcd_sw(0);
        }

        private void tcd2_sw_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(tcddac2.Text))
            {
                MessageBox.Show("设置值不能为空！！！");
            }
            else
            {
                ushort val = ushort.Parse(tcddac2.Text);


                if (val >= 0 && val < 150)
                {
                    gctcd_setdac(1, val);
                }
                else
                {
                    MessageBox.Show("超过设置值(0~150)！！！");
                }
            }

            String tcd2_flag = tcd2_sw.Text;
            string[] sArray = tcd2_flag.Split(new char[2] { '(', ')' });
            if (sArray[1] == "关")
            {
                tcd2_sw.Text = "中TCD(开)";
            }
            else if (sArray[1] == "开")
            {
                tcd2_sw.Text = "中TCD(关)";
            }
            Console.WriteLine(sArray[1]);
            gctcd_sw(1);
        }

        private void tcd3_sw_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(tcddac3.Text))
            {
                MessageBox.Show("设置值不能为空！！！");
            }
            else
            {
                ushort val = ushort.Parse(tcddac3.Text);


                if (val >= 0 && val < 150)
                {
                    gctcd_setdac(2, val);
                }
                else
                {
                    MessageBox.Show("超过设置值(0~150)！！！");
                }
            }

            String tcd3_flag = tcd3_sw.Text;
            string[] sArray = tcd3_flag.Split(new char[2] { '(', ')' });
            if (sArray[1] == "关")
            {
                tcd3_sw.Text = "后TCD(开)";
            }
            else if (sArray[1] == "开")
            {
                tcd3_sw.Text = "后TCD(关)";
            }
            Console.WriteLine(sArray[1]);
            gctcd_sw(2);
        }

        private void fireupbtnI_Click(object sender, EventArgs e)
        {
            //if (min >= 30)
            {
                if (String.IsNullOrEmpty(fireuptime.Text) || String.IsNullOrEmpty(hvalvetime.Text))
                {
                    MessageBox.Show("点火时间 和 氢气阀时间不能为空！！！");
                }
                else
                {
                    ushort time1 = ushort.Parse(fireuptime.Text);
                    ushort time2 = ushort.Parse(hvalvetime.Text);
                    ushort time3 = ushort.Parse(firstfiredelay.Text);

                    if (time1 >= time2)
                    {
                        auxtoolforfireup(1, time1, time2, time3);
                        this.fireupbtn.Enabled = false;
                    }
                    else
                    {
                        MessageBox.Show("点火时间需大于等于氢气阀时间！！！");
                    }
                }
            }
            //else
            //{
            //    MessageBox.Show("请在三十分钟后在点火！");
            //}
        }

        private void fidfire_Tick(object sender, EventArgs e)
        {
            if (CProfileDevice.m_DevParam.syspara.u16DetFrontType == 1)
            {
                if (globaldata.m_dpbuffer.ShowList.showDet.iDetStatue == 1)
                {
                    this.fireupbtn.Enabled = false;
                }
                else
                {
                    this.fireupbtn.Enabled = true;

                }
            }

            if (CProfileDevice.m_DevParam.syspara.u16DetInterType == 1)
            {
                if (globaldata.m_dpbuffer.ShowList.showDet.fDetStatue == 1)
                {
                    this.fireupbtnI.Enabled = false;
                }
                else
                {
                    this.fireupbtnI.Enabled = true;

                }
            }

            this.tcd1_sw.Enabled = this.tcd2_sw.Enabled = this.tcd3_sw.Enabled = globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.system_Statue;
        }

        private void auxtool_FormClosed(object sender, FormClosedEventArgs e)
        {
            fidfire.Stop();
        }

        public event Adjust_SW adsw;
        private void Button1_Click(object sender, EventArgs e)
        {
            adsw(1);
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            adsw(0);
        }

        private double lastnum;
        private void Text_KeyDown(object sender, KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty((sender as TextBox).Text))
            {
                lastnum = Convert.ToDouble((sender as TextBox).Text);
            }
            else
            {
                lastnum = 0;
            }
        }

        private void Text_KeyUp(object sender, KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty((sender as TextBox).Text))
            {
                double nextnum = Convert.ToDouble((sender as TextBox).Text);
                if (nextnum < 0)
                {
                    MessageBox.Show("延时时间设置不能小于0");
                    (sender as TextBox).Text = lastnum.ToString();
                }
                if (nextnum > 20)
                {
                    MessageBox.Show("延时时间设置不能大于20");
                    (sender as TextBox).Text = lastnum.ToString();
                }
            }
        }

        //private void fireupbtnb_Click(object sender, EventArgs e)
        //{
        //    if (String.IsNullOrEmpty(fireuptime.Text) || String.IsNullOrEmpty(hvalvetime.Text))
        //    {
        //        MessageBox.Show("点火时间 和 氢气阀时间不能为空！！！");
        //    }
        //    else
        //    {
        //        ushort time1 = ushort.Parse(fireuptime.Text);
        //        ushort time2 = ushort.Parse(hvalvetime.Text);
        //        ushort time3 = ushort.Parse(firstfiredelay.Text);

        //        if (time1 >= time2)
        //        {
        //            auxtoolforfireup(2, time1, time2, time3);
        //        }
        //        else
        //        {
        //            MessageBox.Show("点火时间需大于等于氢气阀时间！！！");
        //        }
        //    }
        //}
    }
}
