using DataBlockHelper.DBHelpers;
using DataBlockHelper.Entity.DB2104Entity;
using Mesnac.Action.Base;
using Mesnac.Action.ChemicalWeighing.AutoControl.DB;
using Mesnac.Action.ChemicalWeighing.AutoControl.Entity;
using Mesnac.Action.ChemicalWeighing.LjMixManager;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Mesnac.Action.ChemicalWeighing.LjPlanning;
using IAction = Mesnac.Action.Base.IAction;
using Mesnac.Core.Service;
using static Mesnac.Action.ChemicalWeighing.AutoControl.AutoLogHelper;
using static Mesnac.Action.ChemicalWeighing.AutoControl.PlcAutoWriteHelper;
using Mesnac.Controls.Default;
using Mesnac.Action.ChemicalWeighing.Report;
using System.Threading;
using DataBlockHelper;
using System.Diagnostics;
using Mesnac.Action.ChemicalWeighing.InterfaceDocking.DockingEntity;
using Mesnac.Action.ChemicalWeighing.InterfaceDocking;
using Mesnac.Action.ChemicalWeighing.SetValueInfo;
using System.Security.Cryptography.X509Certificates;
using Mesnac.Action.ChemicalWeighing.FreeDb;
using Mesnac.Action.ChemicalWeighing.MainDetailControl.Entity;
using MCLabelEntity = Mesnac.Action.ChemicalWeighing.AutoControl.Entity.MCLabelEntity;

namespace Mesnac.Action.ChemicalWeighing.AutoControl
{
    internal class AutoControl : ChemicalWeighingAction, IAction
    {
        private List<Control> McControls;
        MCTextBoxEntity TextE = new MCTextBoxEntity();
        ButtonEntity ButtonE = new ButtonEntity();
        MCLabelEntity LabelE = new MCLabelEntity();
        MCComboBoxEntity ComboE = new MCComboBoxEntity();
        MCRadioButtonEntity RadioE = new MCRadioButtonEntity();

        public void Run(RuntimeParameter runtime)
        {
            base.RunIni(runtime);       //必须要调用

            McControls = GetAllControls();

            ControlsHelper.ControlImport<MCTextBox>(TextE, McControls);
            ControlsHelper.ControlImport<System.Windows.Forms.Button>(ButtonE, McControls);
            ControlsHelper.ControlImport<MCCombobox>(ComboE, McControls);
            ControlsHelper.ControlImport<MCLabel>(LabelE, McControls);
            ControlsHelper.ControlImport<MCRadioButton>(RadioE, McControls);

            try
            {
                BottomSelect(runtime);
            }
            catch (Exception e)
            {
                MessageBox.Show("请检查输入内容格式是否正确!");
            }

        }

