using MesWorkMqtt;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Receiving;

using bizFacade;
using MesWorkMqtt;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MQTTnet;
using System.Threading;
using DoWhatPlc;
using MesServerWork;

namespace SyUpLoad
{
    public partial class PartLoad
    {
        /// <summary>
        /// MesServerFunction
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        /// <param name="fun"></param>
        private void MesServerFunction(string msgValue)
        {
            try
            {
                DoWhatPlc.ReturnValueString rv = Newtonsoft.Json.JsonConvert.DeserializeObject<DoWhatPlc.ReturnValueString>(msgValue);

                string OpName;
                string tagValue;
                string fun;
                string mesServerCode;
                string mesServerdllName;

                OpName = rv.OpName;
                switch (rv.TagValue.ToLower())
                {
                    case "false":
                        tagValue = "0";
                        break;
                    case "true":
                        tagValue = "1";
                        break;
                    default:
                        tagValue = rv.TagValue;
                        break;
                }
                fun = rv.FunCmd;
                mesServerCode = rv.MesServerCode;
                mesServerdllName = rv.FunName;
                CustomeEvetnArgs e = new CustomeEvetnArgs();
                e.OpName = OpName;
                e.TagValue = tagValue;
                switch (fun)
                {

                    //
                    case "fun_MesReadOver":
                        fun_MesReadOver(e);
                        break;
                    case "fun_MesRead":
                        fun_MesRead(e);
                        break;
                    case "fun_EngineTypeGetOverMES":
                        fun_EngineTypeGetOverMES(e);
                        break;
                    case "fun_WorkStart":
                        fun_WorkStart(e);
                        break;
                    case "fun_PartLoad":

                        fun_PartLoad(e);
                        break;
                    case "fun_EngineTypeGetOverPLC":
                        fun_EngineTypeGetOver_PLC(e);
                        break;
                    case "fun_FixAllow":
                        fun_FixAllow(e);
                        break;

                }

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "MesServerFunction");
            }
        }

        private void fun_EngineTypeGetOverMES(object e)
        {
            DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
            string OpName = Convert.ToString(msgEvent.OpName);
            int tagValue = Convert.ToInt32(msgEvent.TagValue);
            var cb = button_ManualPartLoad;
            cb.Invoke(new Action(() => {
                if (tagValue == 1)
                {
                    cb.Enabled = false;
                }
                else
                {
                    cb.Enabled = true;
                }
            }));
            SetChekcBox(tagValue, checkBox_MesEngineTypeGetOver);

        }
        private void fun_FixAllow(object e)
        {
            DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
            string OpName = Convert.ToString(msgEvent.OpName);
            int tagValue = Convert.ToInt32(msgEvent.TagValue);

            
            string funID = ht_OpnameFun[OpName].ToString();
            switch (funID)
            {
                case "1":
                    SetChekcBox(tagValue, checkBox_MesGoOver);
                    break;
                case "2":
                    SetChekcBox(tagValue, checkBox_MesGoOver2);
                    break;
                case "3":
                    SetChekcBox(tagValue, checkBox_MesGoOver3);
                    break;
            }

        }

        private void fun_EngineTypeGetOver_PLC(object e)
        {
            DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
            string OpName = Convert.ToString(msgEvent.OpName);
            int tagValue = Convert.ToInt32(msgEvent.TagValue);

            string funID = ht_OpnameFun[OpName].ToString();
            switch (funID)
            {
                case "1":
                    fun_EngineTypeGetOver_PLC1(e); 
                    break;
                case "2":
                    fun_EngineTypeGetOver_PLC2(e);
                    break;
                case "3":
                    fun_EngineTypeGetOver_PLC3(e);
                    break;
            }
        }
        private void fun_EngineTypeGetOver_PLC1(object e)
        {
            DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
            string OpName = Convert.ToString(msgEvent.OpName);
            int tagValue = Convert.ToInt32(msgEvent.TagValue);

            SetChekcBox(tagValue, checkBox_PlcEngineTypeGetOver);
            try
            {
                if (tagValue == 1)
                {
                    int EngineTypeID;//机型代码
                    int PartPallet_Code;//托盘编号
                    string EngineType;//机型
                    string EngineID;//工件编号

                    MIS_BASE.GetEngineTypeFromPLC(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code);
                    SetTextBox(EngineType, textBox_PlcType);
                    SetTextBox(EngineID, textBox_PlcEngineID);
                    EngineTypeGetOver_PLC_Work(OpName, EngineTypeID, EngineType, EngineID, PartPallet_Code);
                }

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function03:EngineTypeGetOver_PLC");
            }
        }
        private void fun_WorkStart(object e)
        {
            DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
            string OpName = Convert.ToString(msgEvent.OpName);
            int tagValue = Convert.ToInt32(msgEvent.TagValue);
            string funID = ht_OpnameFun[OpName].ToString();
            switch(funID)
            {
                case "1":
            SetChekcBox(tagValue, checkBox_WorkStart);
                    break;
                case "2":
            SetChekcBox(tagValue, checkBox_WorkStart2);
                    break;
                case "3":
            SetChekcBox(tagValue, checkBox_WorkStart3);
                    break;
            }
        }

        private void button_ManualPartLoad_Click(object sender, EventArgs e)
        {
            fun_PartLoadManual(OPName01);
        }
    }
}
