using Aitex.Sorter.Common;
using MECF.Framework.Common.CommonData;
using MECF.Framework.Common.DataCenter;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.OperationCenter;
using MECF.Framework.Common.Schedulers;
using OpenSEMI.ClientBase;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Threading;
using Venus_Core;
using Venus_MainPages.Unity;
using Venus_Themes.CustomControls;
using Venus_Themes.Unity;
using static Venus_Core.NiceRobotAction;

namespace Venus_MainPages.ViewModels
{

    public enum TMModule
    {
        PMA, PMB, PMC, PMD, LLA, LLB
    }
    public enum TMBlade
    {
        Blade1, Blade2
    }

    internal class TMViewModel : BindableBase
    {
        #region 私有字段
        //private ModuleInfo m_PMAWafer;
        private ModuleInfo m_LLAModuleInfo;
        private ModuleInfo m_LLBModuleInfo;
        private ModuleInfo m_TMModuleInfo;

        private bool m_TMIsOFFline;
        private bool m_LLIsOFFline;
        private WaferInfo m_PMAWafer;
        private WaferInfo m_PMBWafer;
        private WaferInfo m_PMCWafer;
        private WaferInfo m_PMDWafer;
        private WaferInfo m_LLAWafer;
        private WaferInfo m_LLBWafer;
        private WaferInfo m_BladeAWafer;
        private WaferInfo m_BladeBWafer;



        private bool m_PMADoorIsOpen;
        //private bool m_PMBDoorIsOpen; 
        //private bool m_PMCDoorIsOpen; 
        //private bool m_PMDDoorIsOpen;
        private bool m_LLADoorIsOpen;
        //private WaferRobotTAction m_WaferRobotTAction;

        //private WaferRobotTAction m_Robot1TAction;
        //private WaferRobotXAction m_Robot1XAction;
        //private WaferRobotTAction m_Robot2TAction;
        //private WaferRobotXAction m_Robot2XAction;
        private TMModule m_PickSelectedModule;
        private TMModule m_PlaceSelectedModule;
        private TMModule m_ExtendSelectedModule;
        private TMModule m_RetractSelectedModule;
        private TMBlade m_PickSelectedBlade;
        private TMBlade m_PlaceSelectedBlade;
        private TMBlade m_ExtendSelectedBlade;
        private TMBlade m_RetractSelectedBlade;

        private TMBlade m_GoToSelectedBlade;

        private TMModule m_GoToSelectedModule;

        private ObservableCollection<int> m_PlaceSoltItemsSource = new ObservableCollection<int>();
        private ObservableCollection<int> m_PickSoltItemsSource = new ObservableCollection<int>();
        private ObservableCollection<int> m_ExtendSoltItemsSource = new ObservableCollection<int>();
        private ObservableCollection<int> m_RetractSoltItemsSource = new ObservableCollection<int>();

        private int m_PickSoltSelectedIndex;
        private int m_PlaceSoltSelectedIndex;
        private int m_ExtendSoltSelectedIndex;
        private int m_RetractSoltSelectedIndex;
        private List<string> m_RtDataKeys = new List<string>();
        private Dictionary<string, object> m_RtDataValues;

        private string m_ModuleCheckedName = "TM";
        private string m_RobotAction;
        private string m_RobotTarget;

        private string m_RobotArm;

        private bool m_PMAIsInstalled;
        private bool m_PMBIsInstalled;
        private bool m_PMCIsInstalled;
        private bool m_PMDIsInstalled;
        private bool m_LLAIsInstalled;
        private bool m_LLBIsInstalled;
        private bool m_TMIsInstalled;


        private List<string> m_OriginalCycle = new List<string>();
        private List<string> m_ToCycle = new List<string>();

        private List<TMModule> m_TMModules = new List<TMModule>();

        private bool m_CycleEnable;
        private string m_OriginalCycleSelectedItem;
        private string m_ToCycleSelectedItem;
        private bool m_PMAIsCycle;
        private bool m_PMBIsCycle;
        private bool m_PMCIsCycle;
        private bool m_PMDIsCycle;

        private int m_CycleCount;
        private PressureType m_PressureType;
        private bool m_firstFlag = true;
        DispatcherTimer timer;


        private NiceXinSongWaferRobotTAction m_BladeNiceXinSongWaferRobotTAction;
        //private NiceXinSongWaferRobotTAction m_Blade2NiceXinSongWaferRobotTAction;

        private NiceXinSongWaferRobotXAction m_Blade1NiceXinSongWaferRobotXAction;
        private NiceXinSongWaferRobotXAction m_Blade2NiceXinSongWaferRobotXAction;
        #endregion

        #region 属性
        public PressureType PressureType
        {
            get { return m_PressureType; }
            set { SetProperty(ref m_PressureType, value); }
        }
        public List<string> OriginalCycle
        {
            get { return m_OriginalCycle; }
            set { SetProperty(ref m_OriginalCycle, value); }
        }
        public List<string> ToCycle
        {
            get { return m_ToCycle; }
            set { SetProperty(ref m_ToCycle, value); }
        }
        public string RobotAction2
        {
            get { return m_RobotAction; }
            set
            {
                //RobotActiont(m_RobotAction, value);

                SetProperty(ref m_RobotAction, value);
            }
        }

        public RobotMoveInfo m_robotMoveInfo;
        public RobotMoveInfo RobotMoveInfo
        {
            get { return m_robotMoveInfo; }
            set
            {
                TMRobotMoveInfoChanged(m_robotMoveInfo, value);
                SetProperty(ref m_robotMoveInfo, value);
            }
        }
        public string RobotTarget
        {
            get { return m_RobotTarget; }
            set
            {
                SetProperty(ref m_RobotTarget, value);
            }
        }
        public string RobotArm
        {
            get { return m_RobotArm; }
            set
            {
                SetProperty(ref m_RobotArm, value);
            }
        }
        public NiceXinSongWaferRobotTAction BladeNiceXinSongWaferRobotTAction
        {
            get { return m_BladeNiceXinSongWaferRobotTAction; }
            set
            {
                SetProperty(ref m_BladeNiceXinSongWaferRobotTAction, value);
            }
        }
        //public NiceXinSongWaferRobotTAction Blade2NiceXinSongWaferRobotTAction
        //{
        //    get { return m_Blade2NiceXinSongWaferRobotTAction; }
        //    set
        //    {
        //        SetProperty(ref m_Blade2NiceXinSongWaferRobotTAction, value);
        //    }
        //}

