
using BPASmartClient.EventBus;
using BPASmartClient.Model.单片机;
using BPASmartClient.Model.单片机.Enum;
using BPASmartClient.SCChip;
using BPASmartClient.SerialPort;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static BPASmartClient.EventBus.EventBus;

namespace BPASmartClient.SCChip
{ 
    /// <summary>
    /// 指令封装
    /// </summary>
    internal class CommandHandler
    {
        private SerialPortClient commProxy;
        private ICChipPackage package = new ICChipPackage();

        /// <summary>
        /// 初始化
        /// </summary>
        internal void Init(SerialPortClient commProxy)
        {
            this.commProxy = commProxy;
            EventBus.EventBus.GetInstance().Subscribe<SCChip_TakeCupEvent>(0,TakeCupEventHandle);
            EventBus.EventBus.GetInstance().Subscribe<SCChip_MakeIceCreamEvent>(0, MakeIceCreamEventHandle);
            EventBus.EventBus.GetInstance().Subscribe<SCChip_SESwitchCreamEvent>(0, StopMakeIceCreamEventHandle);
            EventBus.EventBus.GetInstance().Subscribe<SCChip_RotorSwitchEvent>(0, RotorSwitchEventHandle);
            EventBus.EventBus.GetInstance().Subscribe<ArticleExitsEvent>(0, ArticleExitsEventHandle);
            EventBus.EventBus.GetInstance().Subscribe<ArticleDistEvent>(0, ArticleDistEventHandle);
        }

        private void ArticleDistEventHandle(IEvent @event, EventCallBackHandle callBack)
        {
            package.Cmd = IC_CMD.ARTICLE_DIST;
            commProxy.SendData(StructureToByte(package));
        }

        private void ArticleExitsEventHandle(IEvent @event, EventCallBackHandle callBack)
        {
            ChipStatus.GetInstance().ArticleExits = false;
            package.Cmd = IC_CMD.ARTICLE_EXITS;
            commProxy.SendData(StructureToByte(package));
        }

        private void StopMakeIceCreamEventHandle(IEvent @event, EventCallBackHandle callBack)
        {
            ChipStatus.GetInstance().ArticleDist = 0;
            package.Cmd = (@event as SCChip_SESwitchCreamEvent).IsOpen ? IC_CMD.OPEN_SE : IC_CMD.CLOSE_SE;
            package.Value = (byte)(@event as SCChip_SESwitchCreamEvent).SteeringEngine;
            commProxy.SendData(StructureToByte(package));
        }

        private void RotorSwitchEventHandle(IEvent @event, EventCallBackHandle callBack)
        {
            package.Cmd = IC_CMD.ROTOR;
            package.Value = (@event as SCChip_RotorSwitchEvent).TurnOn ? (byte)IC_ROTOR.OPEN_ROTOR : (byte)IC_ROTOR.CLOSE_ROTOR;
            commProxy.SendData(StructureToByte(package));
        }

        private void MakeIceCreamEventHandle(IEvent @event, EventCallBackHandle callBack)
        {
            switch ((@event as SCChip_MakeIceCreamEvent).SteeringEngine)
            {
                case IC_SE.SE_1:
                    ChipStatus.GetInstance().CompletedOpen_SE_1 = false;
                    break;
                case IC_SE.SE_2:
                    ChipStatus.GetInstance().CompletedOpen_SE_2 = false;
                    break;
                case IC_SE.SE_3:
                    ChipStatus.GetInstance().CompletedOpen_SE_3 = false;
                    break;
            }
            package.Cmd = IC_CMD.OPEN_SE;
            package.Value = (byte)(@event as SCChip_MakeIceCreamEvent).SteeringEngine;
            commProxy.SendData(StructureToByte(package));

            bool wait = true;
            DateTime waitTimeout = DateTime.Now.AddSeconds(3);
            while (wait)
            {
                wait = DateTime.Now < waitTimeout;
                if (wait)
                {
                    switch ((@event as SCChip_MakeIceCreamEvent).SteeringEngine)
                    {
                        case IC_SE.SE_1:
                            wait = !ChipStatus.GetInstance().CompletedOpen_SE_1;
                            break;
                        case IC_SE.SE_2:
                            wait = !ChipStatus.GetInstance().CompletedOpen_SE_2;
                            break;
                        case IC_SE.SE_3:
                            wait = !ChipStatus.GetInstance().CompletedOpen_SE_3;
                            break;
                    }
                }
                Thread.Sleep(10);
            }
            Thread.Sleep(2000);
            package.Cmd = IC_CMD.CLOSE_SE;
            package.Value = (byte)(@event as SCChip_MakeIceCreamEvent).SteeringEngine;
            commProxy.SendData(StructureToByte(package));

            wait = true;
            waitTimeout = DateTime.Now.AddSeconds(3);
            while (wait)
            {
                wait = DateTime.Now < waitTimeout;
                if (wait)
                {
                    switch ((@event as SCChip_MakeIceCreamEvent).SteeringEngine)
                    {
                        case IC_SE.SE_1:
                            wait = !ChipStatus.GetInstance().CompletedClose_SE_1;
                            break;
                        case IC_SE.SE_2:
                            wait = !ChipStatus.GetInstance().CompletedClose_SE_2;
                            break;
                        case IC_SE.SE_3:
                            wait = !ChipStatus.GetInstance().CompletedClose_SE_3;
                            break;
                    }
                }
                Thread.Sleep(10);
            }
        }

        private void TakeCupEventHandle(IEvent @event, EventCallBackHandle callBack)
        {
            switch ((@event as SCChip_TakeCupEvent).Cup)
            {
                case IC_CUP.CUP_ICECREAM:
                    ChipStatus.GetInstance().CompletedTake_CPU_CUP_ICECREAM = false;
                    break;
                case IC_CUP.CUP_COFFEE:
                    ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE = false;
                    break;
            }
            package.Cmd = IC_CMD.TAKE_CUP;
            package.Value = (byte)(@event as SCChip_TakeCupEvent).Cup;
            commProxy.SendData(StructureToByte(package));

            bool wait = true;
            var waitTimeout = DateTime.Now.AddSeconds(3);
            while (wait)
            {
                wait = DateTime.Now < waitTimeout;
                if (wait)
                {
                    switch ((@event as SCChip_TakeCupEvent).Cup)
                    {
                        case IC_CUP.CUP_ICECREAM:
                            wait = !ChipStatus.GetInstance().CompletedTake_CPU_CUP_ICECREAM;
                            break;
                        case IC_CUP.CUP_COFFEE:
                            wait = !ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE;
                            break;
                    }
                }
                Thread.Sleep(10);
            }
        }


        private byte[] StructureToByte(ICChipPackage structure)
        {
            structure.Header = 0xAA;
            structure.Sender = IC_SENDER.CONSOLE;
            structure.End = 0xBB;

            int size = Marshal.SizeOf(typeof(ICChipPackage));
            byte[] buffer = new byte[size];
            IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
            try
            {
                Marshal.StructureToPtr(structure, bufferIntPtr, true);
                Marshal.Copy(bufferIntPtr, buffer, 0, size);
            }
            finally
            {
                Marshal.FreeHGlobal(bufferIntPtr);
            }
            return buffer;
        }


    }
}
