using LLAirtightApi;
using Sunny.UI;
using System;
using System.Linq;
using System.Windows.Forms;

namespace SLZ_4
{
    public partial class Electricity : Form
    {
        public CONFIG_PARAM configParam;
        public Electricity()
        {
            InitializeComponent();

            configParam = new CONFIG_PARAM();

            cb_MachineNum.SelectedIndex = 0;
            cb_ParaNum.SelectedIndex = 0;
        }

        public void SetConfig()
        {
            checkCode1.Checked = FormMain.form.arrScanParam[0].bCheckLen;
            uiTextCH1Length.Text = FormMain.form.arrScanParam[0].iCodeLength.ToString();
            checkStart1.Checked = FormMain.form.arrScanParam[0].bStart;

            checkCode2.Checked = FormMain.form.arrScanParam[1].bCheckLen;
            uiTextCH2Length.Text = FormMain.form.arrScanParam[1].iCodeLength.ToString();
            checkStart2.Checked = FormMain.form.arrScanParam[1].bStart;

            //if (FormMain.form.arrDevObj[cb_MachineNum.SelectedIndex].groupId >= 0
            //    && FormMain.form.arrDevObj[cb_MachineNum.SelectedIndex].groupId < 10)
            //{
            //    cb_ParaNum.SelectedIndex = FormMain.form.arrDevObj[cb_MachineNum.SelectedIndex].groupId;
            //}
            //else
            //{
            //    readConfig();
            //}
            readConfig();
        }