        public NiceXinSongWaferRobotXAction Blade1NiceXinSongWaferRobotXAction
        {
            get { return m_Blade1NiceXinSongWaferRobotXAction; }
            set
            {
                SetProperty(ref m_Blade1NiceXinSongWaferRobotXAction, value);
            }
        }
        public NiceXinSongWaferRobotXAction Blade2NiceXinSongWaferRobotXAction
        {
            get { return m_Blade2NiceXinSongWaferRobotXAction; }
            set
            {
                SetProperty(ref m_Blade2NiceXinSongWaferRobotXAction, value);
            }
        }
        //int arm1oldWaferStatus = 100;
        //int arm2oldWaferStatus = 100;
        private async void TMRobotMoveInfoChanged(RobotMoveInfo oldValue, RobotMoveInfo newValue)
        {

            string RobotTarget;
            if (oldValue == null || newValue == null)
            {
                return;
            }

            #region Rotating
            if ((oldValue.Action == RobotAction.None || oldValue.ArmTarget != newValue.ArmTarget) && (newValue.Action == RobotAction.Rotating))
            {
                var TMRobotMoveActionBladeTarget = newValue.BladeTarget;
                if (TMRobotMoveActionBladeTarget != null)
                {
                    RobotTarget = TMRobotMoveActionBladeTarget.ToString();
                }
                else
                {
                    return;
                }
                var values = RobotTarget.Split('.');
                var arm = values[0];
                var module = values[1];

                bool isParseOK;
                BladeNiceXinSongWaferRobotTAction = CommonFunction.ToEnum<NiceXinSongWaferRobotTAction>(module, out isParseOK);
                if (isParseOK == false)
                {
                    return;
                }

                //if (arm == "ArmA")
                //{        
                //}
                //else if (arm == "ArmB")
                //{
                //    Blade2NiceXinSongWaferRobotTAction = (NiceXinSongWaferRobotTAction)Enum.Parse(typeof(NiceXinSongWaferRobotTAction), module, true);
                //}
            }
            #endregion

            #region pick 和 place LL
            if ((oldValue.Action == RobotAction.None || oldValue.ArmTarget != newValue.ArmTarget) && (newValue.Action == RobotAction.Placing || newValue.Action == RobotAction.Picking))
            {
                var TMRobotMoveActionBladeTarget = newValue.BladeTarget;
                if (TMRobotMoveActionBladeTarget != null)
                {
                    RobotTarget = TMRobotMoveActionBladeTarget.ToString();
                }
                else
                {
                    return;
                }
                var values = RobotTarget.Split('.');
                var arm = values[0];
                var module = values[1];

                if (arm == "ArmA")
                {

                    bool isParseOK;
                    BladeNiceXinSongWaferRobotTAction = CommonFunction.ToEnum<NiceXinSongWaferRobotTAction>(module, out isParseOK);
                    if (isParseOK == false)
                    {
                        return;
                    }

                    //var waferRobotTAction = (NiceXinSongWaferRobotTAction)Enum.Parse(typeof(NiceXinSongWaferRobotTAction), module, true);

                    //BladeNiceXinSongWaferRobotTAction = waferRobotTAction;
                    await Task.Delay(600);
                    Blade1NiceXinSongWaferRobotXAction = NiceXinSongWaferRobotXAction.Extend1;
                    while ((newValue.Action == RobotAction.Placing && ModuleManager.ModuleInfos["TMRobot"].WaferManager.Wafers[0].WaferStatus != 0) || (newValue.Action == RobotAction.Picking && ModuleManager.ModuleInfos["TMRobot"].WaferManager.Wafers[0].WaferStatus == 0))
                    {
                        await Task.Delay(100);
                    }
                    //Robot1XAction = WaferRobotXAction.Retract;
                    Blade1NiceXinSongWaferRobotXAction = NiceXinSongWaferRobotXAction.Retract1;

                }
                else if (arm == "ArmB")
                {
                    bool isParseOK;
                    BladeNiceXinSongWaferRobotTAction = CommonFunction.ToEnum<NiceXinSongWaferRobotTAction>(module, out isParseOK);
                    if (isParseOK == false)
                    {
                        return;
                    }
                    //var waferRobotTAction = (NiceXinSongWaferRobotTAction)Enum.Parse(typeof(NiceXinSongWaferRobotTAction), module, true);

                    //BladeNiceXinSongWaferRobotTAction = waferRobotTAction;
                    //var waferRobotTAction = (WaferRobotTAction)Enum.Parse(typeof(WaferRobotTAction), module, true);
                    //var waferRobotTAction = (NiceXinSongWaferRobotTAction)Enum.Parse(typeof(NiceXinSongWaferRobotTAction), module, true);
                    //Blade2NiceXinSongWaferRobotTAction = waferRobotTAction;

                    //if (waferRobotTAction != Blade2NiceXinSongWaferRobotTAction)
                    //{
                    //}
                    await Task.Delay(600);
                    Blade2NiceXinSongWaferRobotXAction = NiceXinSongWaferRobotXAction.Extend2;

                    while ((newValue.Action == RobotAction.Placing && ModuleManager.ModuleInfos["TMRobot"].WaferManager.Wafers[1].WaferStatus != 0) || (newValue.Action == RobotAction.Picking && ModuleManager.ModuleInfos["TMRobot"].WaferManager.Wafers[1].WaferStatus == 0))
                    {
                        await Task.Delay(100);
                    }
                    Blade2NiceXinSongWaferRobotXAction = NiceXinSongWaferRobotXAction.Retract2;

                }
            }

            #endregion          

            #region pick 和 place pm
            else if ((oldValue.Action == RobotAction.None || oldValue.ArmTarget != newValue.ArmTarget) && newValue.Action == RobotAction.Extending)
            {
                var TMRobotMoveActionBladeTarget = newValue.BladeTarget;
                if (TMRobotMoveActionBladeTarget != null)
                {
                    RobotTarget = TMRobotMoveActionBladeTarget.ToString();
                }
                else
                {
                    return;
                }
                var values = RobotTarget.Split('.');
                var arm = values[0];
                var module = values[1];
                bool isParseOK;
                BladeNiceXinSongWaferRobotTAction = CommonFunction.ToEnum<NiceXinSongWaferRobotTAction>(module, out isParseOK);
                if (isParseOK == false)
                {
                    return;
                }
                //var waferRobotTAction = (NiceXinSongWaferRobotTAction)Enum.Parse(typeof(NiceXinSongWaferRobotTAction), module, true);
                //BladeNiceXinSongWaferRobotTAction = waferRobotTAction;
                if (arm == "ArmA")
                {
                    await Task.Delay(600);
                    Blade1NiceXinSongWaferRobotXAction = NiceXinSongWaferRobotXAction.Extend1;
                }
                else if (arm == "ArmB")
                {
                    await Task.Delay(600);
                    Blade2NiceXinSongWaferRobotXAction = NiceXinSongWaferRobotXAction.Extend2;
                }
            }
            else if ((oldValue.Action == RobotAction.None || oldValue.ArmTarget != newValue.ArmTarget) && newValue.Action == RobotAction.Retracting)
            {
                var TMRobotMoveActionBladeTarget = newValue.BladeTarget;
                if (TMRobotMoveActionBladeTarget != null)
                {
                    RobotTarget = TMRobotMoveActionBladeTarget.ToString();
                }
                else
                {
                    return;
                }
                var values = RobotTarget.Split('.');
                var arm = values[0];
                if (arm == "ArmA")
                {
                    //Robot1XAction = WaferRobotXAction.Retract;
                    Blade1NiceXinSongWaferRobotXAction = NiceXinSongWaferRobotXAction.Retract1;

                }
                else if (arm == "ArmB")
                {
                    //Robot2XAction = WaferRobotXAction.Retract;
                    Blade2NiceXinSongWaferRobotXAction = NiceXinSongWaferRobotXAction.Retract2;
                }
            }
            #endregion

            #region Home
            else if (oldValue.Action == RobotAction.None && newValue.Action == RobotAction.Homing)
            {
                Blade1NiceXinSongWaferRobotXAction = NiceXinSongWaferRobotXAction.Retract1;
                Blade2NiceXinSongWaferRobotXAction = NiceXinSongWaferRobotXAction.Retract2;

                await Task.Delay(2000);
                BladeNiceXinSongWaferRobotTAction = NiceXinSongWaferRobotTAction.T_Origin;

            }

            #endregion

        }

