using BPASmartClient.Helper;
using BPASmartClient.Message;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BPASmartClient.SerialPort
{
    public class JuicerHelper
    {

        //private volatile static JuicerHelper _Instance;
        //public static JuicerHelper GetInstance => _Instance ?? (_Instance = new JuicerHelper());
        //private JuicerHelper() { }

        private System.IO.Ports.SerialPort comPort = new System.IO.Ports.SerialPort();
        ConcurrentQueue<byte[]> SerialMessages = new ConcurrentQueue<byte[]>();
        public bool IsOpen => comPort.IsOpen;

        public bool Open(string portName, int baudRate)
        {
            while (!System.IO.Ports.SerialPort.GetPortNames().Contains(portName))
            {
                Thread.Sleep(1000);
            }
            while (!comPort.IsOpen)
            {
                comPort.PortName = portName;
                comPort.BaudRate = baudRate;
                comPort.DataBits = 8;
                comPort.Parity = Parity.None;
                comPort.StopBits = StopBits.One;
                comPort.ReadTimeout = 1000;
                comPort.WriteTimeout = 1000;
                //comPort.DataReceived += ComPort_DataReceived;
                comPort.RtsEnable = true;
                try
                {
                    comPort.Open();
                }
                catch (Exception ex)
                {
                    MessageLog.GetInstance.ShowEx(ex.ToString());
                    Thread.Sleep(5000);
                }
            }
            MessageLog.GetInstance.Show($"{portName} 串口打开成功");
            return comPort.IsOpen;
        }

        /// <summary>
        /// 开始制作
        /// </summary>
        /// <param name="aisle"></param>
        /// <returns></returns>
        public bool StartCook(byte aisle)
        {
            byte sum = (byte)(0x21 + aisle);
            byte checksum = (byte)~sum;
            checksum++;
            byte[] buffers = new byte[6] { 0x7E, 0x03, 0x00, 0x21, aisle, checksum };

            if (comPort.IsOpen)
            {
                comPort.Write(buffers, 0, buffers.Length);
                while (comPort.BytesToRead < 6)
                {
                    Thread.Sleep(1);
                }
                byte[] receive = new byte[comPort.BytesToRead];
                comPort.Read(receive, 0, receive.Length);
                if (receive.Length == 6)
                    return receive[4] == 1;
            }
            return false;
        }

        /// <summary>
        /// 获取果汁机状态
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public int[] GetDeviceStatus()
        {
            List<int> res = new List<int>();
            byte[] buffers = new byte[5] { 0x7E, 0x02, 0x00, 0x13, 0xED };
            if (comPort.IsOpen)
            {
                comPort.Write(buffers, 0, buffers.Length);
                DateTime newDate = DateTime.Now;
                while (comPort.BytesToRead < 33)
                {
                    Thread.Sleep(1);
                    if (DateTime.Now.Subtract(newDate).TotalSeconds >= 2) break;
                }
                byte[] receive = new byte[comPort.BytesToRead];
                comPort.Read(receive, 0, receive.Length);
                if (receive.Length == 33)
                {
                    res.Add(receive.Skip(24).Take(4).ToArray().BytesToInt());
                    res.Add(receive.Skip(28).Take(4).ToArray().BytesToInt());
                }
                return res.ToArray();
            }
            return default;
        }
    }
}
