using Common;
using MegaRobo.C00225155.AppServer;
using MegaRobo.C00225155.AppServer.ExecuteWorks;
using MegaRobo.C00225155.ControlDevices.Scanner;
using MegaRobo.C00225155.DataAccess;
using MegaRobo.C00225155.Entities;
using MegaRobo.C00225155.Entities.Entity_DB;
using MegaRobo.Connections;
using MegaRobo.ControlDevices;
using MegaRobo.PipetteTool.HamiltonConsole.PipetteDevices.HamiltonDevices;
using MegaRobo.WpfInfrastructure.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Pipette1ml = MegaRobo.PipetteTool.HamiltonConsole.PipetteDevices.HamiltonDevices;
using Pipette5ml = MegaRobo.PipetteTool.HamiltonConsole.PipetteDevices.Hamilton5mlDevices;

namespace MegaRobo.C00225155App.DevicesViewModels
{
    public class DeviceStatusViewModel_Detect : ViewModelBaseEx
    {
        public DeviceStatusViewModel_Detect()
        {

        }

        #region properties
        private IDataAccessService DataAccessService { set; get; }
        private WorkService _workService;
        //private StationService_Config StationService_Config { get; set; }
        private StationService_ReactionProcessing StationService_React { get; set; }

        //protected Pipette1ml.PipetteService pipetteService_1ml_Config { get; set; }
        //protected Pipette5ml.Pipette5mlService pipetteService_5ml_Config { get; set; }
        protected Pipette5ml.Pipette5mlService pipetteService_5ml_Detect { get; set; }
        protected ScannerService scanner2Service { get; set; }

        private List<PLCErrorPoint> plcErrorList;
        public List<PLCErrorPoint> PlcErrorList
        {
            get { return plcErrorList; }
            set { SetProperty(ref plcErrorList, value); }
        }

        /// <summary>
        /// 配置站1ml移液枪工作状态
        /// </summary>
        public ControlWorkStatus Pipette1Ml_Config_WorkStatus { get; set; }
        public string Pipe1mlWarnText { get; set; }

        public ControlWorkStatus Pipette5Ml_Config_WorkStatus { get; set; }
        public string Pipe5mlWarnText { get; set; }

        public ControlWorkStatus Pipette5Ml_Detect_WorkStatus { get; set; }
        public string Pipe5mlWarnText_Detect { get; set; }

        public ControlWorkStatus RamanWorkStatus { get; set; }
        public string RamanWarnText { get; set; }

        public ControlWorkStatus MagneticStirWorkStatus { get; set; }
        public string MagneticStirWarnText { get; set; }

        public ControlWorkStatus Scanner1WorkStatus { get; set; }
        public string Scanner1WarnText { get; set; }

        public ControlWorkStatus Scanner2WorkStatus { get; set; }
        public string Scanner2WarnText { get; set; }

        public ControlWorkStatus LiquidWeightWorkStatus { get; set; }
        public string LiquidWeightWarnText { get; set; }

        public ControlWorkStatus PowderWeightWorkStatus { get; set; }
        public string PowderWeightWarnText { get; set; }

        public ControlWorkStatus DetectWeightWorkStatus { get; set; }
        public string DetectWeightWarnText { get; set; }
        #endregion

        #region functions
        protected override void LoadServices()
        {
            base.LoadServices();
            this.DataAccessService = this.CoreService.GetServiceInstance<IDataAccessService>();
            this._workService = this.CoreService.GetServiceInstance<WorkService>();
            //pipetteService_1ml_Config = this.CoreService.GetServiceInstance<PipetteService>(DeviceNames.Pipette_Config_1ml.ToString());
            //pipetteService_5ml_Config = this.CoreService.GetServiceInstance<Pipette5ml.Pipette5mlService>(DeviceNames.Pipette_Config_5ml.ToString());
            //pipetteService_5ml_Detect = this.CoreService.GetServiceInstance<Pipette5ml.Pipette5mlService>(DeviceNames.Pipette_Detect_5ml.ToString());
            //scanner2Service = this.CoreService.GetServiceInstance<ScannerService>(DeviceNames.Scanner2.ToString());

            //StationService_Config = this._workService.stationService_Config;
            StationService_React = this._workService.stationService_React;
        }