        //private async void RobotActiont(string oldValue,string newValue)
        //{

        //    if (oldValue == "None" && newValue == "Placing" && ModuleManager.ModuleInfos["TMRobot"].WaferManager.Wafers[0].WaferStatus==1)
        //    {
        //        var TMRobotMoveActionBladeTarget = QueryDataClient.Instance.Service.GetData("TM.RobotMoveAction.BladeTarget");
        //        if (TMRobotMoveActionBladeTarget != null)
        //        {
        //            RobotTarget = TMRobotMoveActionBladeTarget.ToString();
        //        }
        //        else
        //        {
        //            return;
        //        }
        //        var values = RobotTarget.Split('.');
        //        var arm = values[0];
        //        var module = values[1];
        //        Robot1TAction = (WaferRobotTAction)Enum.Parse(typeof(WaferRobotTAction), module, true);
        //        await Task.Delay(1500);
        //        Robot1XAction = WaferRobotXAction.Extend;
        //        if (arm == "ArmA")
        //        {
        //            Robot1TAction = (WaferRobotTAction)Enum.Parse(typeof(WaferRobotTAction), module, true);
        //            await Task.Delay(1500);
        //            Robot1XAction = WaferRobotXAction.Extend;
        //        }
        //        else if (arm == "ArmB")
        //        {
        //            Robot2TAction = (WaferRobotTAction)Enum.Parse(typeof(WaferRobotTAction), module, true);
        //            await Task.Delay(1500);
        //            Robot2XAction = WaferRobotXAction.Extend;
        //        }
        //    }
        //    if (oldValue == "None" && newValue == "Placing" && ModuleManager.ModuleInfos["TMRobot"].WaferManager.Wafers[1].WaferStatus == 1)
        //    {
        //        var TMRobotMoveActionBladeTarget = QueryDataClient.Instance.Service.GetData("TM.RobotMoveAction.BladeTarget");
        //        if (TMRobotMoveActionBladeTarget != null)
        //        {
        //            RobotTarget = TMRobotMoveActionBladeTarget.ToString();
        //        }
        //        else
        //        {
        //            return;
        //        }
        //        var values = RobotTarget.Split('.');
        //        var arm = values[0];
        //        var module = values[1];
        //        if (arm == "ArmA")
        //        {
        //            Robot1TAction = (WaferRobotTAction)Enum.Parse(typeof(WaferRobotTAction), module, true);
        //            await Task.Delay(1500);
        //            Robot1XAction = WaferRobotXAction.Extend;
        //        }
        //        else if (arm == "ArmB")
        //        {
        //            Robot2TAction = (WaferRobotTAction)Enum.Parse(typeof(WaferRobotTAction), module, true);
        //            await Task.Delay(1500);
        //            Robot2XAction = WaferRobotXAction.Extend;
        //        }
        //        //Robot2TAction = (WaferRobotTAction)Enum.Parse(typeof(WaferRobotTAction), module, true);
        //        //await Task.Delay(1500);
        //        //Robot2XAction = WaferRobotXAction.Extend;               
        //    }
        //    if (arm1oldWaferStatus != ModuleManager.ModuleInfos["TM"].WaferManager.Wafers[0].WaferStatus)
        //    {                     
        //        Robot1XAction = WaferRobotXAction.Retract;             
        //    }
        //    if (arm2oldWaferStatus != ModuleManager.ModuleInfos["TM"].WaferManager.Wafers[1].WaferStatus)
        //    {
        //        Robot2XAction = WaferRobotXAction.Retract;
        //    }

        //    if (oldValue == "None" && newValue == "Picking" && ModuleManager.ModuleInfos["TM"].WaferManager.Wafers[1].WaferStatus == 0)
        //    {
        //        var TMRobotMoveActionBladeTarget = QueryDataClient.Instance.Service.GetData("TM.RobotMoveAction.BladeTarget");
        //        if (TMRobotMoveActionBladeTarget != null)
        //        {
        //            RobotTarget = TMRobotMoveActionBladeTarget.ToString();
        //        }
        //        else
        //        {
        //            return;
        //        }
        //        var values = RobotTarget.Split('.');
        //        var arm = values[0];
        //        var module = values[1];
        //        if (arm == "ArmA")
        //        {
        //            Robot1TAction = (WaferRobotTAction)Enum.Parse(typeof(WaferRobotTAction), module, true);
        //            await Task.Delay(1500);
        //            Robot1XAction = WaferRobotXAction.Extend;
        //        }
        //        else if (arm == "ArmB")
        //        {
        //            Robot2TAction = (WaferRobotTAction)Enum.Parse(typeof(WaferRobotTAction), module, true);
        //            await Task.Delay(1500);
        //            Robot2XAction = WaferRobotXAction.Extend;
        //        }