        public void readConfig()
        {
            try
            {
                if (FormMain.form.arrDevObj[cb_MachineNum.SelectedIndex] != null
                    && FormMain.form.arrDevObj[cb_MachineNum.SelectedIndex].bStatus)
                {
                    if (FormMain.form.arrDevObj[cb_MachineNum.SelectedIndex].GetConfig(ref configParam) > 0)
                    {
                        showConfig(ref configParam);
                    }
                    else
                    {
                        MessageBox.Show("读取参数失败！");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                BtnRead.Enabled = true;
            }
        }
        // 读取参数 按钮
        private void BtnRead_Click(object sender, EventArgs e)
        {
            readConfig();
        }

        private void showConfig(ref CONFIG_PARAM pParam)
        {
            tb_FullTime.Text = pParam.inflationTime.ToString();
            tb_BalanTime.Text = pParam.balanceTime.ToString();
            tb_TestTime1.Text = pParam.testTime1.ToString();
            tb_ExhaustTime.Text = pParam.exhaustTime.ToString();
            tb_Evolume.Text = pParam.fEquivalentVolume.ToString();
            tb_DelayTime1.Text = pParam.delayTime1.ToString();
            tb_DelayTime2.Text = pParam.delayTime2.ToString();
            tb_RelieveDelay.Text = pParam.testTime2.ToString();

            tb_FPtoplimit.Text = pParam.pressureUpperLimit.ToString();
            tb_FPlowlimit.Text = pParam.pressureLowerLimit.ToString();
            tb_BalanPreMax.Text = pParam.balanceUpperLimit.ToString();
            tb_BalanPreMin.Text = pParam.balanceLowerLimit.ToString();
            tb_Leaktoplimit.Text = pParam.leakageUpperLimit.ToString();
            tb_Leaklowlimit.Text = pParam.leakageLowerLimit.ToString();
            cb_PUnit.SelectedIndex = pParam.pressureUnit;
            cb_LUnit.SelectedIndex = pParam.leakageUnit;
            chk_ChkBee.Checked = pParam.bee;
            BtnRead.Enabled = true;
        }

        // 保存参数 按钮
        private void BtnSave_Click(object sender, EventArgs e)
        {
            configParam.inflationTime = float.Parse(tb_FullTime.Text);
            configParam.balanceTime = float.Parse(tb_BalanTime.Text);
            configParam.testTime1 = float.Parse(tb_TestTime1.Text);
            configParam.exhaustTime = float.Parse(tb_ExhaustTime.Text);
            configParam.fEquivalentVolume = float.Parse(tb_Evolume.Text);
            configParam.delayTime1 = float.Parse(tb_DelayTime1.Text);
            configParam.delayTime2 = float.Parse(tb_DelayTime2.Text);
            configParam.testTime2 = float.Parse(tb_RelieveDelay.Text);

            configParam.pressureUpperLimit = float.Parse(tb_FPtoplimit.Text);
            configParam.pressureLowerLimit = float.Parse(tb_FPlowlimit.Text);
            configParam.balanceUpperLimit = float.Parse(tb_BalanPreMax.Text);
            configParam.balanceLowerLimit = float.Parse(tb_BalanPreMin.Text);
            configParam.leakageUpperLimit = float.Parse(tb_Leaktoplimit.Text);
            configParam.leakageLowerLimit = float.Parse(tb_Leaklowlimit.Text);
            configParam.pressureUnit = cb_PUnit.SelectedIndex;
            configParam.leakageUnit = cb_LUnit.SelectedIndex;
            configParam.bee = chk_ChkBee.Checked;
            if (FormMain.form.arrDevObj[cb_MachineNum.SelectedIndex] != null
                    && FormMain.form.arrDevObj[cb_MachineNum.SelectedIndex].bStatus)
            {
                if (FormMain.form.arrDevObj[cb_MachineNum.SelectedIndex].SetConfig(ref configParam))
                {
                    MessageBox.Show("配置成功");
                }
                else
                {
                    MessageBox.Show("配置失败");
                }
            }
            else
            {
                MessageBox.Show("通道" + cb_MachineNum.SelectedIndex + "未连接");
            }
        }

        private void ParaNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            //切换分组
            if (FormMain.form.arrDevObj[cb_MachineNum.SelectedIndex] != null 
                && FormMain.form.arrDevObj[cb_MachineNum.SelectedIndex].bStatus)
            {
                FormMain.form.arrDevObj[cb_MachineNum.SelectedIndex].SetGroupId(cb_ParaNum.SelectedIndex);

                readConfig();
            }
        }

        private void MachineNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

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


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

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

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
               // Form1.f1.plc.w正压模式();
            }
           
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
           //     Form1.f1.plc.w负压模式();
            }
            
        }

        // 通道1扫码
        private void checkCode1_CheckedChanged(object sender, EventArgs e)
        {
            FormMain.form.arrScanParam[0].bCheckLen = checkCode1.Checked;
            Config.saveScanParam(0);
        }

        // 通道2扫码
        private void checkCode2_CheckedChanged(object sender, EventArgs e)
        {
            FormMain.form.arrScanParam[1].bCheckLen = checkCode2.Checked;
            Config.saveScanParam(1);
        }

        private void PUnit_TextChanged(object sender, EventArgs e)
        {
            lb_PUnit1.Text = cb_PUnit.Text;
            lb_PUnit2.Text = cb_PUnit.Text;
        }

        private void LUnit_TextChanged(object sender, EventArgs e)
        {
            lb_LUnit1.Text = cb_LUnit.Text;
            lb_LUnit2.Text = cb_LUnit.Text;
        }

        private void checkStart1_CheckedChanged(object sender, EventArgs e)
        {
            FormMain.form.arrScanParam[0].bStart = checkStart1.Checked;
            Config.saveScanParam(0);
            FormMain.form.updataScanStart(0, checkStart1.Checked);
        }

        private void checkStart2_CheckedChanged(object sender, EventArgs e)
        {
            FormMain.form.arrScanParam[1].bStart = checkStart2.Checked;
            Config.saveScanParam(1);
            FormMain.form.updataScanStart(1, checkStart2.Checked);
        }

        private void uiButtonSaveCode_Click(object sender, EventArgs e)
        {
            FormMain.form.arrScanParam[0].bCheckLen = checkCode1.Checked;
            FormMain.form.arrScanParam[0].iCodeLength = int.Parse(uiTextCH1Length.Text);
            FormMain.form.arrScanParam[0].bStart = checkStart1.Checked;

            FormMain.form.arrScanParam[1].bCheckLen = checkCode2.Checked;
            FormMain.form.arrScanParam[1].iCodeLength = int.Parse(uiTextCH2Length.Text);
            FormMain.form.arrScanParam[1].bStart = checkStart2.Checked;

            for (int i = 0; i < 2; i++)
            {
                Config.saveScanParam(i);
            }

            MessageBox.Show("设置成功");
        }

        private void cb_MachineNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            readConfig();
        }
    }
}
