using BPA.Utility;
using HBLConsole.Communication;
using HBLConsole.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static BPA.Utility.EventBus;

namespace HBLDevice.IceCream
{
    /// <summary>
    /// 指令封装
    /// </summary>
    internal class CommandHandler
    {
        byte[] cmdHeartDW;

        private SerialPortClient commProxy;
        private IceCreamEndCook iceCreamEndCook = new IceCreamEndCook();
        public Action<bool> PauseAsk { get; set; }

        /// <summary>
        /// 初始化
        /// </summary>
        internal void Init(SerialPortClient commProxy)
        {
            this.commProxy = commProxy;
            ICMSG_Heart_DW heartDW = new ICMSG_Heart_DW();
            cmdHeartDW = IcPack.StructureToByte(heartDW);
            EventBus.GetInstance().Subscribe<ModeSetEvent>(ModeSetEventHandle);
            EventBus.GetInstance().Subscribe<DischargeEvent>(DischargeEventHandle);
        }
        /// <summary>
        /// 发送心跳
        /// </summary>
        internal byte[] GetHeartDW()
        {
            return cmdHeartDW;
        }


        public void ModeSetEventHandle(IEvent @event, EventCallBackHandle callBack = null)
        {
            PauseAsk?.Invoke(true);
            Thread.Sleep(200);
            var data = IcPack.StructureToByte(ICMSG_MODE_DW.Build(((ModeSetEvent)@event).Mode));
            commProxy.SendData(data);
            Thread.Sleep(200);
            PauseAsk?.Invoke(false);
            MessageLog.GetInstance.Show(string.Format("设置模式[{0}]", ((ModeSetEvent)@event).Mode));
        }

        public void DischargeEventHandle(IEvent @event, EventCallBackHandle callBack = null)
        {
            if (MorkIStatus.GetInstance().Fault != MORKI_FAULT.未发生故障)
            {
                MessageLog.GetInstance.Show(string.Format("当前存在故障[{0}%]，不允许制作", MorkIStatus.GetInstance().Fault));
                return;
            }
            if (MorkIStatus.GetInstance().CXB <= 86)
            {
                MessageLog.GetInstance.Show(string.Format("当前成型比[{0}%]，低于86%，不允许制作", MorkIStatus.GetInstance().CXB));
                return;
            }

            bool modeRight = MorkIStatus.GetInstance().CurrentMode == MORKI_MODE.制冷模式;

            if (!modeRight)
            {
                var temp = IcPack.StructureToByte(ICMSG_MODE_DW.Build(MORKI_MODE.制冷模式));
                commProxy.SendData(temp);
                MessageLog.GetInstance.Show(string.Format("出料操作->设置模式[{0}]", MORKI_MODE.制冷模式));

                DateTime freeTime = DateTime.Now.AddSeconds(5);
                while (DateTime.Now < freeTime)
                {
                    Thread.Sleep(10);
                    modeRight = MorkIStatus.GetInstance().CurrentMode == MORKI_MODE.制冷模式;
                    if (modeRight)
                        break;
                }
            }

            if (modeRight)
            {
                PauseAsk?.Invoke(true);
                Thread.Sleep(200);
                var data = IcPack.StructureToByte(ICMSG_MODE_DW.Build(MORKI_MODE.打料));
                commProxy.SendData(data);
                Thread.Sleep(200);
                PauseAsk?.Invoke(false);
                iceCreamEndCook.Publish();
                MessageLog.GetInstance.Show(string.Format("出料操作->设置模式[{0}]", MORKI_MODE.打料));
            }
            else
            {
                MessageLog.GetInstance.Show(string.Format("出料操作->模式切换失败，当前模式[{0}]，不允许出料", MorkIStatus.GetInstance().CurrentMode));
            }
        }
    }
}