        //    }


        //    #region pick 和 place pm
        //    if (oldValue == "None" && newValue == "Extending")
        //    {
        //        var TMRobotMoveActionBladeTarget = QueryDataClient.Instance.Service.GetData("TM.RobotMoveAction.BladeTarget");
        //        if (TMRobotMoveActionBladeTarget != null)
        //        {
        //            RobotTarget = TMRobotMoveActionBladeTarget.ToString();
        //        }
        //        else
        //        {
        //            return;
        //        }
        //        var values = RobotTarget.Split('.');
        //        var arm = values[0];
        //        var module = values[1];
        //        if (arm == "ArmA")
        //        {
        //            Robot1TAction = (WaferRobotTAction)Enum.Parse(typeof(WaferRobotTAction), module, true);
        //            await Task.Delay(1500);
        //            Robot1XAction = WaferRobotXAction.Extend;
        //        }
        //        else if (arm == "ArmB")
        //        {
        //            Robot2TAction = (WaferRobotTAction)Enum.Parse(typeof(WaferRobotTAction), module, true);
        //            await Task.Delay(1500);
        //            Robot2XAction = WaferRobotXAction.Extend;
        //        }

        //    }
        //    if (oldValue == "None" && newValue == "Retracting")
        //    {
        //        var TMRobotMoveActionBladeTarget = QueryDataClient.Instance.Service.GetData("TM.RobotMoveAction.BladeTarget");
        //        if (TMRobotMoveActionBladeTarget != null)
        //        {
        //            RobotTarget = TMRobotMoveActionBladeTarget.ToString();
        //        }
        //        else
        //        {
        //            return;
        //        }
        //        var values = RobotTarget.Split('.');
        //        var arm = values[0];
        //        if (arm == "ArmA")
        //        {
        //            Robot1XAction = WaferRobotXAction.Retract;
        //        }
        //        else if (arm == "ArmB")
        //        {
        //            Robot2XAction = WaferRobotXAction.Retract;
        //        }

        //    }
        //    #endregion


        //    arm1oldWaferStatus = ModuleManager.ModuleInfos["TM"].WaferManager.Wafers[0].WaferStatus;
        //    arm2oldWaferStatus = ModuleManager.ModuleInfos["TM"].WaferManager.Wafers[1].WaferStatus;


