using BPA.SingleDevice.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using BPA.Communication;
using BPA.Model;
using BPA.Helper;
using System.Threading;
using Microsoft.Extensions.FileSystemGlobbing.Internal.PathSegments;

namespace BPA.SingleDevice.Business
{
    public class MainControl : IMainControl
    {
        private volatile static MainControl _Instance;
        public static IMainControl GetInstance => _Instance ?? (_Instance = new MainControl());
        private MainControl() { }

        /// <summary>
        /// 通讯集合（key=1-4代表1号味魔方到4号味魔方，5为输送带的控制）
        /// </summary>
        ConcurrentDictionary<EDeviceType, IDeviceAbs> Comm { get; set; } = new ConcurrentDictionary<EDeviceType, IDeviceAbs>();

        private void CommInit()
        {
            Enum.GetNames(typeof(EDeviceType)).ToList().ForEach(x => { Comm.TryAdd(x.ToEnum<EDeviceType>(), new DeviceControl()); });
            Comm[EDeviceType.WeimoCube1].SetPar("192.168.1.100", EDeviceType.WeimoCube1);
            Comm[EDeviceType.WeimoCube2].SetPar("192.168.1.100", EDeviceType.WeimoCube2);
            Comm[EDeviceType.WeimoCube3].SetPar("192.168.1.100", EDeviceType.WeimoCube3);
            Comm[EDeviceType.WeimoCube4].SetPar("192.168.1.100", EDeviceType.WeimoCube4);
            Comm[EDeviceType.Conveyor].SetPar("192.168.1.100", EDeviceType.Conveyor);
            Task.Run(() =>
            {
                Parallel.ForEach(Comm, (s) => { s.Value.Stop(); });
            });
        }

        public void Start()
        {
            CommInit();
            TaskManage.GetInstance.StartLong(() =>
            {
                while (GlobalData.GoodsModels.Count > 0 && !Comm[EDeviceType.Conveyor].AllowBatching)
                {
                    Task.Run(() =>
                    {
                        if (GlobalData.GoodsModels.TryDequeue(out GoodsModel gm))
                        {
                            //对设备进行分组，筛选出对应设备所需要的料仓中的原料信息
                            var res = gm.RawMaters.GroupBy(p => p.DeviceNum).ToDictionary(p => p.Key, p => p.ToList());
                            res.ToList().ForEach(item =>
                            {
                                int index = item.Key - 1;
                                if (index >= 0 && index < Comm[EDeviceType.Conveyor].DeviceStationDetection.Length)
                                {
                                    if (Comm[EDeviceType.Conveyor].DeviceStationDetection[index])
                                    {
                                        Comm[EDeviceType.Conveyor].ConveyorControl(false);
                                        ushort[] weights = new ushort[14];
                                        item.Value.ForEach(s =>
                                        {
                                            int tempIndex = s.WarehouseNum - 1;
                                            if (tempIndex >= 0 && tempIndex < weights.Length)
                                            {
                                                weights[tempIndex] = s.Weight;
                                            }
                                        });
                                        Comm[(EDeviceType)item.Key].WriteControl(weights);
                                    }
                                }
                            });
                        }
                    });
                }
                Thread.Sleep(10);
            }, "配料任务", true);
        }

        public void Stop()
        {

        }
    }
}
