using MWS.Data.InterFace;
using MWS.Data.Manager;
using MWS.Domain.Common;
using MWS.Domain.Tasks.Weighing;
using MWS.Model;
using MWS.Model.Model;
using System.Collections.Generic;
using System.Threading;

namespace MWS.Domain.Tasks
{
    public class WeighSaveMain
    {
        private IDevice deviceManager;
        private List<Device> devices;


        public WeighSaveMain()
        {
            deviceManager = new DeviceManager();
            devices = deviceManager.GetAll();
            var indicatorConfig = ConfigHelper.GetIndicatorConfig();
            var systemConfig = ConfigHelper.GetSystemConfig();
            ThreadManager(indicatorConfig, systemConfig);
          
        }

        private void ThreadManager(IndicatorConfigModel indicatorConfig, SystemConfig systemConfig)
        {
            var readDataInterval = ConfigHelper.GetSystemConfig()?.DataReadInterval ?? 100;

            switch (indicatorConfig.Type)
            {
                case 1:
                    foreach (var device in devices)
                    {
                        //QCX_H4C_S S = new QCX_H4C_S(readDataInterval, systemConfig);
                        //ThreadPool.QueueUserWorkItem(new WaitCallback(S.Listen), device);

                        ShunZhouSocketClient shunZhouSocketClient = new ShunZhouSocketClient(readDataInterval,systemConfig,device.Ip,(ushort)device.Port,18, device);
                        //socketClient.OnSocketDataArrival += SocketClient_OnSocketDataArrival;
                        //socketClient.OnSocketConnected += SocketClient_OnSocketConnected;
                        //socketClient.OnSocketDisconnected += SocketClient_OnSocketDisconnected;
                        //socketClient.OnSocketCheckStateChanged += SocketClient_OnSocketCheckStateChanged;
                        //_ = socketClient.ConnectServer();
                        _ = shunZhouSocketClient.ConnectServer();
                        //ThreadPool.QueueUserWorkItem(new WaitCallback(await shunZhouSocketClient.ConnectServer));
                    }
                    break;
                case 2:
                    foreach (var device in indicatorConfig.Devices)
                    {
                        QCX_H4C_C11 C11 = new QCX_H4C_C11(readDataInterval, systemConfig);
                        ThreadPool.QueueUserWorkItem(new WaitCallback(C11.Listen), device);
                    }
                    break;
                case 3:
                    break;
            }
        }
        private static void SocketClient_OnSocketCheckStateChanged(int id, bool state)
        {
            //Console.WriteLine("发射设备：" + id + ", 状态：" + (state ? "正常" : "异常"));
        }

        private static async void SocketClient_OnSocketDisconnected()
        {
            //Console.WriteLine("连接服务断开");
            //await shunZhouSocketClient.ConnectServer();
        }

        private static void SocketClient_OnSocketConnected()
        {
            //Console.WriteLine("连接服务成功");
        }

        //private static void SocketClient_OnSocketDataArrival(ReceviceDataModel data)
        //{
        //    var dataTemp = tempDatas.FirstOrDefault(c => c.SourceId == data.SourceId);
        //    if (dataTemp != null)
        //    {
        //        dataTemp.NetWeight = (ushort)(data.NetWeight / 10);
        //    }
        //    else
        //    {
        //        tempDatas.Add(new SendDataModel(data.SourceId, (ushort)(data.NetWeight / 10)));
        //    }
        //}
    }
}