        //}
        public WaferInfo LLAWafer
        {
            get { return m_LLAWafer; }
            set { SetProperty(ref m_LLAWafer, value); }
        }
        public WaferInfo LLBWafer
        {
            get { return m_LLBWafer; }
            set { SetProperty(ref m_LLBWafer, value); }
        }
        public WaferInfo PMAWafer
        {
            get { return m_PMAWafer; }
            set { SetProperty(ref m_PMAWafer, value); }
        }
        public WaferInfo PMBWafer
        {
            get { return m_PMBWafer; }
            set { SetProperty(ref m_PMBWafer, value); }
        }
        public WaferInfo PMCWafer
        {
            get { return m_PMCWafer; }
            set { SetProperty(ref m_PMCWafer, value); }
        }
        public WaferInfo PMDWafer
        {
            get { return m_PMDWafer; }
            set { SetProperty(ref m_PMDWafer, value); }
        }
        public WaferInfo BladeAWafer
        {
            get { return m_BladeAWafer; }
            set { SetProperty(ref m_BladeAWafer, value); }
        }
        public WaferInfo BladeBWafer
        {
            get { return m_BladeBWafer; }
            set { SetProperty(ref m_BladeBWafer, value); }
        }
        public ModuleInfo LLAModuleInfo
        {
            get { return m_LLAModuleInfo; }
            set { SetProperty(ref m_LLAModuleInfo, value); }
        }
        public ModuleInfo LLBModuleInfo
        {
            get { return m_LLBModuleInfo; }
            set { SetProperty(ref m_LLBModuleInfo, value); }
        }
        public ModuleInfo TMModuleInfo
        {
            get { return m_TMModuleInfo; }
            set { SetProperty(ref m_TMModuleInfo, value); }
        }
        public bool TMIsOFFline
        {
            get { return m_TMIsOFFline; }
            set { SetProperty(ref m_TMIsOFFline, value); }
        }
        public bool LLIsOFFline
        {
            get { return m_LLIsOFFline; }
            set { SetProperty(ref m_LLIsOFFline, value); }
        }
        public bool PMADoorIsOpen
        {
            get { return m_PMADoorIsOpen; }
            set { SetProperty(ref m_PMADoorIsOpen, value); }
        }
        public bool LLADoorIsOpen
        {
            get { return m_LLADoorIsOpen; }
            set { SetProperty(ref m_LLADoorIsOpen, value); }
        }
        //public WaferRobotTAction WaferRobotTAction
        //{
        //    get { return m_WaferRobotTAction; }
        //    set { SetProperty(ref m_WaferRobotTAction, value); }
        //}
        //public WaferRobotTAction Robot1TAction
        //{
        //    get { return m_Robot1TAction; }
        //    set { SetProperty(ref m_Robot1TAction, value); }
        //}
        //public WaferRobotXAction Robot1XAction
        //{
        //    get { return m_Robot1XAction; }
        //    set { SetProperty(ref m_Robot1XAction, value); }
        //}
        //public WaferRobotTAction Robot2TAction
        //{
        //    get { return m_Robot2TAction; }
        //    set { SetProperty(ref m_Robot2TAction, value); }
        //}
        //public WaferRobotXAction Robot2XAction
        //{
        //    get { return m_Robot2XAction; }
        //    set { SetProperty(ref m_Robot2XAction, value); }
        //}
        public TMModule PickSelectedModule
        {
            get { return m_PickSelectedModule; }
            set { SetProperty(ref m_PickSelectedModule, value); }
        }
        public TMModule PlaceSelectedModule
        {
            get { return m_PlaceSelectedModule; }
            set { SetProperty(ref m_PlaceSelectedModule, value); }
        }
        public TMModule ExtendSelectedModule
        {
            get { return m_ExtendSelectedModule; }
            set { SetProperty(ref m_ExtendSelectedModule, value); }
        }
        public TMModule RetractSelectedModule
        {
            get { return m_RetractSelectedModule; }
            set { SetProperty(ref m_RetractSelectedModule, value); }
        }
        public TMBlade PickSelectedBlade
        {
            get { return m_PickSelectedBlade; }
            set { SetProperty(ref m_PickSelectedBlade, value); }
        }
        public TMBlade PlaceSelectedBlade
        {
            get { return m_PlaceSelectedBlade; }
            set { SetProperty(ref m_PlaceSelectedBlade, value); }
        }
        public TMBlade ExtendSelectedBlade
        {
            get { return m_ExtendSelectedBlade; }
            set { SetProperty(ref m_ExtendSelectedBlade, value); }
        }
        public TMBlade RetractSelectedBlade
        {
            get { return m_RetractSelectedBlade; }
            set { SetProperty(ref m_RetractSelectedBlade, value); }
        }
        public TMBlade GoToSelectedBlade
        {
            get { return m_GoToSelectedBlade; }
            set { SetProperty(ref m_GoToSelectedBlade, value); }
        }
        public TMModule GoToSelectedModule
        {
            get { return m_GoToSelectedModule; }
            set { SetProperty(ref m_GoToSelectedModule, value); }
        }
        public ObservableCollection<int> PickSoltItemsSource
        {
            get { return m_PickSoltItemsSource; }
            set { SetProperty(ref m_PickSoltItemsSource, value); }
        }
        public ObservableCollection<int> PlaceSoltItemsSource
        {
            get { return m_PlaceSoltItemsSource; }
            set { SetProperty(ref m_PlaceSoltItemsSource, value); }
        }
        public ObservableCollection<int> ExtendSoltItemsSource
        {
            get { return m_ExtendSoltItemsSource; }
            set { SetProperty(ref m_ExtendSoltItemsSource, value); }
        }
        public ObservableCollection<int> RetractSoltItemsSource
        {
            get { return m_RetractSoltItemsSource; }
            set { SetProperty(ref m_RetractSoltItemsSource, value); }
        }
        public int PickSoltSelectedIndex
        {
            get { return m_PickSoltSelectedIndex; }
            set { SetProperty(ref m_PickSoltSelectedIndex, value); }
        }
        public int PlaceSoltSelectedIndex
        {
            get { return m_PlaceSoltSelectedIndex; }
            set { SetProperty(ref m_PlaceSoltSelectedIndex, value); }
        }
        public int ExtendSoltSelectedIndex
        {
            get { return m_ExtendSoltSelectedIndex; }
            set { SetProperty(ref m_ExtendSoltSelectedIndex, value); }
        }
        public int RetractSoltSelectedIndex
        {
            get { return m_RetractSoltSelectedIndex; }
            set { SetProperty(ref m_RetractSoltSelectedIndex, value); }
        }
        public Dictionary<string, object> RtDataValues
        {
            get { return m_RtDataValues; }
            set { SetProperty(ref m_RtDataValues, value); }
        }
        public bool PMAIsInstalled
        {
            get { return m_PMAIsInstalled; }
            set { SetProperty(ref m_PMAIsInstalled, value); }
        }
        public bool PMBIsInstalled
        {
            get { return m_PMBIsInstalled; }
            set { SetProperty(ref m_PMBIsInstalled, value); }
        }
        public bool PMCIsInstalled
        {
            get { return m_PMCIsInstalled; }
            set { SetProperty(ref m_PMCIsInstalled, value); }
        }
        public bool PMDIsInstalled
        {
            get { return m_PMDIsInstalled; }
            set { SetProperty(ref m_PMDIsInstalled, value); }
        }
        public bool LLAIsInstalled
        {
            get { return m_LLAIsInstalled; }
            set { SetProperty(ref m_LLAIsInstalled, value); }
        }
        public bool LLBIsInstalled
        {
            get { return m_LLBIsInstalled; }
            set { SetProperty(ref m_LLBIsInstalled, value); }
        }
        public bool TMIsInstalled
        {
            get { return m_TMIsInstalled; }
            set { SetProperty(ref m_TMIsInstalled, value); }
        }

        public List<TMModule> TMModules
        {
            get { return m_TMModules; }
            set { SetProperty(ref m_TMModules, value); }
        }
        public bool CycleEnable
        {
            get { return m_CycleEnable; }
            set { SetProperty(ref m_CycleEnable, value); }
        }
        public string OriginalCycleSelectedItem
        {
            get { return m_OriginalCycleSelectedItem; }
            set { SetProperty(ref m_OriginalCycleSelectedItem, value); }
        }
        public string ToCycleSelectedItem
        {
            get { return m_ToCycleSelectedItem; }
            set { SetProperty(ref m_ToCycleSelectedItem, value); }
        }
        public bool PMAIsCycle
        {
            get { return m_PMAIsCycle; }
            set { SetProperty(ref m_PMAIsCycle, value); }
        }
        public bool PMBIsCycle
        {
            get { return m_PMBIsCycle; }
            set { SetProperty(ref m_PMBIsCycle, value); }
        }
        public bool PMCIsCycle
        {
            get { return m_PMCIsCycle; }
            set { SetProperty(ref m_PMCIsCycle, value); }
        }
        public bool PMDIsCycle
        {
            get { return m_PMDIsCycle; }
            set { SetProperty(ref m_PMDIsCycle, value); }
        }
        public int CycleCount
        {
            get { return m_CycleCount; }
            set { SetProperty(ref m_CycleCount, value); }
        }

        #endregion

        #region 命令
        private DelegateCommand _RobotHomeCommand;
        public DelegateCommand RobotHomeCommand =>
            _RobotHomeCommand ?? (_RobotHomeCommand = new DelegateCommand(OnRobotHome));
        private DelegateCommand _GotoCommand;
        public DelegateCommand GotoCommand =>
            _GotoCommand ?? (_GotoCommand = new DelegateCommand(OnGoto));

