using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.SerialPorts;
using TouchSocket.Sockets;
using 智慧物流软件系统.Views.Controls;

namespace 智慧物流软件系统
{
    public static class ScannerManager
    {
        public static List<Scanner> Scanners = new List<Scanner>();
        public static void InitScanners()
        {

            //获取当前配置文件的串口号
            var ScannerComList = LocalFile.Config.ScannerComList;
            //通过配置的串口号生成对象
            ScannerComList.ForEach(COM =>
            {
                try
                {
                    var client = new SerialPortClient();
                    //成功连接到端口
                    client.Connected = (client, e) =>
                    {
                        Logs.Write($"扫码枪{client.MainSerialPort.PortName},已成功连接！", LogsType.Scanner);
                        //初始化扫码枪对象
                        var Scanner = new Scanner()
                        {
                            SerialPortClient = (SerialPortClient)client,
                            //ScannerDisplayControl = new ScannerDisplayControl(client.MainSerialPort.PortName),
                            COM = client.MainSerialPort.PortName,
                            TempCode = string.Empty,
                        };
                        Scanners.Add(Scanner);
                        return EasyTask.CompletedTask;
                    };
                    client.Setup(new TouchSocket.Core.TouchSocketConfig()
                        .SetSerialPortOption(new SerialPortOption()
                        {
                            BaudRate = 9600,//波特率
                            DataBits = 8,//数据位
                            Parity = System.IO.Ports.Parity.None,//校验位
                            PortName = COM,
                            StopBits = System.IO.Ports.StopBits.One//停止位
                        }));

                    client.Connect(LocalFile.Config.ScannerTimeOut, new CancellationToken());
                }
                catch (Exception ex)
                {
                    Logs.Write($"初始化扫码枪异常！{ex.Message}", LogsType.Scanner);
                }
            });

        }
    }

    public class Scanner
    {
        public SerialPortClient SerialPortClient { get; set; }

        public ScannerDisplayControl ScannerDisplayControl { get; set; }

        /// <summary>
        /// 是否在入库模式中
        /// </summary>
        public bool IsInstoreMode { get; set; }

        public DateTime IsInstoreModeTime { get; set; } = DateTime.MinValue;
        /// <summary>
        /// 串口号
        /// </summary>
        public string COM { get; set; }
        /// <summary>
        /// 暂存的码
        /// </summary>
        public string TempCode { get; set; } = string.Empty;

        public string ShelfCode { get; set; } = string.Empty;

        public string ModulesStr { get; set; } = string.Empty;

        public string MatSn { get; set; } = string.Empty;

        public string InstoreUser { get; set; } = string.Empty;
    }
}
