using Common;
using Common.Models;
using CommunityToolkit.Mvvm.Messaging;
using MegaRobo.C00225155.ControlDevices;
using MegaRobo.C00225155.ControlDevices.Scanner;
using MegaRobo.C00225155.ControlDevices.WeightDevice;
using MegaRobo.C00225155.Entities;
using MegaRobo.C00225155.Entities.Entity_DB;
using MegaRobo.Contract;
using MegaRobo.Logger;
using MegaRobo.PipetteTool.HamiltonConsole.PipetteDevices.HamiltonDevices;

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Pipette1ml = MegaRobo.PipetteTool.HamiltonConsole.PipetteDevices.HamiltonDevices;
using Pipette5ml = MegaRobo.PipetteTool.HamiltonConsole.PipetteDevices.Hamilton5mlDevices;
using Task = System.Threading.Tasks.Task;
using MegaRobo.ControlDevices.Models;
using NetTaste;
using System.Threading.Tasks;
using MegaRobo.Contract.Abstractions;
using MegaRobo.ControlDevices;
using MegaRobo.Entities;
using Nito.AsyncEx;

namespace MegaRobo.C00225155.AppServer.ExecuteWorks
{
    public partial class StationService_ReactionProcessing : StationServiceBase
    {
        private IModbusClientExtend _modbusClient_Plc2;
        public StationProperty StationProp = new StationProperty();

        #region 服务模组

        protected ScannerService scannerService_react;
        private PipetteDictionary pipetteDictionary { get; set; }
        private Pipette1ml.PipetteService pipetteService_1ml { get; set; }
       
        #endregion

        #region events
       
        #endregion

        public StationService_ReactionProcessing(ICoreService coreService, WorkService workService, DeviceNames plcName, string station) : base(coreService, workService, plcName, station)
        {
            //Initial();
        }

        protected override void LoadServices()
        {
            base.LoadServices();
            scannerService_react = this.CoreService.GetServiceInstance<ScannerService>(DeviceNames.Scanner_ReactRack.ToString());
            pipetteService_1ml = this.CoreService.GetServiceInstance<Pipette1ml.PipetteService>(DeviceNames.Pipette_React_1ml.ToString());
            pipetteDictionary = new PipetteDictionary();

            //_modbusClient_Plc2 = this.CoreService.GetServiceInstance<IModbusClientExtend>(DeviceNames.Plc2.ToString());
            if (this.ModbusClient != null)
            {
                //this.ModbusClient.Command.WriteValue<ushort>(PlcPoints_React.W_BoxPlateUploadFinish, 0);
                //this.ModbusClient.Command.WriteValue<ushort>(PlcPoints_React.W_DnLoad, 0);
                //this.ModbusClient.Command.WriteValue<ushort>(PlcPoints_React.W_BoxPlatePos, 0);
                //this.ModbusClient.Command.WriteValue<ushort>(PlcPoints_React.W_BoxPlatePutFinish, 0);
                //this.ModbusClient.Command.WriteValue<ushort>(PlcPoints_React.W_BoxPlateCave1have, 0);
                //this.ModbusClient.Command.WriteValue<ushort>(PlcPoints_React.W_BoxPlateCave2have, 0);
            }
        }
       
        protected override async void ModbusClientBaseOnScannedEvent(object sender, EventArgs e)
        {
            base.ModbusClientBaseOnScannedEvent(sender, e);
           
            this.StationProp.ManualResetEvent.WaitOne(); //暂停标志

     
        }


        /// <summary>
        /// 计算枪头吸液次数
        /// </summary>
        /// <param name="volumn"></param>
        /// <returns></returns>
        private List<double> CalculateAbsorbCount(double volumn)
        {
            List<double> counts = new List<double>();
            int count = (int)(volumn / 5000);
            for (int i = 0; i < count; i++)
            {
                counts.Add(5000);
            }
            if (volumn % 5000 > 0)
            {
                counts.Add(volumn % 5000);
            }
            return counts;
        }

        private async Task SetObstructFlag(string name, bool value)
        {
            try
            {
                if (this.StationProp.ObstructFlagList.TryGetValue(name, out var targetFlag))
                {
                    if (targetFlag != null)
                    {
                        lock (targetFlag)
                        {
                            targetFlag.flag = value;
                        }
                    }
                }
            }
            catch (Exception)
            {
                //throw;
            }
        }

        private async Task<bool> ReadObstructFlag(string name, bool value = true, bool bAny = false)
        {
            try
            {
                if (!bAny)
                {
                    if (this.StationProp.ObstructFlagList.TryGetValue(name, out var targetFlag))
                    {
                        if (targetFlag != null)
                        {
                            lock (targetFlag)
                            {
                                return targetFlag.flag == value;
                            }
                        }

                    }
                }
                else if (bAny)
                {
                    foreach (var pair in this.StationProp.ObstructFlagList)
                    {
                        if (pair.Key.Contains(name))
                        {
                            lock (pair.Value)
                            {
                                if (pair.Value.flag == value)
                                {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
            return false;
        }

    }
}