        private DelegateCommand _ExtendCommand;
        public DelegateCommand ExtendCommand =>
            _ExtendCommand ?? (_ExtendCommand = new DelegateCommand(OnExtend));
        private DelegateCommand _RetractCommand;
        public DelegateCommand RetractCommand =>
            _RetractCommand ?? (_RetractCommand = new DelegateCommand(OnRetract));

        private DelegateCommand<object> _ModuleChangeCommand;
        public DelegateCommand<object> ModuleChangeCommand =>
            _ModuleChangeCommand ?? (_ModuleChangeCommand = new DelegateCommand<object>(OnModuleChange));

        private DelegateCommand _PickCommand;
        public DelegateCommand PickCommand =>
            _PickCommand ?? (_PickCommand = new DelegateCommand(OnPick));

        private DelegateCommand _PlaceCommand;
        public DelegateCommand PlaceCommand =>
            _PlaceCommand ?? (_PlaceCommand = new DelegateCommand(OnPlace));

        private DelegateCommand _HomeCommand;
        public DelegateCommand HomeCommand =>
            _HomeCommand ?? (_HomeCommand = new DelegateCommand(OnHome));

        private DelegateCommand _VentCommand;
        public DelegateCommand VentCommand =>
            _VentCommand ?? (_VentCommand = new DelegateCommand(OnVent));

        private DelegateCommand _PumpCommand;
        public DelegateCommand PumpCommand =>
            _PumpCommand ?? (_PumpCommand = new DelegateCommand(OnPump));

        private DelegateCommand _PurgeCommand;
        public DelegateCommand PurgeCommand =>
            _PurgeCommand ?? (_PurgeCommand = new DelegateCommand(OnPurge));

        private DelegateCommand<object> _ModuleCheckedCommand;
        public DelegateCommand<object> ModuleCheckedCommand =>
            _ModuleCheckedCommand ?? (_ModuleCheckedCommand = new DelegateCommand<object>(OnModuleChecked));

        private DelegateCommand _AbortCommand;
        public DelegateCommand AbortCommand =>
            _AbortCommand ?? (_AbortCommand = new DelegateCommand(OnAbort));

        private DelegateCommand _StartCycleCommand;
        public DelegateCommand StartCycleCommand =>
            _StartCycleCommand ?? (_StartCycleCommand = new DelegateCommand(OnStartCycle));

        private DelegateCommand _StopCycleCommand;

        public DelegateCommand StopCycleCommand =>
            _StopCycleCommand ?? (_StopCycleCommand = new DelegateCommand(OnStopCycle));

        private DelegateCommand _SystemHomeCommand;

        public DelegateCommand SystemHomeCommand =>
            _SystemHomeCommand ?? (_SystemHomeCommand = new DelegateCommand(OnSystemHome));
        private DelegateCommand<object> _ChamberDoorOpenCommand;
        public DelegateCommand<object> ChamberDoorOpenCommand =>
            _ChamberDoorOpenCommand ?? (_ChamberDoorOpenCommand = new DelegateCommand<object>(OnChamberDoorOpen));

        private DelegateCommand<object> _ChamberDoorCloseCommand;
        public DelegateCommand<object> ChamberDoorCloseCommand =>
            _ChamberDoorCloseCommand ?? (_ChamberDoorCloseCommand = new DelegateCommand<object>(OnChamberDoorClose));

        private DelegateCommand _LoadCommand;
        public DelegateCommand LoadCommand =>
            _LoadCommand ?? (_LoadCommand = new DelegateCommand(OnLoad));
        #endregion

        #region 构造函数
        public TMViewModel()
        {
            string allModules = QueryDataClient.Instance.Service.GetConfig($"System.InstalledModules").ToString();
            PMAIsInstalled = allModules.Contains("PMA");
            PMBIsInstalled = allModules.Contains("PMB");
            PMCIsInstalled = allModules.Contains("PMC");
            PMDIsInstalled = allModules.Contains("PMD");
            LLAIsInstalled = allModules.Contains("LLA");
            LLBIsInstalled = allModules.Contains("LLB");
            TMIsInstalled = allModules.Contains("TM");
            if (PMAIsInstalled == true)
            {
                TMModules.Add(TMModule.PMA);
            }
            if (PMBIsInstalled == true)
            {
                TMModules.Add(TMModule.PMB);

            }
            if (PMCIsInstalled == true)
            {
                TMModules.Add(TMModule.PMC);

            }
            if (PMDIsInstalled == true)
            {
                TMModules.Add(TMModule.PMD);
            }
            if (LLAIsInstalled == true)
            {
                OriginalCycle.Add("LLA");
                ToCycle.Add("LLA");
                TMModules.Add(TMModule.LLA);

            }
            if (LLBIsInstalled == true)
            {
                OriginalCycle.Add("LLB");
                ToCycle.Add("LLB");
                TMModules.Add(TMModule.LLB);
            }
            addDataKeys();
            RtDataValues = QueryDataClient.Instance.Service.PollData(m_RtDataKeys);
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(0.1);
            timer.Tick += Timer_Tick;
            //timer.Start();

            PickSoltItemsSource.Add(1);
            PlaceSoltItemsSource.Add(1);
            ExtendSoltItemsSource.Add(1);
            RetractSoltItemsSource.Add(1);

        }



