using BPA.Utility;
using HBLConsole.Communication;
using HBLConsole.Service;
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 BPA.Utility.EventBus;

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

        /// <summary>
        /// 初始化
        /// </summary>
        internal void Init(SerialPortClient commProxy)
        {
            this.commProxy = commProxy;
            EventBus.GetInstance().Subscribe<TakeCupEvent>(TakeCupEventHandle);
            EventBus.GetInstance().Subscribe<MakeIceCreamEvent>(MakeIceCreamEventHandle);
            EventBus.GetInstance().Subscribe<SESwitchCreamEvent>(StopMakeIceCreamEventHandle);
            EventBus.GetInstance().Subscribe<RotorSwitchEvent>(RotorSwitchEventHandle);
        }

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

        private void RotorSwitchEventHandle(IEvent @event, EventCallBackHandle callBack)
        {
            package.Cmd = IC_CMD.ROTOR;
            package.Value = (@event as 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 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 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 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(1000);
            package.Cmd = IC_CMD.CLOSE_SE;
            package.Value = (byte)(@event as MakeIceCreamEvent).SteeringEngine;
            commProxy.SendData(StructureToByte(package));

            wait = true;
            waitTimeout = DateTime.Now.AddSeconds(3);
            while (wait)
            {
                wait = DateTime.Now < waitTimeout;
                if (wait)
                {
                    switch ((@event as 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 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 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 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;
        }


    }
}
