using Common;
using MegaRobo.C00225155.AppServer;
using MegaRobo.C00225155.AppServer.ExecuteWorks;
using MegaRobo.C00225155.ControlDevices;
using MegaRobo.C00225155.ControlDevices.Scanner;
using MegaRobo.C00225155.DataAccess;
using MegaRobo.C00225155.Entities;
using MegaRobo.C00225155.Entities.Entity_DB;
using MegaRobo.Connections;
using MegaRobo.Contract;
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 : ViewModelBaseEx
    {
        #region properties
        private IDataAccessService DataAccessService { set; get; }
        private WorkService _workService;
        private StationService_MaterialDosing StationService_Dose { 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 scanner1Service { get; set; }
        //protected ScannerService scanner2Service { get; set; }


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

        private ControlWorkStatus _pipette1Ml_Config_WorkStatus = new ControlWorkStatus();
        /// <summary>
        /// 配置站1ml移液枪工作状态
        /// </summary>
        public ControlWorkStatus Pipette1Ml_Config_WorkStatus
        {
            get => _pipette1Ml_Config_WorkStatus;
            set => SetProperty(ref _pipette1Ml_Config_WorkStatus, value);
        }
        public string Pipe1mlWarnText { get; set; }

        private ControlWorkStatus _Pipette5Ml_Config_WorkStatus = new ControlWorkStatus();
        public ControlWorkStatus Pipette5Ml_Config_WorkStatus
        {
            get => _Pipette5Ml_Config_WorkStatus;
            set => SetProperty(ref _Pipette5Ml_Config_WorkStatus, value);
        }
        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; }

        private ControlWorkStatus _MagneticStirWorkStatus = new ControlWorkStatus();
        public ControlWorkStatus MagneticStirWorkStatus
        {
            get => _MagneticStirWorkStatus;
            set => SetProperty(ref _MagneticStirWorkStatus, value);
        }

        public string MagneticStirWarnText { get; set; }

        private ControlWorkStatus _Scanner1WorkStatus = new ControlWorkStatus();
        public ControlWorkStatus Scanner1WorkStatus
        {
            get => _Scanner1WorkStatus;
            set => SetProperty(ref _Scanner1WorkStatus, value);
        }
        public string Scanner1WarnText { get; set; }

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

        private ControlWorkStatus _LiquidWeightWorkStatus = new ControlWorkStatus();
        public ControlWorkStatus LiquidWeightWorkStatus
        {
            get => _LiquidWeightWorkStatus;
            set => SetProperty(ref _LiquidWeightWorkStatus, value);
        }
        public string LiquidWeightWarnText { get; set; }

        private ControlWorkStatus _PowderWeightWorkStatus = new ControlWorkStatus();
        public ControlWorkStatus PowderWeightWorkStatus 
        { 
            get=>_PowderWeightWorkStatus; 
            set=>SetProperty(ref _PowderWeightWorkStatus,value); 
        }
        public string PowderWeightWarnText { get; set; }

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

        #region functions
        public DeviceStatusViewModel()
        {
            //PlcErrorList = new List<PLCErrorPoint>();
            //PlcErrorList.Add(new PLCErrorPoint() { ErrorAddress = "213.01", ErrorInfo = "122343w54" });
            //PlcErrorList.Add(new PLCErrorPoint() { ErrorAddress = "213.01", ErrorInfo = "122343w54" });
            //PlcErrorList.Add(new PLCErrorPoint() { ErrorAddress = "213.01", ErrorInfo = "122343w54" });
        }

        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());
            ////ramanSpetraService = this.CoreService.GetServiceInstance<RamanSpetraService>(DeviceNames.RamanSpectra.ToString());

            //scanner1Service = this.CoreService.GetServiceInstance<ScannerService>(DeviceNames.Scanner1.ToString());
            ////scanner2Service = this.CoreService.GetServiceInstance<ScannerService>(DeviceNames.Scanner2.ToString());

            StationService_Dose = this._workService.stationService_Dose;
            //StationService_Detect = this._workService.stationService_Detect;
        }

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

            this.StationService_Dose.OnScanPlcErrorListEvent += StationService_Config_OnScanPlcErrorListEvent;
            this.StationService_Dose.WorkStatusChanged += WorkServiceOnWorkStatusChanged;
            this._workService.MonitoringConnectionService.CheckCompleted += MonitoringConnectionServiceOnCheckCompleted;
        }

        /// <summary>
        /// 配置站的报警弹窗
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StationService_Config_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;
            //    }
            //    //else 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

    }
}