        private void Timer_Tick(object sender, EventArgs e)
        {
            if (LLAIsInstalled == true)
            {
                LLAWafer = ModuleManager.ModuleInfos["LLA"].WaferManager.Wafers.FirstOrDefault(x => x.WaferStatus != 0);
                LLAModuleInfo = ModuleManager.ModuleInfos["LLA"];
            }
            if (LLBIsInstalled == true)
            {
                LLBWafer = ModuleManager.ModuleInfos["LLB"].WaferManager.Wafers.FirstOrDefault(x => x.WaferStatus != 0);

                LLBModuleInfo = ModuleManager.ModuleInfos["LLB"];
            }
            if (PMAIsInstalled == true)
            {
                PMAWafer = ModuleManager.ModuleInfos["PMA"].WaferManager.Wafers[0];
            }
            if (PMBIsInstalled == true)
            {
                PMBWafer = ModuleManager.ModuleInfos["PMB"].WaferManager.Wafers[0];
            }
            if (PMCIsInstalled == true)
            {
                PMCWafer = ModuleManager.ModuleInfos["PMC"].WaferManager.Wafers[0];
            }
            if (PMDIsInstalled == true)
            {
                PMDWafer = ModuleManager.ModuleInfos["PMD"].WaferManager.Wafers[0];
            }

            if (TMIsInstalled == true)
            {
                BladeAWafer = ModuleManager.ModuleInfos["TMRobot"].WaferManager.Wafers[0];
                BladeBWafer = ModuleManager.ModuleInfos["TMRobot"].WaferManager.Wafers[1];
                TMModuleInfo = ModuleManager.ModuleInfos["TMRobot"];
            }


            RtDataValues = QueryDataClient.Instance.Service.PollData(m_RtDataKeys);


            var TMRobotMoveActionArmTarget = QueryDataClient.Instance.Service.GetData("TM.RobotMoveAction.ArmTarget");
            if (TMRobotMoveActionArmTarget != null)
            {
                RobotArm = ModuleManager.ModuleInfos["TMRobot"].WaferManager.Wafers[0].WaferStatus.ToString();
            }


            var TMRobotMoveActionBladeTarget = QueryDataClient.Instance.Service.GetData("TM.RobotMoveAction.BladeTarget");
            if (TMRobotMoveActionBladeTarget != null)
            {
                RobotTarget = TMRobotMoveActionBladeTarget.ToString();
            }

            var TMRobotMoveActionRobotAction = QueryDataClient.Instance.Service.GetData("TM.RobotMoveAction.RobotAction");

            if (TMRobotMoveActionRobotAction != null)
            {
                RobotAction2 = TMRobotMoveActionRobotAction.ToString();
            }

            RobotMoveInfo = (RobotMoveInfo)QueryDataClient.Instance.Service.GetData("TM.RobotMoveAction");
        }
        #endregion

        #region 命令方法
        private void OnRobotHome()
        {
            InvokeClient.Instance.Service.DoOperation($"TM.RobotHome", "TMRobot");
        }
        private void OnGoto()
        {
            var moduleName = (ModuleName)Enum.Parse(typeof(ModuleName), GoToSelectedModule.ToString(), true);
            //var selectedHand = (Hand)Enum.Parse(typeof(Hand), GoToSelectedBlade.ToString(), true);
            Queue<MoveItem> moveItems = new Queue<MoveItem>();
            MoveItem moveItem = new MoveItem(0, 0, moduleName, 0, Hand.Both);
            moveItems.Enqueue(moveItem);
            InvokeClient.Instance.Service.DoOperation($"TM.{RtOperation.Goto}", moveItems);
            
            //InvokeClient.Instance.Service.DoOperation($"TM.{RtOperation.Goto}", moduleName, selectedHand, selectedHand);
        }
        private void OnExtend()
        {
            var moduleName = (ModuleName)Enum.Parse(typeof(ModuleName), ExtendSelectedModule.ToString(), true);
            var selectedHand = (Hand)Enum.Parse(typeof(Hand), ExtendSelectedBlade.ToString(), true);
            InvokeClient.Instance.Service.DoOperation($"TM.{RtOperation.Extend}", moduleName, selectedHand, selectedHand);
           

        }
        private void OnRetract()
        {
            var moduleName = (ModuleName)Enum.Parse(typeof(ModuleName), RetractSelectedModule.ToString(), true);
            var selectedHand = (Hand)Enum.Parse(typeof(Hand), RetractSelectedBlade.ToString(), true);
            InvokeClient.Instance.Service.DoOperation($"TM.{RtOperation.Retract}", moduleName, selectedHand, selectedHand);
        }
        private void OnModuleChange(object obj)
        {
            var value = obj.ToString();
            switch (value)
            {
                case "Pick":
                    PickSoltItemsSource.Clear();
                    if ((int)PickSelectedModule == 4)
                    {
                        for (int i = 1; i <= LLAModuleInfo.WaferManager.Wafers.Count; i++)
                        {
                            PickSoltItemsSource.Add(i);
                        }
                    }
                    else if ((int)PickSelectedModule == 5)
                    {
                        for (int i = 1; i <= LLBModuleInfo.WaferManager.Wafers.Count; i++)
                        {
                            PickSoltItemsSource.Add(i);
                        }
                    }
                    else
                    {
                        PickSoltItemsSource.Add(1);
                    }
                    PickSoltSelectedIndex = 0;
                    break;

                case "Place":
                    PlaceSoltItemsSource.Clear();
                    if ((int)PlaceSelectedModule == 4)
                    {
                        for (int i = 1; i <= LLAModuleInfo.WaferManager.Wafers.Count; i++)
                        {
                            PlaceSoltItemsSource.Add(i);
                        }

                    }
                    else if ((int)PlaceSelectedModule == 5)
                    {
                        for (int i = 1; i <= LLBModuleInfo.WaferManager.Wafers.Count; i++)
                        {
                            PlaceSoltItemsSource.Add(i);
                        }
                    }
                    else
                    {
                        PlaceSoltItemsSource.Add(1);

                    }
                    PlaceSoltSelectedIndex = 0;
                    break;

                case "Extend":
                    ExtendSoltItemsSource.Clear();
                    if ((int)ExtendSelectedModule == 4)
                    {
                        for (int i = 1; i <= LLAModuleInfo.WaferManager.Wafers.Count; i++)
                        {
                            ExtendSoltItemsSource.Add(i);
                        }

                    }
                    else if ((int)ExtendSelectedModule == 5)
                    {
                        for (int i = 1; i <= LLBModuleInfo.WaferManager.Wafers.Count; i++)
                        {
                            ExtendSoltItemsSource.Add(i);
                        }
                    }
                    else
                    {
                        ExtendSoltItemsSource.Add(1);
                    }
                    ExtendSoltSelectedIndex = 0;
                    break;

                case "Retract":
                    RetractSoltItemsSource.Clear();
                    if ((int)RetractSelectedModule == 4)
                    {
                        for (int i = 1; i <= LLAModuleInfo.WaferManager.Wafers.Count; i++)
                        {
                            RetractSoltItemsSource.Add(i);
                        }
                        //RetractSoltItemsSource = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8 };
                    }
                    else if ((int)RetractSelectedModule == 5)
                    {
                        for (int i = 1; i <= LLBModuleInfo.WaferManager.Wafers.Count; i++)
                        {
                            RetractSoltItemsSource.Add(i);
                        }
                    }
                    else
                    {
                        RetractSoltItemsSource.Add(1);
                    }
                    RetractSoltSelectedIndex = 0;
                    break;
            }

        }
        private void OnPick()
        {
            Queue<MoveItem> moveItems = new Queue<MoveItem>();

            var moduleName = (ModuleName)Enum.Parse(typeof(ModuleName), PickSelectedModule.ToString(), true);
            var selectedHand = (Hand)Enum.Parse(typeof(Hand), PickSelectedBlade.ToString(), true);
            MoveItem moveItem = new MoveItem(moduleName, PickSoltItemsSource[PickSoltSelectedIndex] - 1, 0, 0, selectedHand);
            moveItems.Enqueue(moveItem);
            if ((int)PickSelectedModule > 3)
            {
                InvokeClient.Instance.Service.DoOperation($"TM.{RtOperation.LLPick}", moveItems);
            }
            else
            {
                InvokeClient.Instance.Service.DoOperation($"TM.{RtOperation.PMPick}", moveItems);
            }
        }
        private void OnPlace()
        {
            Queue<MoveItem> moveItems = new Queue<MoveItem>();
            var moduleName = (ModuleName)Enum.Parse(typeof(ModuleName), PlaceSelectedModule.ToString(), true);

            var selectedHand = (Hand)Enum.Parse(typeof(Hand), PlaceSelectedBlade.ToString(), true);
            MoveItem moveItem = new MoveItem(0, 0, moduleName, PlaceSoltItemsSource[PlaceSoltSelectedIndex] - 1, selectedHand);
            moveItems.Enqueue(moveItem);
            if ((int)PlaceSelectedModule > 3)
            {
                InvokeClient.Instance.Service.DoOperation($"TM.{RtOperation.LLPlace}", moveItems);
            }
            else
            {
                InvokeClient.Instance.Service.DoOperation($"TM.{RtOperation.PMPlace}", moveItems);
            }
        }
        private void OnHome()
        {
            InvokeClient.Instance.Service.DoOperation($"{m_ModuleCheckedName}.Home");
        }
        private void OnVent()
        {
            InvokeClient.Instance.Service.DoOperation($"{m_ModuleCheckedName}.{RtOperation.Vent}");
        }
        private void OnPump()
        {
            InvokeClient.Instance.Service.DoOperation($"{m_ModuleCheckedName}.{RtOperation.Pump}");
        }
        private void OnPurge()
        {
            InvokeClient.Instance.Service.DoOperation($"{m_ModuleCheckedName}.{RtOperation.Purge}");
        }
        private void OnModuleChecked(object obj)
        {
            m_ModuleCheckedName = obj.ToString();
        }
        private void OnAbort()
        {
            InvokeClient.Instance.Service.DoOperation($"{m_ModuleCheckedName}.{RtOperation.Abort}");
        }
        private void OnStartCycle()
        {
            if (CycleEnable == false)
            {
                return;
            }
            List<string> strings = new List<string>();
            if (OriginalCycleSelectedItem != "")
            {
                strings.Add(OriginalCycleSelectedItem);
            }
            else
            {
                return;
            }

            if (PMAIsCycle == true)
            {
                strings.Add("PMA");
            }
            if (PMBIsCycle == true)
            {
                strings.Add("PMB");
            }
            if (PMCIsCycle == true)
            {
                strings.Add("PMC");
            }
            if (PMDIsCycle == true)
            {
                strings.Add("PMD");
            }
            if (ToCycleSelectedItem != "")
            {
                strings.Add(ToCycleSelectedItem);
            }
            else
            {
                return;
            }
            InvokeClient.Instance.Service.DoOperation("TMCycle.Start", strings.ToArray(), CycleCount);

        }

