using Aitex.Core.Common.DeviceData;
using Aitex.Core.Util;
using Aitex.Core.Utilities;
using Caliburn.Micro;
using MECF.Framework.UI.Client.CenterViews.Editors.Recipe;
using MECF.Framework.UI.Client.ClientBase;
using OpenSEMI.ClientBase;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Aitex.Core.UI.MVVM;
using System.Windows;
using SicUI.Controls;
using System.Windows.Media;
using Aitex.Core.Common.DeviceData.IoDevice;
using MECF.Framework.Common.Account.Permissions;
using MECF.Framework.UI.Client.CenterViews.Editors;
using MECF.Framework.Common.Aitex.Core.Common.DeviceData;

namespace SicUI.Models.PMs
{
    public class PMOperationViewModel : SicModuleUIViewModelBase, ISupportMultipleSystem
    {
        [Subscription("Status")]
        public string Status { get; set; }

        [Subscription("IsService")]
        public bool IsService { get; set; }

        public double Opacity { get; set; }
        public bool TMAEnable { get; set; }


        public bool Mfc5IsInstalled { get; set; }

        public string CarbonSource { get; set; }
      

        #region Enable
        //维护模式
        public bool IsServiceMode => (Status == "Idle" || Status == "Safety" || Status == "VacIdle" || Status == "ProcessIdle" || Status == "Error" || Status == "ServiceIdle") && !IsOnline;

        public bool IsEnableGasMap => (Status == "Idle" || Status == "VacIdle" || Status == "ServiceIdle") && !IsOnline;

        public bool EnableProcessIdle => (Status == "Idle" || Status == "VacIdle" || Status == "ProcessIdle" || Status == "Safety");

        public bool VacIdleEnable => (Status == "Idle" || Status == "VacIdle" || Status == "ProcessIdle" || Status == "Safety");

        public bool EnableIdleRoutine => (Status == "Idle" || Status == "Safety" || Status == "VacIdle") && !IsOnline;

        public bool EnableIdle => (Status == "ProcessIdle" || Status == "VacIdle" || Status == "Safety" || Status == "Idle" || Status == "ServiceIdle") && !IsOnline;

        public bool EnableService => (Status == "Idle" || Status == "Safety" || Status == "VacIdle" || Status == "Error") && !IsOnline;

        public bool EnableExchangeMo => (Status == "Idle" || Status == "Safety" || Status == "VacIdle") && !IsOnline && IsService;

        public string LableStatue => IsService ? "Service" : Status;

        //LeakCheck后只能执行ATM Idle,其他Routine按钮不允许执行
        private bool EnableLeakCheck = true;