        //按钮事件
        private void BottomSelect(RuntimeParameter runtime)
        {
            #region 自动参数下传

            #region 湿混机糊化机参数下传

            if (ButtonE.Download == runtime.Sender)
            {
                if (MessageBox.Show("确认下传数据?", "下传确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }

                GelWetDownload(TextE, ComboE, RadioE);
            }

            #endregion

            #region 水称参数下传

            if (ButtonE.WaterDownload == runtime.Sender)
            {
                if (MessageBox.Show("确认下发水称数据?", "下发确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }

                WaterDownload(TextE, ComboE);

                if (ComboE.CALeftOrRight.MCValue.ToString() == "1")
                {
                    LjLogControl.ManualLogControl("冷水称A向机台1设置值", Convert.ToInt32(TextE.ColdValueA.MCValue.ToString()), 1, 1, 1);
                    LjLogControl.ManualLogControl("冷水称A向机台2设置值", Convert.ToInt32(TextE.ColdValueA.MCValue.ToString()), 1, 2, 1);
                    LjLogControl.ManualLogControl("冷水称A向机台3设置值",
                        Convert.ToInt32(TextE.ColdValueA.MCValue.ToString()) + Convert.ToInt32(TextE.CADeviation.MCValue.ToString()), 1, 3, 1);
                    LjLogControl.ManualLogControl("冷水称A向机台4设置值",
                            Convert.ToInt32(TextE.ColdValueA.MCValue.ToString()) + Convert.ToInt32(TextE.CADeviation.MCValue.ToString()), 1, 4, 1);
                }
                else if (ComboE.CALeftOrRight.MCValue.ToString() == "2")
                {
                    LjLogControl.ManualLogControl("冷水称A向机台3设置值", Convert.ToInt32(TextE.ColdValueA.MCValue.ToString()), 1, 3, 1);
                    LjLogControl.ManualLogControl("冷水称A向机台4设置值", Convert.ToInt32(TextE.ColdValueA.MCValue.ToString()), 1, 4, 1);
                    LjLogControl.ManualLogControl("冷水称A向机台1设置值",
                        Convert.ToInt32(TextE.ColdValueA.MCValue.ToString()) + Convert.ToInt32(TextE.CADeviation.MCValue.ToString()), 1, 1, 1);
                    LjLogControl.ManualLogControl("冷水称A向机台2设置值",
                            Convert.ToInt32(TextE.ColdValueA.MCValue.ToString()) + Convert.ToInt32(TextE.CADeviation.MCValue.ToString()), 1, 2, 1);
                }

                if (ComboE.HALeftOrRight.MCValue.ToString() == "1")
                {
                    LjLogControl.ManualLogControl("热水称A向机台1设置值", Convert.ToInt32(TextE.HotValueA.MCValue.ToString()), 1, 1, 1);
                    LjLogControl.ManualLogControl("热水称A向机台2设置值", Convert.ToInt32(TextE.HotValueA.MCValue.ToString()), 1, 2, 1);
                    LjLogControl.ManualLogControl("热水称A向机台3设置值",
                        Convert.ToInt32(TextE.HotValueA.MCValue.ToString()) + Convert.ToInt32(TextE.HADeviation.MCValue.ToString()), 1, 3, 1);
                    LjLogControl.ManualLogControl("热水称A向机台4设置值",
                            Convert.ToInt32(TextE.HotValueA.MCValue.ToString()) + Convert.ToInt32(TextE.HADeviation.MCValue.ToString()), 1, 4, 1);
                }
                else if (ComboE.HALeftOrRight.MCValue.ToString() == "2")
                {
                    LjLogControl.ManualLogControl("热水称A向机台3设置值", Convert.ToInt32(TextE.HotValueA.MCValue.ToString()), 1, 3, 1);
                    LjLogControl.ManualLogControl("热水称A向机台4设置值", Convert.ToInt32(TextE.HotValueA.MCValue.ToString()), 1, 4, 1);
                    LjLogControl.ManualLogControl("热水称A向机台1设置值",
                        Convert.ToInt32(TextE.HotValueA.MCValue.ToString()) + Convert.ToInt32(TextE.HADeviation.MCValue.ToString()), 1, 1, 1);
                    LjLogControl.ManualLogControl("热水称A向机台2设置值",
                            Convert.ToInt32(TextE.HotValueA.MCValue.ToString()) + Convert.ToInt32(TextE.HADeviation.MCValue.ToString()), 1, 2, 1);
                }

                if (ComboE.CBLeftOrRight.MCValue.ToString() == "1")
                {
                    LjLogControl.ManualLogControl("冷水称B向机台5设置值", Convert.ToInt32(TextE.ColdValueB.MCValue.ToString()), 1, 5, 1);
                    LjLogControl.ManualLogControl("冷水称B向机台6设置值", Convert.ToInt32(TextE.ColdValueB.MCValue.ToString()), 1, 6, 1);
                    LjLogControl.ManualLogControl("冷水称B向机台7设置值",
                        Convert.ToInt32(TextE.ColdValueB.MCValue.ToString()) + Convert.ToInt32(TextE.CBDeviation.MCValue.ToString()), 1, 7, 1);
                    LjLogControl.ManualLogControl("冷水称B向机台8设置值",
                            Convert.ToInt32(TextE.ColdValueB.MCValue.ToString()) + Convert.ToInt32(TextE.CBDeviation.MCValue.ToString()), 1, 8, 1);
                }
                else if (ComboE.CBLeftOrRight.MCValue.ToString() == "2")
                {
                    LjLogControl.ManualLogControl("冷水称B向机台7设置值", Convert.ToInt32(TextE.ColdValueB.MCValue.ToString()), 1, 7, 1);
                    LjLogControl.ManualLogControl("冷水称B向机台8设置值", Convert.ToInt32(TextE.ColdValueB.MCValue.ToString()), 1, 8, 1);
                    LjLogControl.ManualLogControl("冷水称B向机台5设置值",
                        Convert.ToInt32(TextE.ColdValueB.MCValue.ToString()) + Convert.ToInt32(TextE.CBDeviation.MCValue.ToString()), 1, 5, 1);
                    LjLogControl.ManualLogControl("冷水称B向机台6设置值",
                            Convert.ToInt32(TextE.ColdValueB.MCValue.ToString()) + Convert.ToInt32(TextE.CBDeviation.MCValue.ToString()), 1, 6, 1);
                }

                if (ComboE.HBLeftOrRight.MCValue.ToString() == "1")
                {
                    LjLogControl.ManualLogControl("热水称B向机台5设置值", Convert.ToInt32(TextE.HotValueB.MCValue.ToString()), 1, 5, 1);
                    LjLogControl.ManualLogControl("热水称B向机台6设置值", Convert.ToInt32(TextE.HotValueB.MCValue.ToString()), 1, 6, 1);
                    LjLogControl.ManualLogControl("热水称B向机台7设置值",
                        Convert.ToInt32(TextE.HotValueB.MCValue.ToString()) + Convert.ToInt32(TextE.HBDeviation.MCValue.ToString()), 1, 7, 1);
                    LjLogControl.ManualLogControl("热水称B向机台8设置值",
                            Convert.ToInt32(TextE.HotValueB.MCValue.ToString()) + Convert.ToInt32(TextE.HBDeviation.MCValue.ToString()), 1, 8, 1);
                }
                else if (ComboE.HBLeftOrRight.MCValue.ToString() == "2")
                {
                    LjLogControl.ManualLogControl("热水称B向机台7设置值", Convert.ToInt32(TextE.HotValueB.MCValue.ToString()), 1, 7, 1);
                    LjLogControl.ManualLogControl("热水称B向机台8设置值", Convert.ToInt32(TextE.HotValueB.MCValue.ToString()), 1, 8, 1);
                    LjLogControl.ManualLogControl("热水称B向机台5设置值",
                        Convert.ToInt32(TextE.HotValueB.MCValue.ToString()) + Convert.ToInt32(TextE.HBDeviation.MCValue.ToString()), 1, 5, 1);
                    LjLogControl.ManualLogControl("热水称B向机台6设置值",
                            Convert.ToInt32(TextE.HotValueB.MCValue.ToString()) + Convert.ToInt32(TextE.HBDeviation.MCValue.ToString()), 1, 6, 1);
                }
            }

            #endregion

            #region 干混机参数下传

            if (ButtonE.DryDownload == runtime.Sender)
            {
                if (MessageBox.Show("确认下传干混机数据?", "下传确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }

                DryDownload(TextE, ComboE, RadioE);
            }

            #endregion

            #endregion

            #region 手动控制

            //手动粉料
            if (ButtonE.GelManualPowder == runtime.Sender)
            {
                if (MessageBox.Show("确认手动下粉料?", "下粉确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }

                bool isReady = GelPowderDownload(RadioE);

                if (!isReady)
                {
                    MessageBox.Show("信号未准备好!");
                }
                else
                {
                    int i = ControlsHelper.SingleSelect(RadioE);
                    LjLogControl.ManualLogControl("手动下粉料向" + i, i, 3);

                    MessageBox.Show("信号下达成功!");
                }
            }

            //手动冷水
            if (ButtonE.GelManualColdWater == runtime.Sender)
            {
                if (MessageBox.Show("确认下冷水?", "下水确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }

                bool isReady = GelColdWaterDownload(RadioE);

                if (!isReady)
                {
                    MessageBox.Show("信号未准备好!");
                }
                else
                {
                    int i = ControlsHelper.SingleSelect(RadioE);
                    LjLogControl.ManualLogControl("手动下冷水向" + i, i, 1);

                    MessageBox.Show("信号下达成功!");
                }

            }

            //手动热水
            if (ButtonE.GelManualHotWater == runtime.Sender)
            {
                if (MessageBox.Show("确认下热水?", "下水确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }

                bool isReady = GelHotWaterDownload(RadioE);

                if (!isReady)
                {
                    MessageBox.Show("信号未准备好!");
                }
                else
                {
                    int i = ControlsHelper.SingleSelect(RadioE);
                    LjLogControl.ManualLogControl("手动下热水向" + i, i, 1);

                    MessageBox.Show("信号下达成功!");
                }
            }

            //手动加糊化料
            if (ButtonE.WetManualGel == runtime.Sender)
            {
                if (MessageBox.Show("糊化机打成手动 然后开门放料", "请使用手动加料", MessageBoxButtons.OK, MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }
                return;



                //bool isReady = WetGelatDownload(RadioE);

                //if (!isReady)
                //{
                //    MessageBox.Show("信号未准备好!");
                //}
                //else
                //{
                //    SingleLog("手动下糊化料", RadioE);

                //    MessageBox.Show("信号下达成功!");
                //}
            }

            //湿混机手动下粉料
            if (ButtonE.WetManualControl == runtime.Sender)
            {

                var valu = TextE.ManualWeight.MCValue.ToString();
                if (string.IsNullOrEmpty(valu))
                {
                    MessageBox.Show("请输入重量值");
                    TextE.ManualWeight.Focus();

                    return;
                }

                if (MessageBox.Show("确认下传数据?", "下传确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }

                bool b = WetGelatDownload(TextE, RadioE);

                if (b)
                {
                    int i = ControlsHelper.SingleSelect(RadioE);
                    LjLogControl.ManualLogControl("手动下黑粉料向" + i, Convert.ToInt32(TextE.ManualWeight.MCValue.ToString()), 3, i, 2);

                    MessageBox.Show("数据下传成功!称量值复位 防止重复点击");
                    TextE.ManualWeight.MCValue = "";


                }


            }

            if (ButtonE.WetManualControlStop == runtime.Sender)
            {
                WetGelatStopDownload(RadioE);
                MessageBox.Show("手动停止称量");
            }

            if (ButtonE.CAOnOff == runtime.Sender)
            {
                if(ButtonE.CAOnOff.Text == "停止称量")
                {
                    DB2105WriteHelper.StopWater(0);
                    ButtonE.CAOnOff.Text = "恢复称量";
                }
                else if(ButtonE.CAOnOff.Text == "恢复称量")
                {
                    DB2105WriteHelper.StartWater(0);
                    ButtonE.CAOnOff.Text = "停止称量";

                }
            }

            if (ButtonE.CBOnOff == runtime.Sender)
            {
                if (ButtonE.CBOnOff.Text == "停止称量")
                {
                    DB2105WriteHelper.StopWater(2);
                    ButtonE.CBOnOff.Text = "恢复称量";
                }
                else if (ButtonE.CBOnOff.Text == "恢复称量")
                {
                    DB2105WriteHelper.StartWater(2);
                    ButtonE.CBOnOff.Text = "停止称量";
                }
            }

            if (ButtonE.HAOnOff == runtime.Sender)
            {
                if (ButtonE.HAOnOff.Text == "停止称量")
                {
                    DB2105WriteHelper.StopWater(1);
                    ButtonE.HAOnOff.Text = "恢复称量";
                }
                else if (ButtonE.HAOnOff.Text == "恢复称量")
                {
                    DB2105WriteHelper.StartWater(1);
                    ButtonE.HAOnOff.Text = "停止称量";
                }
            }

            if (ButtonE.HBOnOff == runtime.Sender)
            {
                if (ButtonE.HBOnOff.Text == "停止称量")
                {
                    DB2105WriteHelper.StopWater(3);
                    ButtonE.HBOnOff.Text = "恢复称量";
                }
                else if (ButtonE.HBOnOff.Text == "恢复称量")
                {
                    DB2105WriteHelper.StartWater(3);
                    ButtonE.HBOnOff.Text = "停止称量";
                }
            }

            //设置现场重量

            if(ButtonE.GelSetWeight == runtime.Sender)
            {

                FormEnqPar formEnqPar = new FormEnqPar();
                formEnqPar.ShowDialog();

                //var singE = ControlsHelper.SingleSelect(RadioE);
                //if (singE == 0)
                //{
                //    MessageBox.Show("请选择湿混机");
                //}
                //else
                //{
                //    float t = Convert.ToSingle(TextE.ManualTolerance.MCValue.ToString());
                //    float w = Convert.ToSingle(TextE.ManualWeight.MCValue.ToString());
                //    int no = (singE + 1) / 2;
                //    string message = $"确认设置螺旋{no} 现场重量 {w}";
                //    if (MessageBox.Show($"{message}?", "现场参数确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                //    {
                //        return;
                //    }
                //    if (t < 0.1)
                //    {
                //        MessageBox.Show("公差不能小于0.1!");
                //        return;
                //    }


                //    SetValue value = new SetValue()
                //    {
                //        Value = w,
                //        Toterance = t
                //    };

                //    PlcAutoWriteHelper.WetSetWeight(value, RadioE);
                //}


            }

            #endregion

            #region 自动启停

            if (ButtonE.LineStart == runtime.Sender)
            {
                if (MessageBox.Show("确认启动生产?", "启动确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }

                LineStart(RadioE);

                MessageBox.Show("产线已启动!");

            }

            if (ButtonE.LineStop == runtime.Sender)
            {
                LineStop(RadioE);

                MessageBox.Show("已经停止生产");

            }

            if (ButtonE.BtnQz == runtime.Sender)
            {
                if (MessageBox.Show("确认启动强制?", "启动强制", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }

                var siemensS7Net = PlcConnect.Instance;
                siemensS7Net.Write($"DB2107.10.2", true);

                MessageBox.Show("已经强制");

            }
            

            #endregion

            #region 维修模式

            if (ButtonE.RepairMode == runtime.Sender)
            {
                RepairOnOff(LabelE, RadioE);
            }

            if(ButtonE.GRepairMode == runtime.Sender)
            {
                GRepairOnOff(ButtonE, RadioE);
            }

            if (ButtonE.DryStartOrStop == runtime.Sender)
            {
                DryStartOrStop(ButtonE, RadioE);
            }

            //DryPauseOrReply
            if (ButtonE.DryPauseOrReply == runtime.Sender)
            {
                DryPauseOrReply(ButtonE, RadioE);
            }


                if (ButtonE.ZeroClean == runtime.Sender)
            {
                if (MessageBox.Show("是否车次清零?", "清零确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }

                var siemensS7Net = PlcConnect.Instance;
                siemensS7Net.Write($"DB2105.6.0", (short)1);
                MessageBox.Show("清零成功");

            }

            #endregion

            #region 粘贴数据

            if (ButtonE.PasteWet == runtime.Sender)
            {
                int index = ComboE.PasteChooseWet.SelectedIndex + 1;
                ControlsHelper.GWInit2(index, ComboE, TextE);
            }

            if (ButtonE.PasteDry == runtime.Sender)
            {
                int index = ComboE.PasteChooseDry.SelectedIndex + 1;
                ControlsHelper.DInit(index, ComboE, TextE);
            }

            #endregion

            #region 清报警 

            if (ButtonE.EliAlarms == runtime.Sender)
            {
                PlcConnect.Instance.Write("DB2107.10.7", true);
                Thread.Sleep(2000);
                PlcConnect.Instance.Write("DB2107.10.7", false);
            }

            #endregion

            #region 下传送料请求 

            if (ButtonE.DownloadRequest == runtime.Sender)
            {
                bool mixok1 = PlcConnect.Instance.ReadBool("DB3000.0.0").Content;
                Time(1, mixok1 ? 1 : 0);
                bool mixok2 = PlcConnect.Instance.ReadBool("DB3000.0.1").Content;
                Time(2, mixok2 ? 1 : 0);
                bool mixok3 = PlcConnect.Instance.ReadBool("DB3000.0.2").Content;
                Time(3, mixok3 ? 1 : 0);
                bool mixok4 = PlcConnect.Instance.ReadBool("DB3000.0.3").Content;
                Time(4, mixok4 ? 1 : 0);
                bool mixok5 = PlcConnect.Instance.ReadBool("DB3000.0.4").Content;
                Time(5, mixok5 ? 1 : 0);
                bool mixok6 = PlcConnect.Instance.ReadBool("DB3000.0.5").Content;
                Time(6, mixok6 ? 1 : 0);
                bool mixok7 = PlcConnect.Instance.ReadBool("DB3000.0.6").Content;
                Trace.WriteLine("7号信号:" + mixok7);
                Time(7, mixok7 ? 1 : 0);
                bool mixok8 = PlcConnect.Instance.ReadBool("DB3000.0.7").Content;
                Trace.WriteLine("8号信号:" + mixok8);
                Time(8, mixok8 ? 1 : 0);
            }

            #endregion

            #region 清除计时记录

            if(runtime.Sender == ButtonE.TimeRecordClear)
            {
                FreeSqlUnit.Instance.Delete<Lj_RGVTimeRecord>().Where(x => x.Done == false).ExecuteDeleted();
            }

            #endregion

        }

        #region 下发接料请求

        /// <summary>
        /// 下发接料请求
        /// </summary>
        /// <param name="deviceNo"></param>
        /// <param name="statu"></param>
        private void Time(int deviceNo, int statu)
        {
            TankIsDischargedSyncEntity tankIsDischargedSyncEntity = new TankIsDischargedSyncEntity()
            {
                reqCode = System.Guid.NewGuid().ToString(),
                reqTime = DateTime.Now,
                deviceNo = deviceNo.ToString(),
                state = statu.ToString(),
            };
            //即使网络不同 也不会影响上位机的使用
            ThreadPool.QueueUserWorkItem(delegate
            {
                HttpResponse httpResponse = new HttpResponse();
                string returnTest = httpResponse.PostResponse(tankIsDischargedSyncEntity);
                MesnacServiceManager.Instance.LoggingService.Info($"湿混机{deviceNo}返回数据" + returnTest);
            });
        }

        #endregion

    }
}