        private void OnStopCycle()
        {

            InvokeClient.Instance.Service.DoOperation("TMCycle.Abort");

        }
        private void OnSystemHome()
        {
            InvokeClient.Instance.Service.DoOperation("System.Home");

        }
        private void OnChamberDoorOpen(object obj)
        {
            InvokeClient.Instance.Service.DoOperation($"{obj?.ToString()}.SlitDoor.Open");
        }
        private void OnChamberDoorClose(object obj)
        {
            InvokeClient.Instance.Service.DoOperation($"{obj?.ToString()}.SlitDoor.Close");
        }
        private void OnLoad()
        {
            if (m_firstFlag)
            {
                object obj = QueryDataClient.Instance.Service.GetData("System.PressureUnitType");
                PressureType = (PressureType)Convert.ToInt32(obj);
                m_firstFlag = false;
            }

        }

        #endregion

        #region 私有方法
        private void addDataKeys()
        {
            if (PMAIsInstalled)
            {
                m_RtDataKeys.Add("PMA.IsSlitDoorClosed");
                m_RtDataKeys.Add("PMA.CalculationPressure");

            }
            if (PMBIsInstalled)
            {
                m_RtDataKeys.Add("PMB.IsSlitDoorClosed");
                m_RtDataKeys.Add("PMB.CalculationPressure");

            }
            if (PMCIsInstalled)
            {
                m_RtDataKeys.Add("PMC.IsSlitDoorClosed");
                m_RtDataKeys.Add("PMC.CalculationPressure");

            }
            if (PMDIsInstalled)
            {
                m_RtDataKeys.Add("PMD.IsSlitDoorClosed");
                m_RtDataKeys.Add("PMD.CalculationPressure");

            }

            m_RtDataKeys.Add("TM.LLATSlitDoor.IsClosed");
            m_RtDataKeys.Add("TM.LLBTSlitDoor.IsClosed");

            m_RtDataKeys.Add("TM.LLAESlitDoor.IsClosed");
            m_RtDataKeys.Add("TM.LLBESlitDoor.IsClosed");

            m_RtDataKeys.Add("TMCycle.CycleIndex");

            m_RtDataKeys.Add("TM.PMASlitDoor.IsClosed");
            m_RtDataKeys.Add("TM.PMBSlitDoor.IsClosed");
            m_RtDataKeys.Add("TM.PMCSlitDoor.IsClosed");
            m_RtDataKeys.Add("TM.PMDSlitDoor.IsClosed");

            m_RtDataKeys.Add("TM.TMProcessGauge.Value");
            m_RtDataKeys.Add("TM.IsOnline");


        }
        public void EnterExitView(bool isEnter)
        {
            if (isEnter)
            {
                Timer_Tick(null, null);
                timer.Start();
            }
            else
            {
                timer.Stop();
            }
        }

        #endregion
    }
}