        protected override void Register()
        {
            base.Register();

            this.StationService_React.OnScanPlcErrorListEvent += StationService_Detect_OnScanPlcErrorListEvent;
            this.StationService_React.WorkStatusChanged += WorkServiceOnWorkStatusChanged_Detect;
            this._workService.MonitoringConnectionService.CheckCompleted += MonitoringConnectionServiceOnCheckCompleted;
        }

        /// <summary>
        /// 检测站扫描错误信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StationService_Detect_OnScanPlcErrorListEvent(object sender, List<PLCErrorPoint> e)
        {
            //Application.Current.Dispatcher.Invoke(() =>
            //{
            //    // 检查是否有报警信息
            //    if (e != null && e.Count > 0)
            //    {
            //        var alarmPopup = new AlarmPopUpWindow(e);

            //        alarmPopup.ShowDialog();
            //    }
            //});
            if (e != null && PlcErrorList.Count != e.Count)
            {
                PlcErrorList = e;
            }
        }

        //private void WorkServiceOnWorkStatusChanged(object sender, ControlWorkStatus e)
        //{
        //    if (e.ComponentName == DeviceNames.Pipette_Config_1ml.ToString())
        //    {
        //        this.Pipette1Ml_Config_WorkStatus = e;
        //        this.OnPropertyChanged(nameof(this.Pipette1Ml_Config_WorkStatus));
        //        return;
        //    }
        //    if (e.ComponentName == DeviceNames.Pipette_Config_5ml.ToString())
        //    {
        //        this.Pipette5Ml_Config_WorkStatus = e;
        //        this.OnPropertyChanged(nameof(this.Pipette5Ml_Config_WorkStatus));
        //        return;
        //    }
        //    if (e.ComponentName == DeviceNames.MagneticStir.ToString())
        //    {
        //        this.MagneticStirWorkStatus = e;
        //        this.OnPropertyChanged(nameof(this.MagneticStirWorkStatus));
        //        return;
        //    }
        //    if (e.ComponentName == DeviceNames.Scanner1.ToString())
        //    {
        //        this.Scanner1WorkStatus = e;
        //        this.OnPropertyChanged(nameof(this.Scanner1WorkStatus));
        //        return;
        //    }
        //    if (e.ComponentName == DeviceNames.LiquidWeight.ToString())
        //    {
        //        this.LiquidWeightWorkStatus = e;
        //        this.OnPropertyChanged(nameof(this.LiquidWeightWorkStatus));
        //        return;
        //    }
        //    if (e.ComponentName == DeviceNames.PowderWeight.ToString())
        //    {
        //        this.PowderWeightWorkStatus = e;
        //        this.OnPropertyChanged(nameof(this.PowderWeightWorkStatus));
        //        return;
        //    }
        //}

        private void WorkServiceOnWorkStatusChanged_Detect(object sender, ControlWorkStatus e)
        {
        //    if (e.ComponentName == DeviceNames.Pipette_Detect_5ml.ToString())
        //    {
        //        this.Pipette5Ml_Detect_WorkStatus = e;
        //        this.OnPropertyChanged(nameof(this.Pipette5Ml_Detect_WorkStatus));
        //        return;
        //    }
        //    if (e.ComponentName == DeviceNames.RamanSpectra.ToString())
        //    {
        //        this.RamanWorkStatus = e;
        //        this.OnPropertyChanged(nameof(this.RamanWorkStatus));
        //        return;
        //    }
        //    if (e.ComponentName == DeviceNames.Scanner2.ToString())
        //    {
        //        this.Scanner2WorkStatus = e;
        //        this.OnPropertyChanged(nameof(this.Scanner2WorkStatus));
        //        return;
        //    }
        //    if (e.ComponentName == DeviceNames.DetectWeight.ToString())
        //    {
        //        this.DetectWeightWorkStatus = e;
        //        this.OnPropertyChanged(nameof(this.DetectWeightWorkStatus));
        //        return;
        //    }
        }