        public SolidColorBrush StatueLabelColor
        {
            get
            {
                if (IsService)
                {
                    return new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 50, 205, 50));
                }
                else
                {
                    return new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 255, 255, 255));
                }
            }
        }


        public Visibility VisbleOnlyIdle
        {
            get
            {
                return IsService ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public Visibility VisibilityMfc5 => Mfc5IsInstalled ? Visibility.Visible : Visibility.Collapsed;
        

        #endregion

        #region MFC



        //PT4Data.OpenDegree > 




        [Subscription("Mfc1.DeviceData")]
        public AITMfcData Mfc1Data { get; set; }

        [Subscription("Mfc3.DeviceData")]
        public AITMfcData Mfc3Data { get; set; }

        [Subscription("Mfc4.DeviceData")]
        public AITMfcData Mfc4Data { get; set; }

        [Subscription("Mfc6.DeviceData")]
        public AITMfcData Mfc6Data { get; set; }

        [Subscription("Mfc7.DeviceData")]
        public AITMfcData Mfc7Data { get; set; }

        [Subscription("Mfc8.DeviceData")]
        public AITMfcData Mfc8Data { get; set; }

        [Subscription("Mfc9.DeviceData")]
        public AITMfcData Mfc9Data { get; set; }

        [Subscription("Mfc10.DeviceData")]
        public AITMfcData Mfc10Data { get; set; }

        [Subscription("Mfc11.DeviceData")]
        public AITMfcData Mfc11Data { get; set; }

        [Subscription("Mfc12.DeviceData")]
        public AITMfcData Mfc12Data { get; set; }

        [Subscription("Mfc13.DeviceData")]
        public AITMfcData Mfc13Data { get; set; }

        [Subscription("Mfc14.DeviceData")]
        public AITMfcData Mfc14Data { get; set; }

        [Subscription("Mfc15.DeviceData")]
        public AITMfcData Mfc15Data { get; set; }

        [Subscription("Mfc16.DeviceData")]
        public AITMfcData Mfc16Data { get; set; }

        [Subscription("Mfc17.DeviceData")]
        public AITMfcData Mfc17Data { get; set; }

        [Subscription("Mfc18.DeviceData")]
        public AITMfcData Mfc18Data { get; set; }

        [Subscription("Mfc19.DeviceData")]
        public AITMfcData Mfc19Data { get; set; }

        [Subscription("Mfc20.DeviceData")]
        public AITMfcData Mfc20Data { get; set; }

        [Subscription("Mfc21.DeviceData")]
        public AITMfcData Mfc21Data { get; set; }

        [Subscription("Mfc22.DeviceData")]
        public AITMfcData Mfc22Data { get; set; }

        [Subscription("Mfc23.DeviceData")]
        public AITMfcData Mfc23Data { get; set; }

        [Subscription("Mfc24.DeviceData")]
        public AITMfcData Mfc24Data { get; set; }

        [Subscription("Mfc25.DeviceData")]
        public AITMfcData Mfc25Data { get; set; }

        [Subscription("Mfc26.DeviceData")]
        public AITMfcData Mfc26Data { get; set; }

        [Subscription("Mfc27.DeviceData")]
        public AITMfcData Mfc27Data { get; set; }

        [Subscription("Mfc28.DeviceData")]
        public AITMfcData Mfc28Data { get; set; }

        [Subscription("Mfc29.DeviceData")]
        public AITMfcData Mfc29Data { get; set; }

        [Subscription("Mfc31.DeviceData")]
        public AITMfcData Mfc31Data { get; set; }

        [Subscription("Mfc32.DeviceData")]
        public AITMfcData Mfc32Data { get; set; }

        [Subscription("Mfc33.DeviceData")]
        public AITMfcData Mfc33Data { get; set; }

        [Subscription("Mfc35.DeviceData")]
        public AITMfcData Mfc35Data { get; set; }

        [Subscription("Mfc36.DeviceData")]
        public AITMfcData Mfc36Data { get; set; }

        [Subscription("Mfc37.DeviceData")]
        public AITMfcData Mfc37Data { get; set; }

        [Subscription("Mfc38.DeviceData")]
        public AITMfcData Mfc38Data { get; set; }

        [Subscription("Mfc40.DeviceData")]
        public AITMfcData Mfc40Data { get; set; }


        #endregion

        #region Pressure
        [Subscription("Pressure1.DeviceData")]
        public AITPressureMeterData PC1 { get; set; }

        [Subscription("Pressure2.DeviceData")]
        public AITPressureMeterData PC2 { get; set; }
        [Subscription("Pressure3.DeviceData")]
        public AITPressureMeterData PC3 { get; set; }


        [Subscription("Pressure4.DeviceData")]
        public AITPressureMeterData PC4 { get; set; }
        [Subscription("Pressure5.DeviceData")]
        public AITPressureMeterData PC5 { get; set; }
        [Subscription("Pressure6.DeviceData")]
        public AITPressureMeterData PC6 { get; set; }

        [Subscription("Pressure7.DeviceData")]
        public AITPressureMeterData PC7 { get; set; }

        [Subscription("Pressure8.DeviceData")]
        public AITPressureMeterData PC8 { get; set; }
        [Subscription("Pressure9.DeviceData")]
        public AITPressureMeterData PC9 { get; set; }

        [Subscription("PT1.DeviceData")]
        public AITPressureMeterData ChamPress { get; set; }

        [Subscription("PT2.DeviceData")]
        public AITPressureMeterData ForelinePress { get; set; }

        public string ChamPressFeedBack
        {
            get { return ChamPress.FeedBack.ToString(ChamPress.FormatString); }
            set { }
        }
        #endregion


        #region Valve
        [Subscription("V18.DeviceData")]
        public AITValveData V18 { get; set; }

        [Subscription("V18s.DeviceData")]
        public AITValveData V18s { get; set; }

        [Subscription("V19.DeviceData")]
        public AITValveData V19 { get; set; }

        [Subscription("V20.DeviceData")]
        public AITValveData V20 { get; set; }

        [Subscription("V20s.DeviceData")]
        public AITValveData V20s { get; set; }

        [Subscription("V21.DeviceData")]
        public AITValveData V21 { get; set; }

        [Subscription("V21s.DeviceData")]
        public AITValveData V21s { get; set; }

        [Subscription("V22.DeviceData")]
        public AITValveData V22 { get; set; }

        [Subscription("V23.DeviceData")]
        public AITValveData V23 { get; set; }

        [Subscription("V23s.DeviceData")]
        public AITValveData V23s { get; set; }

        [Subscription("V25.DeviceData")]
        public AITValveData V25 { get; set; }

        [Subscription("V27.DeviceData")]
        public AITValveData V27 { get; set; }

        [Subscription("V31.DeviceData")]
        public AITValveData V31 { get; set; }

        [Subscription("V32.DeviceData")]
        public AITValveData V32 { get; set; }

        [Subscription("V33.DeviceData")]
        public AITValveData V33 { get; set; }

        [Subscription("V33s.DeviceData")]
        public AITValveData V33s { get; set; }

        [Subscription("V35.DeviceData")]
        public AITValveData V35 { get; set; }

        [Subscription("V36.DeviceData")]
        public AITValveData V36 { get; set; }

        [Subscription("V37.DeviceData")]
        public AITValveData V37 { get; set; }

        [Subscription("V37s.DeviceData")]
        public AITValveData V37s { get; set; }

        [Subscription("V39.DeviceData")]
        public AITValveData V39 { get; set; }

        [Subscription("V39s.DeviceData")]
        public AITValveData V39s { get; set; }

        [Subscription("V40.DeviceData")]
        public AITValveData V40 { get; set; }

        [Subscription("V40s.DeviceData")]
        public AITValveData V40s { get; set; }

        [Subscription("V41.DeviceData")]
        public AITValveData V41 { get; set; }

        [Subscription("V42.DeviceData")]
        public AITValveData V42 { get; set; }

        [Subscription("V43.DeviceData")]
        public AITValveData V43 { get; set; }

        [Subscription("V43s.DeviceData")]
        public AITValveData V43s { get; set; }

        [Subscription("V45.DeviceData")]
        public AITValveData V45 { get; set; }

        [Subscription("V46.DeviceData")]
        public AITValveData V46 { get; set; }

        [Subscription("V46s.DeviceData")]
        public AITValveData V46s { get; set; }

        [Subscription("V47.DeviceData")]
        public AITValveData V47 { get; set; }

        [Subscription("V47s.DeviceData")]
        public AITValveData V47s { get; set; }

        [Subscription("V48.DeviceData")]
        public AITValveData V48 { get; set; }

        [Subscription("V48s.DeviceData")]
        public AITValveData V48s { get; set; }

        [Subscription("V49.DeviceData")]
        public AITValveData V49 { get; set; }

        [Subscription("V50.DeviceData")]
        public AITValveData V50 { get; set; }

        [Subscription("V50s.DeviceData")]
        public AITValveData V50s { get; set; }

        [Subscription("V51.DeviceData")]
        public AITValveData V51 { get; set; }

        [Subscription("V51s.DeviceData")]
        public AITValveData V51s { get; set; }

        [Subscription("V52.DeviceData")]
        public AITValveData V52 { get; set; }

        [Subscription("V52s.DeviceData")]
        public AITValveData V52s { get; set; }

        [Subscription("V53.DeviceData")]
        public AITValveData V53 { get; set; }

        [Subscription("V53s.DeviceData")]
        public AITValveData V53s { get; set; }

        [Subscription("V54.DeviceData")]
        public AITValveData V54 { get; set; }

        [Subscription("V54s.DeviceData")]
        public AITValveData V54s { get; set; }

        [Subscription("V55.DeviceData")]
        public AITValveData V55 { get; set; }

        [Subscription("V56.DeviceData")]
        public AITValveData V56 { get; set; }

        [Subscription("V58.DeviceData")]
        public AITValveData V58 { get; set; }

        [Subscription("V58s.DeviceData")]
        public AITValveData V58s { get; set; }

        [Subscription("V59.DeviceData")]
        public AITValveData V59 { get; set; }

        [Subscription("V59s.DeviceData")]
        public AITValveData V59s { get; set; }

        [Subscription("V60.DeviceData")]
        public AITValveData V60 { get; set; }

        [Subscription("V61.DeviceData")]
        public AITValveData V61 { get; set; }

        [Subscription("V62.DeviceData")]
        public AITValveData V62 { get; set; }

        [Subscription("V63.DeviceData")]
        public AITValveData V63 { get; set; }

        [Subscription("V66.DeviceData")]
        public AITValveData V66 { get; set; }

        [Subscription("V67.DeviceData")]
        public AITValveData V67 { get; set; }

        [Subscription("V68.DeviceData")]
        public AITValveData V68 { get; set; }

        [Subscription("V69.DeviceData")]
        public AITValveData V69 { get; set; }

        [Subscription("V70.DeviceData")]
        public AITValveData V70 { get; set; }

        [Subscription("V72.DeviceData")]
        public AITValveData V72 { get; set; }

        [Subscription("V73.DeviceData")]
        public AITValveData V73 { get; set; }

        [Subscription("V74.DeviceData")]
        public AITValveData V74 { get; set; }

        [Subscription("V75.DeviceData")]
        public AITValveData V75 { get; set; }

        [Subscription("V76.DeviceData")]
        public AITValveData V76 { get; set; }

        [Subscription("V87.DeviceData")]
        public AITValveData V87 { get; set; }

        [Subscription("V88.DeviceData")]
        public AITValveData V88 { get; set; }

        [Subscription("V89.DeviceData")]
        public AITValveData V89 { get; set; }

        [Subscription("V90.DeviceData")]
        public AITValveData V90 { get; set; }

        [Subscription("V91.DeviceData")]
        public AITValveData V91 { get; set; }

        [Subscription("V92.DeviceData")]
        public AITValveData V92 { get; set; }

        [Subscription("V93.DeviceData")]
        public AITValveData V93 { get; set; }

        [Subscription("V94.DeviceData")]
        public AITValveData V94 { get; set; }

        [Subscription("V95.DeviceData")]
        public AITValveData V95 { get; set; }

        [Subscription("V96.DeviceData")]
        public AITValveData V96 { get; set; }

        [Subscription("V97.DeviceData")]
        public AITValveData V97 { get; set; }

        [Subscription("EPV1.DeviceData")]
        public AITValveData EPV1 { get; set; }

        [Subscription("EPV2.DeviceData")]
        public AITValveData EPV2 { get; set; }
        #endregion

        #region Temp


        [Subscription("TC2.L3InputTempSetPoint")]
        public float SCRL3InputTemp { get; set; }

        [Subscription("TC1.L1InputTempSetPoint")]
        public float L1InputTemp { get; set; }


        [Subscription("TC1.L2InputTempSetPoint")]
        public float L2InputTemp { get; set; }

        [Subscription("TC1.L3InputTempSetPoint")]
        public float L3InputTemp { get; set; }

        [Subscription("PSU2.SimulatorTemp")]
        public double SimulatorTemp { get; set; }


        #endregion

        #region CoolingWater Temp Data
        [Subscription("FlowTemp1.DeviceData")]
        public AITWaterFlowData FlowTemp1 { get; set; }

        [Subscription("FlowTemp2.DeviceData")]
        public AITWaterFlowData FlowTemp2 { get; set; }

        [Subscription("FlowTemp3.DeviceData")]
        public AITWaterFlowData FlowTemp3 { get; set; }

        [Subscription("FlowTemp4.DeviceData")]
        public AITWaterFlowData FlowTemp4 { get; set; }

        [Subscription("FlowTemp5.DeviceData")]
        public AITWaterFlowData FlowTemp5 { get; set; }

        [Subscription("FlowTemp6.DeviceData")]
        public AITWaterFlowData FlowTemp6 { get; set; }

        [Subscription("FlowTemp7.DeviceData")]
        public AITWaterFlowData FlowTemp7 { get; set; }

        [Subscription("FlowTemp8.DeviceData")]
        public AITWaterFlowData FlowTemp8 { get; set; }

        [Subscription("FlowTemp9.DeviceData")]
        public AITWaterFlowData FlowTemp9 { get; set; }

        [Subscription("FlowTemp10.DeviceData")]
        public AITWaterFlowData FlowTemp10 { get; set; }

        [Subscription("FlowTemp11.DeviceData")]
        public AITWaterFlowData FlowTemp11 { get; set; }

        [Subscription("FlowTemp12.DeviceData")]
        public AITWaterFlowData FlowTemp12 { get; set; }

        [Subscription("FlowTemp13.DeviceData")]
        public AITWaterFlowData FlowTemp13 { get; set; }

        [Subscription("FlowTemp14.DeviceData")]
        public AITWaterFlowData FlowTemp14 { get; set; }

        #endregion

        #region Pump
        [Subscription("Pump.DeviceData")]
        public AITPumpData PumpData { get; set; }


        //[Subscription("PMDRYVacuumPump.DeviceDataMP")]
        //public AITPumpData PumpDataMP { get; set; }

        //[Subscription("PMDRYVacuumPump.DeviceDataBP")]
        //public AITPumpData PumpDataBP { get; set; }

        #endregion

        #region Sensor

        [Subscription("SensorChamLidClosed.DeviceData")]
        public AITSensorData Sensor1 { get; set; }

        [Subscription("CleanRoutineSucceed.DeviceData")]
        public AITSensorData CleanRoutineSucceed { get; set; }

        //[Subscription("Sensor2.DeviceData")]
        //public AITSensorData Sensor2 { get; set; }

        //[Subscription("SensorChamBodyOTSW.DeviceData")]
        //public AITSensorData Sensor3 { get; set; }

        [Subscription("SensorChamCabDoorClosed.DeviceData")]
        public AITSensorData Sensor4 { get; set; }

        [Subscription("SensorChamPressAboveATMSW.DeviceData")]
        public AITSensorData Sensor5 { get; set; }

        [Subscription("SensorChamAtSafeProcessPressSW.DeviceData")]
        public AITSensorData Sensor6 { get; set; }

        //[Subscription("Sensor7.DeviceData")]
        //public AITSensorData Sensor7 { get; set; }

        [Subscription("SensorDORPressVacSW.DeviceData")]
        public AITSensorData Sensor8 { get; set; }

        [Subscription("SensorHeaterTempBelow900CSW.DeviceData")]
        public AITSensorData Sensor9 { get; set; }

        [Subscription("SensorConfinementRingUp.DeviceData")]
        public AITSensorData Sensor10 { get; set; }

        ////[Subscription("Sensor11.DeviceData")]
        ////public AITSensorData Sensor11 { get; set; }

        ////[Subscription("Sensor12.DeviceData")]
        ////public AITSensorData Sensor12 { get; set; }

        ////[Subscription("Sensor13.DeviceData")]
        ////public AITSensorData Sensor13 { get; set; }

        ////[Subscription("Sensor14.DeviceData")]
        ////public AITSensorData Sensor14 { get; set; }

        ////[Subscription("Sensor15.DeviceData")]
        ////public AITSensorData Sensor15 { get; set; }

        ////[Subscription("Sensor16.DeviceData")]
        ////public AITSensorData Sensor16 { get; set; }

        ////[Subscription("Sensor17.DeviceData")]
        ////public AITSensorData Sensor17 { get; set; }

        ////[Subscription("Sensor18.DeviceData")]
        ////public AITSensorData Sensor18 { get; set; }

        ////[Subscription("Sensor19.DeviceData")]
        ////public AITSensorData Sensor19 { get; set; }

        ////[Subscription("Sensor20.DeviceData")]
        ////public AITSensorData Sensor20 { get; set; }

        ////[Subscription("Sensor21.DeviceData")]
        ////public AITSensorData Sensor21 { get; set; }

        [Subscription("SensorPMH2DetectorSW.DeviceData")]
        public AITSensorData Sensor22 { get; set; }

        [Subscription("SensorGBHCLDetectorSW.DeviceData")]
        public AITSensorData Sensor23 { get; set; }

        [Subscription("SensorGBDoorClosed.DeviceData")]
        public AITSensorData Sensor24 { get; set; }

        [Subscription("SensorDryPumpAlarm.DeviceData")]
        public AITSensorData Sensor25 { get; set; }

        [Subscription("SensorPumpExhaustPressSW.DeviceData")]
        public AITSensorData Sensor26 { get; set; }

        [Subscription("SensorPumpExhaustDPSW.DeviceData")]
        public AITSensorData Sensor27 { get; set; }

        [Subscription("SensorScrubberIntlkSW.DeviceData")]
        public AITSensorData Sensor28 { get; set; }

        [Subscription("SensorFacilityIntlkSW.DeviceData")]
        public AITSensorData Sensor29 { get; set; }

        [Subscription("SensorReactorWaterLeakSW.DeviceData")]
        public AITSensorData Sensor30 { get; set; }

        [Subscription("SensorGBWaterLeakSW.DeviceData")]
        public AITSensorData Sensor33 { get; set; }

        [Subscription("SensorPRWaterLeakSW.DeviceData")]
        public AITSensorData Sensor34 { get; set; }



        #endregion


        #region Total Gas Flow

        public double InputGasFlowCount1 => (V97.IsOpen ? Mfc40Data.FeedBack : 0)
                                            + (V91.IsOpen ? Mfc33Data.FeedBack : 0)
                                            + (V87.IsOpen ? Mfc27Data.FeedBack : 0)
                                            + (V90.IsOpen ? Mfc31Data.FeedBack : 0)
                                            + (V90.IsOpen ? Mfc25Data.FeedBack : 0)
                                            + (V90.IsOpen ? Mfc22Data.FeedBack : 0)
                                            + (V90.IsOpen ? Mfc19Data.FeedBack : 0)
                                            + (V89.IsOpen ? Mfc29Data.FeedBack : 0)
                                            + (V89.IsOpen ? Mfc26Data.FeedBack : 0)
                                            + (V89.IsOpen ? Mfc23Data.FeedBack : 0)
                                            + (V89.IsOpen ? Mfc20Data.FeedBack : 0)
                                            + (V88.IsOpen ? Mfc28Data.FeedBack : 0)
                                            + (V88.IsOpen ? Mfc15Data.FeedBack : 0)
                                            + (V88.IsOpen ? Mfc9Data.FeedBack : 0);

        public double InputGasFlowCount2 => V92.IsOpen ? Mfc32Data.FeedBack : 0;

        public double InputGasFlowCount3 => (V96.IsOpen ? Mfc38Data.FeedBack : 0)
                                            + (V95.IsOpen ? Mfc37Data.FeedBack : 0)
                                            + (V94.IsOpen ? Mfc36Data.FeedBack : 0)
                                            + (V93.IsOpen ? Mfc35Data.FeedBack : 0)
                                            + (V92.IsOpen ? Mfc32Data.FeedBack : 0);




        public double InputGasFlowTotal =>
            InputGasFlowCount1 + InputGasFlowCount2 + InputGasFlowCount3;

        #endregion


        [Subscription("PSU1.AllHeatEnable")]
        public bool AllHeatEnable { get; set; }

        //底部温度和侧壁功率
        [Subscription("TC1.TempCtrlTCIN")]
        public float TC1Temp2 { get; set; }

        [Subscription("SCR1.PowerFeedBack")]
        public float SCR1Power { get; set; }

        [Subscription("SCR2.PowerFeedBack")]
        public float SCR2Power { get; set; }

        [Subscription("SCR3.PowerFeedBack")]
        public float SCR3Power { get; set; }

        [Subscription("SCR1.DeviceData")]
        public IoPsuData SCR1Data { get; set; }

        [Subscription("SCR2.DeviceData")]
        public IoPsuData SCR2Data { get; set; }

        [Subscription("SCR3.DeviceData")]
        public IoPsuData SCR3Data { get; set; }


        [Subscription("PSU1.DeviceData")]
        public IoPsuData PSU1Data { get; set; }

        [Subscription("PSU2.DeviceData")]
        public IoPsuData PSU2Data { get; set; }

        [Subscription("PSU3.DeviceData")]
        public IoPsuData PSU3Data { get; set; }

        [Subscription("PSU1.OutputPowerFeedBack")]
        public float PSU1Power { get; set; }

        [Subscription("PSU2.OutputPowerFeedBack")]
        public float PSU2Power { get; set; }

        [Subscription("PSU3.OutputPowerFeedBack")]
        public float PSU3Power { get; set; }



        [Subscription("PMServo.ActualSpeedFeedback")]
        public float ActualSpeedFeedback { get; set; }


        [Subscription("PSU1.OutputArmsFeedBack")]
        public float OutputArmsFeedBack { get; set; }

        public string PSUArmStr => OutputArmsFeedBack.ToString("0.0");

        [Subscription("PSU1.OutputPowerFeedBack")]
        public float OutputPowerFeedBack { get; set; }

        public string PSUPowerStr => OutputPowerFeedBack.ToString("0.0");

        [Subscription("GasConnector.GasConnectorTightenFeedback")]
        public bool IsGasConnectorTighten { get; set; }



        [Subscription("TV.DeviceData")]
        public AITThrottleValveData TV { get; set; }

        [Subscription("SHLid.Status")]
        public string SHLidStatus { get; set; }

        public Visibility SHLidIsOpen
        {
            get
            {
                if (SHLidStatus == "Loosen") return Visibility.Hidden;
                return Visibility.Visible;
            }
        }

        public string LidTopColor
        {
            get
            {
                if (SHLidStatus == "Unknown") return "Yellow";
                if (SHLidStatus == "Error") return "Red";
                return "DimGray";
            }
        }

        [Subscription("BottomSection.Status")]
        public string BottomSectionStatus { get; set; }

        [Subscription("ChamberLiftPin.UpFeedback")]
        public bool UpFeedback { get; set; }

        [Subscription("ChamberLiftPin.DownFeedback")]
        public bool DownFeedback { get; set; }

        [Subscription("ChamberLiftPin.State")]
        public string LiftStatus
        {
            get;
            set;
        }

        public bool IsLiftUpEnable
        {
            get { return IsPMIdle && LiftStatus != "Up" && !IsOnline; }
        }
        public bool IsLiftDownEnable
        {
            get { return IsPMIdle && LiftStatus != "Down" && !IsOnline; }
        }

        [Subscription("ChamberDoor.State")]
        public string ChamberDoorState { get; set; }

        public bool IsChamberDoorOpenEnable
        {
            get { return IsPMIdle && ChamberDoorState != "Open" && !IsOnline; }
        }
        public bool IsChamberDoorCloseEnable
        {
            get { return IsPMIdle && ChamberDoorState != "Close" && !IsOnline; }
        }

        public string DoorColor
        {
            get
            {
                if (ChamberDoorState == "Unknown") return "Yellow";
                if (ChamberDoorState == "Error") return "Red";
                return "DimGray";
            }
        }



        [Subscription("VentValve.DeviceData")]
        public AITValveData VentValve { get; set; }

        [Subscription("Gas1Valve.DeviceData")]
        public AITValveData Gas1Valve { get; set; }

        [Subscription("Gas2Valve.DeviceData")]
        public AITValveData Gas2Valve { get; set; }

        [Subscription("Gas3Valve.DeviceData")]
        public AITValveData Gas3Valve { get; set; }

        [Subscription("Gas4Valve.DeviceData")]
        public AITValveData Gas4Valve { get; set; }

        [Subscription("FinalValve.DeviceData")]
        public AITValveData FinalValve { get; set; }

        [Subscription("ElectricalCoolingValve.DeviceData")]
        public AITValveData ElectricalCoolingValve { get; set; }

        [Subscription("ChamberLidCoolingValve.DeviceData")]
        public AITValveData ChamberLidCoolingValve { get; set; }

        [Subscription("MicrowaveCoolingValve.DeviceData")]
        public AITValveData MicrowaveCoolingValve { get; set; }

        [Subscription("RF.DeviceData")]
        public AITRfData RfData { get; set; }

        [Subscription("Microwave.DeviceData")]
        public AITRfData MicrowaveData { get; set; }

        [Subscription("ChamberHeater.DeviceData")]
        public AITHeaterData ChamberHeaterData { get; set; }

        [Subscription("ThrottleValve.DeviceData")]
        public AITThrottleValveData ThrottleValveData { get; set; }

        [Subscription("ChamberPressure.DeviceData")]
        public AITPressureMeterData ChamberPressure { get; set; }

        //[Subscription("CDAPressure.DeviceData")]
        //public AITPressureMeterData CDAPressure { get; set; }

        [Subscription("ChamberMonitorPressure.DeviceData")]
        public AITPressureMeterData ChamberMonitorPressure { get; set; }

        //[Subscription("Gas1Pressure.DeviceData")]
        //public AITPressureMeterData Gas1PressureData { get; set; }

        //[Subscription("Gas2Pressure.DeviceData")]
        //public AITPressureMeterData Gas2PressureData { get; set; }

        //[Subscription("Gas3Pressure.DeviceData")]
        //public AITPressureMeterData Gas3PressureData { get; set; }

        //[Subscription("Gas4Pressure.DeviceData")]
        //public AITPressureMeterData Gas4PressureData { get; set; }


        [Subscription("SensorEmo.DeviceData")]
        public AITSensorData SensorEmo { get; set; }

        [Subscription("SensorLidClosed.DeviceData")]
        public AITSensorData SensorLidClosed { get; set; }

        [Subscription("SensorDoor1Open.DeviceData")]
        public AITSensorData SensorDoor1Open { get; set; }

        [Subscription("SensorDoor2Open.DeviceData")]
        public AITSensorData SensorDoor2Open { get; set; }

        [Subscription("SensorDoor3Open.DeviceData")]
        public AITSensorData SensorDoor3Open { get; set; }

        [Subscription("SensorDoor4Open.DeviceData")]
        public AITSensorData SensorDoor4Open { get; set; }

        [Subscription("SensorVentFlowSwitch.DeviceData")]
        public AITSensorData SensorVentFlowSwitch { get; set; }

        [Subscription("SensorVacuumSwitch.DeviceData")]
        public AITSensorData SensorVacuumSwitch { get; set; }

        [Subscription("MainPump.DeviceData")]
        public AITPumpData MainPumpData { get; set; }

        [Subscription("SelectedRecipeName")]
        public string SelectedRecipeName
        {
            get;
            set;
        }

        public string SelectedRecipeNameDisplay
        {
            get
            {
                if (string.IsNullOrEmpty(SelectedRecipeName))
                    return string.Empty;
                return SelectedRecipeName.Substring("Sic\\Process\\".Length);
            }
        }

        [Subscription("StepName")]
        public string RecipeStepName { get; set; }

        [Subscription("StepNumber")]
        public string RecipeStepNumber { get; set; }

        [Subscription("ElapseTime")]
        public double RecipeElapseTime { get; set; }

        [Subscription("IsOnline")]
        public bool IsOnline { get; set; }
        public string OnlineState => IsOnline ? "Online" : "Offline";
        public string OnlineButtomContent => IsOnline ? "Offline" : "Online";

        public bool IsOnlineEnable
        {
            get { return IsPMIdle && !IsOnline; }
        }
        public bool IsOfflineEnable
        {
            get { return IsPMIdle && IsOnline; }
        }

        [Subscription("Status")]
        public string PMStatus { get; set; }
        public string PMStatusBackground
        {
            get { return ModuleStatusBackground.GetStatusBackground(PMStatus); }
        }
        public bool IsAbortEnable
        {
            get { return !IsPMIdle && PMStatus != "Init" && !IsPMError; }
        }
        public bool IsPMError
        {
            get { return PMStatus == "Error"; }
        }

        public bool IsPMIdle
        {
            get { return PMStatus == "Idle"; }
        }

        public bool IsEnableOperation
        {
            get
            {
                return PMStatus == "Idle" && !IsOnline;
            }
        }

        public bool IsHomed => PMStatus != "Init";
        public bool IsStartButtonEnable => PMStatus == "Idle" && !IsOnline;
        public bool IsSkipButtonEnable => PMStatus == "Process" && !IsOnline;
        public bool IsStopButtonEnable => PMStatus == "Process" && !IsOnline;

        public WaferInfo PMWafer
        {
            get
            {
                if (ModuleManager.ModuleInfos[Module].WaferManager.Wafers.Count > 0)
                    return ModuleManager.ModuleInfos[Module].WaferManager.Wafers[0];
                return new WaferInfo();
            }
        }

        public string Module => SystemName;


        public bool IsEnableOnline
        {
            get { return !IsOnline; }
        }

        public bool IsEnableOffline
        {
            get { return IsOnline; }
        }


        [IgnorePropertyChange]
        public string TVModeSetPoint
        {
            get;
            set;
        }
        [IgnorePropertyChange]
        public string TVPositionSetPoint
        {
            get;
            set;
        }
        [IgnorePropertyChange]
        public string TVPressureSetPoint
        {
            get;
            set;
        }
        [IgnorePropertyChange]
        public string Gas1MfcSetPoint
        {
            get;
            set;
        }
        [IgnorePropertyChange]
        public string Gas2MfcSetPoint
        {
            get;
            set;
        }
        [IgnorePropertyChange]
        public string Gas3MfcSetPoint
        {
            get;
            set;
        }
        [IgnorePropertyChange]
        public string Gas4MfcSetPoint
        {
            get;
            set;
        }
        [IgnorePropertyChange]
        public string MicrowavePowerSetPoint
        {
            get;
            set;
        }
        [IgnorePropertyChange]
        public string RfPowerSetPoint
        {
            get;
            set;
        }

        public string LabelU11 { get; set; }
        public string LabelU12 { get; set; }
        public string LabelU13 { get; set; }

        public ICommand CmdTvEnable { get; set; }
        public ICommand CmdTvReset { get; set; }
        public ICommand CmdTvPostionToZero { get; set; }
        public ICommand CmdTurnGasValve { get; set; }
        public ICommand CmdSetMfcFlow { get; set; }

        public PMOperationViewModel()
        {
            this.DisplayName = "Operation";
            CmdTvEnable = new DelegateCommand<object>(PerformCmdTvEnable);
            CmdTvReset = new DelegateCommand<object>(PerformCmdTvReset);
            CmdTurnGasValve = new DelegateCommand<object>(PerformCmdTurnGasValve);
            CmdSetMfcFlow = new DelegateCommand<object>(PerformCmdSetMfcFlow);
        }

        private void SetLabels()
        {
            if (Module == "PM1")
            {
                LabelU11 = "TMPump1Flow(U11)";
                LabelU12 = "Spare1(U12)";
                LabelU13 = "Spare2(U13)";
            }
            else if (Module == "PM2")
            {
                LabelU11 = "Spare1(U11)";
                LabelU12 = "Spare2(U12)";
                LabelU13 = "Spare3(U13)";
            }
            else
            {
                //Wrong Module
            }
        }

        private void PerformCmdTvEnable(object data)
        {
            var pair = (KeyValuePair<string, string>)data;
            InvokeClient.Instance.Service.DoOperation($"{SystemName}.TV.SetTVValveEnable", pair.Value);
        }
        private void PerformCmdTvReset(object data)
        {
            InvokeClient.Instance.Service.DoOperation($"{SystemName}.TV.SetTVReset", true);
        }
        private void PerformCmdTurnGasValve(object data)
        {
            var pair = (KeyValuePair<string, string>)data;
            InvokeClient.Instance.Service.DoOperation($"{SystemName}.{pair.Key}.{AITValveOperation.GVTurnValve}", pair.Value);
        }
        private void PerformCmdSetMfcFlow(object param)
        {
            object[] args = (object[])param; //0:devicename, 1:operation, 2:args
            if (args.Length == 3)
            {
                InvokeClient.Instance.Service.DoOperation($"{args[0]}.Ramp", args[2]);
            }
        }
        protected override void OnActivate()
        {
            base.OnActivate();

            //var serviceMode = QueryDataClient.Instance.Service.GetConfig("System.IsServiceControlMode");
            //EnableServiceControl = (bool)serviceMode;
            this.SetLabels();
            GetSC();


        }

        private void GetSC()
        {
            var mfc5IsInstalled = QueryDataClient.Instance.Service.GetConfig($"PM.{Module}.MFC.Mfc5.IsInstalled");
            if (mfc5IsInstalled != null)
            {
                Mfc5IsInstalled = (bool)QueryDataClient.Instance.Service.GetConfig($"PM.{Module}.MFC.Mfc5.IsInstalled");
            }
            
            TMAEnable = (bool)QueryDataClient.Instance.Service.GetConfig($"PM.{Module}.TMAEnable");
            CarbonSource = (string)QueryDataClient.Instance.Service.GetConfig($"PM.{Module}.CarbonSource");
            
           
            Opacity = TMAEnable ? 1 : 0.2;
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();
            base.InitPM();

            //权限
            var roleID = BaseApp.Instance.UserContext.Role.RoleId;
            reactorStatusEnable = RoleAccountProvider.Instance.GetMenuPermission(roleID, "PM1.Main.ReactorStatus") == MenuPermissionEnum.MP_READ_WRITE;
            reactorServiceEnable = RoleAccountProvider.Instance.GetMenuPermission(roleID, "PM1.Main.ReactorService") == MenuPermissionEnum.MP_READ_WRITE;


        }

        private bool reactorStatusEnable;
        public bool ReactorStatusEnable
        {
            get
            {
                return reactorStatusEnable;
            }
        }

        private bool reactorServiceEnable;
        public bool ReactorServiceEnable
        {
            get
            {
                return reactorServiceEnable;
            }
        }

        public void MoveLiftPinUp()
        {
            InvokeClient.Instance.Service.DoOperation($"{Module}.ChamberLiftPin.MoveUp");
        }

        public void MoveLiftPinDown()
        {
            InvokeClient.Instance.Service.DoOperation($"{Module}.ChamberLiftPin.MoveDown");
        }

        public void BottomSectionUp()
        {
            InvokeClient.Instance.Service.DoOperation($"{Module}.BottomSection.Up");
        }

        public void BottomSectionDown()
        {
            InvokeClient.Instance.Service.DoOperation($"{Module}.BottomSection.Down");
        }

        public void OpenChamberDoor()
        {
            InvokeClient.Instance.Service.DoOperation($"{Module}.ChamberDoor.Open");
        }

        public void CloseChamberDoor()
        {
            InvokeClient.Instance.Service.DoOperation($"{Module}.ChamberDoor.Close");
        }

        public void SetOnline()
        {
            InvokeClient.Instance.Service.DoOperation($"{Module}.PutOnline");
        }

        public void SetOffline()
        {
            InvokeClient.Instance.Service.DoOperation($"{Module}.PutOffline");
        }

        public void OnOffline()
        {
            if (IsOnline)
            {
                InvokeClient.Instance.Service.DoOperation($"{Module}.SetOffline");
            }
            else
            {
                InvokeClient.Instance.Service.DoOperation($"{Module}.SetOnline");
            }
        }

        public void Home()
        {
            var selection = DialogBox.ShowDialog(DialogButton.Yes | DialogButton.Cancel, DialogType.CONFIRM,
             "Are you sure perform the operation Home");
            if (selection == DialogButton.Yes)
            {
                InvokeClient.Instance.Service.DoOperation($"{Module}.Home");
            }
        }

        public void Reset()
        {
            InvokeClient.Instance.Service.DoOperation($"{Module}.Reset");

            //清除GEM报警
            InvokeClient.Instance.Service.DoOperation("GEM_ClearModuleAlarm", Module);
        }

        public void ToService()
        {
            var selection = DialogBox.ShowDialog(DialogButton.Yes | DialogButton.Cancel, DialogType.CONFIRM,
            "Are you sure perform the operation Service");
            if (selection == DialogButton.Yes)
            {
                InvokeClient.Instance.Service.DoOperation($"{Module}.SetToServiceIdle");
            }
        }

        public bool ShowChoosenDialog(string strInfo)
        {
            ChooseDialogBoxViewModel dialog = new ChooseDialogBoxViewModel();
            dialog.DisplayName = "Tips";
            dialog.InfoStr = strInfo;


            WindowManager wm = new WindowManager();
            bool? bret = wm.ShowDialog(dialog);
            if (!bret.HasValue || !bret.Value)
            {
                return false;
            }
            return true;
        }

        public void SelectRecipe()
        {
            var dialog = new RecipeSelectDialogViewModel();
            dialog.DisplayName = "Select Recipe";


            var wm = new WindowManager();
            var bret = wm.ShowDialog(dialog);
            if ((bool)bret)
            {
                var array = dialog.DialogResult.Split(new char[] { '\\' });

                InvokeClient.Instance.Service.DoOperation($"{SystemName}.SelectRecipe", dialog.DialogResult);
            }
        }

        public void Start()
        {
            InvokeClient.Instance.Service.DoOperation($"{Module}.RunRecipe", SelectedRecipeName);
        }

        public void Skip()
        {
            InvokeClient.Instance.Service.DoOperation($"{Module}.RecipeSkipStep");
        }


        protected override void InvokeAfterUpdateProperty(Dictionary<string, object> data)
        {
            base.InvokeAfterUpdateProperty(data);
        }

        public void SetTVMode()
        {
            if (string.IsNullOrEmpty(TVModeSetPoint))
            {
                DialogBox.ShowWarning("Select TV mode first");
                return;
            }

            PressureCtrlMode mode = PressureCtrlMode.Undefined;
            switch (TVModeSetPoint)
            {
                case "Pressure":
                    mode = PressureCtrlMode.TVPressureCtrl;
                    break;
                case "Position":
                    mode = PressureCtrlMode.TVPositionCtrl;
                    break;
                case "Close":
                    mode = PressureCtrlMode.TVClose;
                    break;
                default:
                    DialogBox.ShowWarning($"Invalid TV mode {TVModeSetPoint}");
                    break;
            }

            InvokeClient.Instance.Service.DoOperation($"{ThrottleValveData.Module}.{ThrottleValveData.DeviceName}.{AITThrottleValveOperation.SetMode}", mode.ToString());
        }

        public void SetTVPosition()
        {
            if (string.IsNullOrEmpty(TVPositionSetPoint) || !float.TryParse(TVPositionSetPoint, out float setpoint))
            {
                DialogBox.ShowWarning($"Invalid value {TVPositionSetPoint}");
                return;
            }

            InvokeClient.Instance.Service.DoOperation($"{ThrottleValveData.Module}.{ThrottleValveData.DeviceName}.{AITThrottleValveOperation.SetPosition}", setpoint.ToString());
        }

        public void SetTVPressure()
        {
            if (string.IsNullOrEmpty(TVPressureSetPoint) || !float.TryParse(TVPressureSetPoint, out float setpoint))
            {
                DialogBox.ShowWarning($"Invalid value {TVPressureSetPoint}");
                return;
            }

            InvokeClient.Instance.Service.DoOperation($"{ThrottleValveData.Module}.{ThrottleValveData.DeviceName}.{AITThrottleValveOperation.SetPressure}", setpoint.ToString());
        }

        public void SetMfcFlow(int index)
        {
            string gasLine = "";
            string value = "";
            if (index == 1)
            {
                value = Gas1MfcSetPoint;
                gasLine = "GasLine1";
            }
            else if (index == 2)
            {
                value = Gas2MfcSetPoint;
                gasLine = "GasLine2";
            }
            else if (index == 3)
            {
                value = Gas3MfcSetPoint;
                gasLine = "GasLine3";
            }
            else if (index == 4)
            {
                value = Gas4MfcSetPoint;
                gasLine = "GasLine4";
            }

            if (string.IsNullOrEmpty(gasLine) || string.IsNullOrEmpty(value) || !float.TryParse(value, out float setpoint))
            {
                DialogBox.ShowWarning($"Invalid value {value}");
                return;
            }

            InvokeClient.Instance.Service.DoOperation($"{SystemName}.{gasLine}.Flow", setpoint.ToString());

        }
        public void SetMicrowavePower()
        {
            if (string.IsNullOrEmpty(MicrowavePowerSetPoint) || !float.TryParse(MicrowavePowerSetPoint, out float setpoint))
            {
                DialogBox.ShowWarning($"Invalid value {MicrowavePowerSetPoint}");
                return;
            }

            InvokeClient.Instance.Service.DoOperation($"{MicrowaveData.Module}.{MicrowaveData.DeviceName}.{AITRfOperation.SetPower}", setpoint.ToString());
        }
        public void SetRfPower()
        {
            if (string.IsNullOrEmpty(RfPowerSetPoint) || !float.TryParse(RfPowerSetPoint, out float setpoint))
            {
                DialogBox.ShowWarning($"Invalid value {RfPowerSetPoint}");
                return;
            }

            InvokeClient.Instance.Service.DoOperation($"{RfData.Module}.{RfData.DeviceName}.{AITRfOperation.SetPower}", setpoint.ToString());
        }

        //Routine操作统一为一个函数
        public void PMOperation(string strPMOP)
        {
            if (strPMOP == "LeakCheck")
            {
                EnableLeakCheck = false;
            }

            if (!IsGasConnectorTighten)
            {
                DialogBox.ShowDialog(DialogButton.OK, DialogType.CONFIRM,
              "Gas Connector is not Tighten,can not Execute Opearation");

                return;
            }

            //有些特殊处理
            if (strPMOP.Contains("ToProcessIdle"))
            {
                if (IsService)
                {
                    DialogBox.ShowError("Unable to enter ProcessIdle in Service Mode.");
                    return;
                }

                //确认同时Process的腔体数量是否达标；若配置中屏蔽此项可立即生效，无需重启
                //var workenable = (bool)QueryDataClient.Instance.Service.GetConfig("System.MultiProcessMode");
                //if (workenable)
                //{
                //    //先发送一次状态
                //    InvokeClient.Instance.Service.DoOperation("MultiProcessClient.UpdateChamberState");

                //    var r = QueryDataClient.Instance.Service.GetData($"MultiProcessClient.{Module}.CanEnterProcessNow");
                //    if (r == null || (bool)r == false)
                //    {
                //        DialogBox.ShowError("Can't Enter ProcessIdle because of MultiProcess Limit");
                //    }
                //}

                //如果加热Enable未打开则返回
                if (!AllHeatEnable)
                {
                    ShowChoosenDialog("1.Make sure the HeatEnable is turned on!");
                    return;
                }
                if (!CleanRoutineSucceed.Value)
                {
                    ShowChoosenDialog("3.Make sure the DO_CleanRoutineSuscessed[DO-173]!");
                    return;
                }

                if (!V94.SetPoint || !V96.SetPoint)
                {
                    ShowChoosenDialog("Final valve not full open, please perform ATM Idle");
                    return;
                }
            }

            if (strPMOP.Contains("TMA"))
            {
                if (!ShowChoosenDialog("1.Make sure the TMA tap has turned off \r\n2.Make sure the confinement in up position!\r\n3.Press OK to continue! "))
                {
                    return;
                }
            }

            if (strPMOP.Contains("TCS"))
            {
                if (!ShowChoosenDialog("1.Make sure the TCS tap has turned off \r\n2.Make sure the confinement in up position!\r\n3.Press OK to continue! "))
                {
                    return;
                }
            }

            //Abort不需要弹框提示
            if (strPMOP.Contains("Abort"))
            {
                InvokeClient.Instance.Service.DoOperation($"{Module}.{strPMOP}");
                return;
            }

            //去除strPMOP中To关键字
            string strCleanTo = strPMOP.Replace("To", "");
            var selection = DialogBox.ShowDialog(DialogButton.Yes | DialogButton.Cancel, DialogType.CONFIRM, $"Are you sure perform the operation {strCleanTo}");
            if (selection == DialogButton.Yes)
            {
                if (strPMOP.Contains("ExchangeMO"))
                {
                    string strMOType = strPMOP.Replace("ExchangeMO", "");
                    InvokeClient.Instance.Service.DoOperation($"{Module}.ExchangeMO", $"{strMOType}");
                }
                else
                {
                    InvokeClient.Instance.Service.DoOperation($"{Module}.{strPMOP}");
                }
            }
        }

    }
}
