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

namespace HBLDevice.Coffee
{
    /// <summary>
    /// 咖啡机
    /// </summary>
    public class CoffeeMachine 
    {
        //通讯代理
        SerialPortClient commProxy = null;
        //数据仓库
        private DataStorage<byte> dataStorage = new DataStorage<byte>();
        //指令组装
        private CommandHandler commandHandler = new CommandHandler();
        //主线程运行标识
        private bool running = false;
        //是否下发指令，主线程等待
        private bool free = true;

        private DrCoffeeStatus drCoffeeStatus;
        /// <summary>
        /// 咖啡机状态
        /// </summary>
        public DrCoffeeStatus CurrentCoffeeStatus
        {
            get { return drCoffeeStatus; }
            set
            {
                if (drCoffeeStatus != value)
                {
                    drCoffeeStatus = value;
                    CoffeeStatusChanged?.Invoke(value);
                }
            }
        }
        private DrCoffeeAppStatus coffeeAppStatus;
        /// <summary>
        /// 应用状态
        /// </summary>
        public DrCoffeeAppStatus CurrentCoffeeAppStatus
        {
            get { return coffeeAppStatus; }
            set
            {
                if (coffeeAppStatus != value)
                {
                    coffeeAppStatus = value;
                    CoffeeAppStatusChanged?.Invoke(value);
                }
            }
        }

        public Action<string> SendCallback;
        public Action<string> ReciveCallback;

        /// <summary>
        /// 咖啡机状态改变回调
        /// </summary>
        public Action<DrCoffeeStatus> CoffeeStatusChanged;
        /// <summary>
        /// 应用状态改变回调
        /// </summary>
        public Action<DrCoffeeAppStatus> CoffeeAppStatusChanged;

        public CoffeeMachine(string portName, BaudRates baud)
        {
            commProxy = new SerialPortClient(portName, baud);
            commProxy.SetDataStorage(dataStorage);
            commandHandler.Init(commProxy);
            commandHandler.PauseAsk = delegate (bool pause)
            {
                free = !pause;
            };

        }

        /// <summary>
        /// 主线程开始运行
        /// </summary>
        public void Start()
        {
            commProxy.Start();
            running = true;
            MainLoop();
        }

        /// <summary>
        /// 停止运行
        /// </summary>
        public void Stop()
        {
            commProxy.Stop();
            running = false;
        }

        ///// <summary>
        ///// 下单制作
        ///// </summary>
        ///// <param name="drinksCode">饮品代码</param>
        //public void PlaceOrder(DrCoffeeDrinksCode drinksCode)
        //{
        //    free = false;
        //    Thread.Sleep(200);
        //    commProxy.SendData(commandHandler.GetDrinksOrder(drinksCode));
        //    SendCallback?.Invoke(BitConverter.ToString(commandHandler.GetDrinksOrder(drinksCode)));

        //    Thread.Sleep(200);
        //    free = true;
        //}

        /// <summary>
        /// 主循环，循环询问状态
        /// </summary>
        private void MainLoop()
        {
            ThreadOperate.GetInstance.StartLong(new Action(() =>
            {
                if (free)
                {
                    commProxy.SendData(commandHandler.GetStatusAsk());
                    SendCallback?.Invoke(BitConverter.ToString(commandHandler.GetStatusAsk()));
                }
                Thread.Sleep(200);
            }), "咖啡机询问线程");
            //Executer.GetInstance().Start(() =>
            //{
            //    while (running)
            //    {
            //        if (free)
            //        {
            //            commProxy.SendData(commandHandler.GetStatusAsk());
            //            SendCallback?.Invoke(BitConverter.ToString(commandHandler.GetStatusAsk()));
            //        }
            //        Thread.Sleep(200);
            //    }
            //}, "咖啡机询问线程");


            ThreadOperate.GetInstance.StartLong(new Action(() =>
            {
                List<byte> temp = new List<byte>();
                //一系列解包
                while (dataStorage.GetSize() > 0)
                {
                    byte item = dataStorage.GetData();
                    if (DrCoffee.HEADER == item)
                    {
                        if (temp.Count == DrCoffee.LENGTH - 1)
                        {
                            temp.Add(item);
                            var package = DrCoffee.UnPack(temp.ToArray());
                            ReciveCallback?.Invoke(BitConverter.ToString(temp.ToArray()));
                            temp.Clear();
                            MorkCStatus.GetInstance().ProcessPackage(package);
                        }
                        else
                        {
                            temp.Clear();
                            temp.Add(item);
                        }
                        continue;
                    }
                    else
                    {
                        if (temp.Count == 1 && item != DrCoffee.LENGTH)
                        {
                            temp.Clear();
                            continue;
                        }
                        temp.Add(item);
                    }
                }
                Thread.Sleep(5);
            }), "咖啡机解析线程");

            //Executer.GetInstance().Start(() =>
            //{
            //    while (running)
            //    {
            //        List<byte> temp = new List<byte>();
            //        //一系列解包
            //        while (dataStorage.GetSize() > 0)
            //        {
            //            byte item = dataStorage.GetData();
            //            if (DrCoffee.HEADER == item)
            //            {
            //                if (temp.Count == DrCoffee.LENGTH - 1)
            //                {
            //                    temp.Add(item);
            //                    var package = DrCoffee.UnPack(temp.ToArray());
            //                    ReciveCallback?.Invoke(BitConverter.ToString(temp.ToArray()));
            //                    temp.Clear();
            //                    MorkCStatus.GetInstance().ProcessPackage(package);
            //                }
            //                else
            //                {
            //                    temp.Clear();
            //                    temp.Add(item);
            //                }
            //                continue;
            //            }
            //            else
            //            {
            //                if (temp.Count == 1 && item != DrCoffee.LENGTH)
            //                {
            //                    temp.Clear();
            //                    continue;
            //                }
            //                temp.Add(item);
            //            }
            //        }
            //        Thread.Sleep(5);
            //    }
            //}, "咖啡机解析线程");
        }

        
    }
}