        private void MonitoringConnectionServiceOnCheckCompleted(object sender, EventArgs e)
        {
            var connList = this._workService.MonitoringConnectionService.ConnectionList;
            foreach (IConnectionAsync connItem in connList)
            {
                ////if (connItem.Name == nameof(DeviceNames.Pipette_Config_1ml))
                ////{
                ////    this.Pipe1mlWarnText = connItem.ConnectionState == ConnectionState.IsConnected ? null : "连接异常";
                ////    this.OnPropertyChanged(nameof(this.Pipe1mlWarnText));
                ////    continue;
                ////}
                ////else if (connItem.Name == nameof(DeviceNames.Pipette_Config_5ml))
                ////{
                ////    this.Pipe5mlWarnText = connItem.ConnectionState == ConnectionState.IsConnected ? null : "连接异常";
                ////    this.OnPropertyChanged(nameof(this.Pipe5mlWarnText));
                ////    continue;
                ////}
                //if (connItem.Name == nameof(DeviceNames.Pipette_Detect_5ml))
                //{
                //    this.Pipe5mlWarnText_Detect = connItem.ConnectionState == ConnectionState.IsConnected ? null : "连接异常";
                //    this.OnPropertyChanged(nameof(this.Pipe5mlWarnText_Detect));
                //    continue;
                //}
                //else if (connItem.Name == nameof(DeviceNames.RamanSpectra))
                //{
                //    this.RamanWarnText = connItem.ConnectionState == ConnectionState.IsConnected ? null : "连接异常";
                //    this.OnPropertyChanged(nameof(this.RamanWarnText));
                //    continue;
                //}
                ////else if (connItem.Name == nameof(DeviceNames.MagneticStir))
                ////{
                ////    this.MagneticStirWarnText = connItem.ConnectionState == ConnectionState.IsConnected ? null : "连接异常";
                ////    this.OnPropertyChanged(nameof(this.MagneticStirWarnText));
                ////    continue;
                ////}
                ////else if (connItem.Name == nameof(DeviceNames.Scanner1))
                ////{
                ////    this.Scanner1WarnText = connItem.ConnectionState == ConnectionState.IsConnected ? null : "连接异常";
                ////    this.OnPropertyChanged(nameof(this.Scanner1WarnText));
                ////    continue;
                ////}
                //else if (connItem.Name == nameof(DeviceNames.Scanner2))
                //{
                //    this.Scanner2WarnText = connItem.ConnectionState == ConnectionState.IsConnected ? null : "连接异常";
                //    this.OnPropertyChanged(nameof(this.Scanner2WarnText));
                //    continue;
                //}
                ////else if (connItem.Name == nameof(DeviceNames.LiquidWeight))
                ////{
                ////    this.LiquidWeightWarnText = connItem.ConnectionState == ConnectionState.IsConnected ? null : "连接异常";
                ////    this.OnPropertyChanged(nameof(this.LiquidWeightWarnText));
                ////    continue;
                ////}
                ////else if (connItem.Name == nameof(DeviceNames.PowderWeight))
                ////{
                ////    this.PowderWeightWarnText = connItem.ConnectionState == ConnectionState.IsConnected ? null : "连接异常";
                ////    this.OnPropertyChanged(nameof(this.PowderWeightWarnText));
                ////    continue;
                ////}
                //else if (connItem.Name == nameof(DeviceNames.DetectWeight))
                //{
                //    this.DetectWeightWarnText = connItem.ConnectionState == ConnectionState.IsConnected ? null : "连接异常";
                //    this.OnPropertyChanged(nameof(this.DetectWeightWarnText));
                //    continue;
                //}
            }
        }
        #endregion

    }
}
