using Aitex.Core.Common;
using Aitex.Core.UI.MVVM;
using Aitex.Core.Util;
using Aitex.Core.Utilities;
using LiveCharts;
using MECF.Framework.Common.Beckhoff.AxisProvider;
using MECF.Framework.Common.Beckhoff.Station;
using MECF.Framework.Common.CommonData.Loader;
using MECF.Framework.Common.CommonData.PUF;
using MECF.Framework.Common.DataCenter;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.OperationCenter;
using MECF.Framework.Common.Utilities;
using OpenSEMI.ClientBase;
using CyberX8_MainPages.Unity;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Threading;
using WaferInfo = OpenSEMI.ClientBase.WaferInfo;
namespace CyberX8_MainPages.ViewModels
{
    public class LoaderSetupViewModel : BindableBase
    {
        #region 常量 
        private const string MOTION_DATA = "MotionData";
        private const string IS_SWITCH_ON = "IsSwitchOn";
        private const string CURRENT_STATION = "CurrentStation";
        private const string SIDE_DATA = "SideData";
        private const string COMMON_DATA = "CommonData";
        private const string MOTOR_POSITION = "MotorPosition";
        private const string CURRENT_OPERATION = "CurrentOperation";
        private const double LOADER_ROTATION_DIFFER = 90;
        #endregion
        #region 内部变量
        #region shuttleA
        /// 
        /// shuttleA模块名称
        /// 
        private string _shuttleAModuleName;
        /// 
        /// shuttleA运动数据
        /// 
        private CommandMotionData _shuttleAMotorData;
        /// 
        /// shuttleA当前位置
        /// 
        private string _shuttleACurrentStation;
        #endregion
        #region TiltA
        /// 
        /// tiltA模块名称
        /// 
        private string _tiltAModuleName;
        /// 
        /// tiltA运动数据
        /// 
        private CommandMotionData _tiltAMotorData;
        /// 
        /// tiltA当前位置
        /// 
        private string _tiltACurrentStation;
        /// 
        /// Loader1 TiltA实际位置数据
        /// 
        private double _loader1TiltAMotorPosition;
       
        #endregion
        #region CRSA
        /// 
        /// crsA模块名称
        /// 
        private string _crsAModuleName;
        /// 
        /// CRSA运动数据
        /// 
        private CommandMotionData _crsAMotorData;
        /// 
        /// crsA当前位置
        /// 
        private string _crsACurrentStation;
        #endregion
        #region Rotation
        /// 
        /// rotation模块名称
        /// 
        private string _rotationModuleName;
        /// 
        /// rotation运动数据
        /// 
        private CommandMotionData _rotationMotorData;
        /// 
        /// rotation当前位置
        /// 
        private string _rotationCurrentStation;
        /// 
        /// Loader1 Rotation UI对应位置
        /// 
        private double _loader1RotationPosition;
        /// 
        /// Loader1 Rotation实际位置数据
        /// 
        private double _loader1RotationMotorPosition;
        
        #endregion
        #region shuttleB
        /// 
        /// shuttleB模块名称
        /// 
        private string _shuttleBModuleName;
        /// 
        /// shuttleB运动数据
        /// 
        private CommandMotionData _shuttleBMotorData;
        /// 
        /// shuttleB当前位置
        /// 
        private string _shuttleBCurrentStation;
        #endregion
        #region TiltB
        /// 
        /// tiltB模块名称
        /// 
        private string _tiltBModuleName;
        /// 
        /// tiltB运动数据
        /// 
        private CommandMotionData _tiltBMotorData;
        /// 
        /// tiltB当前位置
        /// 
        private string _tiltBCurrentStation;
        /// 
        /// Loader1 TiltB实际位置数据
        /// 
        private double _loader1TiltBMotorPosition;
        #endregion
        #region CRSB
        /// 
        /// crsB模块名称
        /// 
        private string _crsBModuleName;
        /// 
        /// crsB运动数据
        /// 
        private CommandMotionData _crsBMotorData;
        /// 
        /// crsB当前位置
        /// 
        private string _crsBCurrentStation;
        #endregion
        #region SideA
        /// 
        /// Side A模块名称
        /// 
        private string _sideAModuleName;
        /// 
        /// Side A数据
        /// 
        private LoaderSideData _sideAData;
        #endregion
        #region SideB
        /// 
        /// Side B模块名称
        /// 
        private string _sideBModuleName;
        /// 
        /// Side B数据
        /// 
        private LoaderSideData _sideBData;
        #endregion
        #region Common
        /// 
        /// Common模块名称
        /// 
        private string _commonModuleName;
        /// 
        /// 公共模块数据
        /// 
        public LoaderCommonData _commonData;
        #endregion
        #region 系统数据
        /// 
        /// WaferA尺寸
        /// 
        private int _waferSizeA = 100;
        /// 
        /// WaferB尺寸
        /// 
        private int _waferSizeB = 100;
        /// 
        /// 查询后台数据集合
        /// 
        private List _rtDataKeys = new List();
        /// 
        /// Busy状态
        /// 
        private bool _isBusy;
        /// 
        /// CRSH状态
        /// 
        private bool _isCRSH;
        /// 
        /// label系列
        /// 
        private ChartValues _labelSeries;
        /// 
        /// Value系列
        /// 
        private ChartValues _valueSeries;
        /// 
        /// 定时器
        /// 
        DispatcherTimer _timer;
        /// 
        /// rt查询key数值字典
        /// 
        private Dictionary _rtDataValueDic = new Dictionary();
        /// 
        /// PlateOutCamera Enable
        /// 
        private Visibility _plateOutCameraVisibility = Visibility.Hidden;
        /// 
        /// Loader的sideA开启状态
        /// 
        private bool m_loaderWaferVisibleA = false;
        /// 
        /// Loader的sideB开启状态
        /// 
        private bool m_loaderWaferVisibleB = false;
        /// 
        /// WaferA信息
        /// 
        private WaferInfo m_loaderWaferA;
        /// 
        /// WaferB信息
        /// 
        private WaferInfo m_loaderWaferB;
        /// 
        /// Loader模块信息
        /// 
        private ModuleInfo _loaderModuleInfo;
        /// 
        /// Loader是否安装
        /// 
        private bool _loaderInstalled;
        /// 
        /// 当前SideA操作
        /// 
        private string _currentSideAOperation;
        /// 
        /// 当前SideA操作
        /// 
        private string _currentSideBOperation;
        #endregion
        #endregion
        #region 属性
        #region 系统数据
        public string Module { get; set; }
        /// 
        /// WaferA尺寸大小
        /// 
        public int WaferSizeA { get { return _waferSizeA; } set { SetProperty(ref _waferSizeA, value); } }
        /// 
        /// WaferA尺寸大小
        /// 
        public int WaferSizeB { get { return _waferSizeB; } set { SetProperty(ref _waferSizeB, value); } }
        /// 
        /// Busy状态
        /// 
        public bool IsBusy { get { return _isBusy; } set { SetProperty(ref _isBusy, value); } }
        /// 
        /// CRSH状态
        /// 
        public bool IsCRSH { get { return _isCRSH; } set { SetProperty(ref _isCRSH, value); } }
        /// 
        /// Label系列
        /// 
        public ChartValues LabelSeries { get { return _labelSeries; } set { SetProperty(ref _labelSeries, value); } }
        /// 
        /// Value系列
        /// 
        public ChartValues ValueSeries { get { return _valueSeries; } set { SetProperty(ref _valueSeries, value); } }
        /// 
        /// Loader的sideA开启状态
        /// 
        public bool LoaderWaferVisibleA
        {
            get { return m_loaderWaferVisibleA; }
            set { SetProperty(ref m_loaderWaferVisibleA, value); }
        }
        /// 
        /// Loader的sideB开启状态
        /// 
        public bool LoaderWaferVisibleB
        {
            get { return m_loaderWaferVisibleB; }
            set { SetProperty(ref m_loaderWaferVisibleB, value); }
        }
        /// 
        /// WaferA信息
        /// 
        public WaferInfo LoaderWaferA
        {
            get { return m_loaderWaferA; }
            set { SetProperty(ref m_loaderWaferA, value); }
        }
        /// 
        /// WaferB信息
        /// 
        public WaferInfo LoaderWaferB
        {
            get { return m_loaderWaferB; }
            set { SetProperty(ref m_loaderWaferB, value); }
        }
        /// 
        /// Loader模块信息
        /// 
        public ModuleInfo LoaderModuleInfo
        {
            get { return _loaderModuleInfo; }
            set { SetProperty(ref _loaderModuleInfo, value); }
        }
        /// 
        /// Loader是否安装
        /// 
        public bool LoaderIsInstalled
        {
            get { return _loaderInstalled; }
            set { SetProperty(ref _loaderInstalled, value); }
        }
        /// 
        /// 当前SideA操作
        /// 
        public string CurrentSideAOperation
        {
            get { return _currentSideAOperation; }
            set { SetProperty(ref _currentSideAOperation, value); }
        }
        /// 
        /// 当前SideB操作
        /// 
        public string CurrentSideBOperation
        {
            get { return _currentSideBOperation; }
            set { SetProperty(ref _currentSideBOperation, value); }
        }
        #endregion
        #region shuttleA
        /// 
        /// shuttleA名称
        /// 
        public string ShuttleAModuleName
        {
            get { return _shuttleAModuleName; }
            set { SetProperty(ref _shuttleAModuleName, value); }
        }
        /// 
        /// shuttleA运动数据
        /// 
        public CommandMotionData ShuttleAMotionData
        {
            get { return _shuttleAMotorData; }
            set { SetProperty(ref _shuttleAMotorData, value); }
        }
        /// 
        /// shuttleA当前位置
        /// 
        public string ShuttleACurrentStation
        {
            get { return _shuttleACurrentStation; }
            set { SetProperty(ref _shuttleACurrentStation, value); }
        }
        #endregion
        #region TiltA
        /// 
        /// TiltA名称
        /// 
        public string TiltAModuleName
        {
            get { return _tiltAModuleName; }
            set { SetProperty(ref _tiltAModuleName, value); }
        }
        /// 
        /// TiltA运动数据
        /// 
        public CommandMotionData TiltAMotionData
        {
            get { return _tiltAMotorData; }
            set { SetProperty(ref _tiltAMotorData, value); }
        }
        /// 
        /// TiltA当前位置
        /// 
        public string TiltACurrentStation
        {
            get { return _tiltACurrentStation; }
            set { SetProperty(ref _tiltACurrentStation, value); }
        }
        /// 
        /// Loader1 TiltA实际位置数据
        /// 
        public double Loader1TiltAMotorPosition
        {
            get { return _loader1TiltAMotorPosition; }
            set { SetProperty(ref _loader1TiltAMotorPosition, value); }
        }
        #endregion
        #region CRSA
        /// 
        /// CRSA名称
        /// 
        public string CRSAModuleName
        {
            get { return _crsAModuleName; }
            set { SetProperty(ref _crsAModuleName, value); }
        }
        /// 
        /// CRSA运动数据
        /// 
        public CommandMotionData CRSAMotionData
        {
            get { return _crsAMotorData; }
            set { SetProperty(ref _crsAMotorData, value); }
        }
        /// 
        /// CRSA当前位置
        /// 
        public string CRSACurrentStation
        {
            get { return _crsACurrentStation; }
            set { SetProperty(ref _crsACurrentStation, value); }
        }
        #endregion
        #region Rotation
        /// 
        /// Rotation名称
        /// 
        public string RotationModuleName
        {
            get { return _rotationModuleName; }
            set { SetProperty(ref _rotationModuleName, value); }
        }
        /// 
        /// Rotation运动数据
        /// 
        public CommandMotionData RotationMotionData
        {
            get { return _rotationMotorData; }
            set { SetProperty(ref _rotationMotorData, value); }
        }
        /// 
        /// Loader1 Rotation UI对应位置     
        /// 
        public double Loader1RotationPosition
        {
            get { return _loader1RotationPosition; }
            set { SetProperty(ref _loader1RotationPosition, value); }
        }
        /// 
        /// Rotation当前位置
        /// 
        public string RotationCurrentStation
        {
            get { return _rotationCurrentStation; }
            set { SetProperty(ref _rotationCurrentStation, value); }
        }
        /// 
        /// Loader1 Rotation实际位置数据
        /// 
        public double Loader1RotationMotorPosition
        {
            get { return _loader1RotationMotorPosition; }
            set { SetProperty(ref _loader1RotationMotorPosition, value); }
        }
       
        
        #endregion
        #region shuttleB
        /// 
        /// shuttleB名称
        /// 
        public string ShuttleBModuleName
        {
            get { return _shuttleBModuleName; }
            set { SetProperty(ref _shuttleBModuleName, value); }
        }
        /// 
        /// shuttleB运动数据
        /// 
        public CommandMotionData ShuttleBMotionData
        {
            get { return _shuttleBMotorData; }
            set { SetProperty(ref _shuttleBMotorData, value); }
        }
        /// 
        /// shuttleB当前位置
        /// 
        public string ShuttleBCurrentStation
        {
            get { return _shuttleBCurrentStation; }
            set { SetProperty(ref _shuttleBCurrentStation, value); }
        }
        #endregion
        #region TiltB
        /// 
        /// TiltB名称
        /// 
        public string TiltBModuleName
        {
            get { return _tiltBModuleName; }
            set { SetProperty(ref _tiltBModuleName, value); }
        }
        /// 
        /// TiltB运动数据
        /// 
        public CommandMotionData TiltBMotionData
        {
            get { return _tiltBMotorData; }
            set { SetProperty(ref _tiltBMotorData, value); }
        }
        /// 
        /// TiltB当前位置
        /// 
        public string TiltBCurrentStation
        {
            get { return _tiltBCurrentStation; }
            set { SetProperty(ref _tiltBCurrentStation, value); }
        }
        /// 
        /// Loader1 TiltB实际位置数据
        /// 
        public double Loader1TiltBMotorPosition
        {
            get { return _loader1TiltBMotorPosition; }
            set { SetProperty(ref _loader1TiltBMotorPosition, value); }
        }
        #endregion
        #region CRSB
        /// 
        /// CRSB名称
        /// 
        public string CRSBModuleName
        {
            get { return _crsBModuleName; }
            set { SetProperty(ref _crsBModuleName, value); }
        }
        /// 
        /// CRSB运动数据
        /// 
        public CommandMotionData CRSBMotionData
        {
            get { return _crsBMotorData; }
            set { SetProperty(ref _crsBMotorData, value); }
        }
        /// 
        /// CRSB当前位置
        /// 
        public string CRSBCurrentStation
        {
            get { return _crsBCurrentStation; }
            set { SetProperty(ref _crsBCurrentStation, value); }
        }
        #endregion
        #region Side A
        /// 
        /// Side A模块名称
        /// 
        public string SideAModuleName
        {
            get { return _sideAModuleName; }
            set { SetProperty(ref _sideAModuleName, value); }
        }
        /// 
        /// Side A数据
        /// 
        public LoaderSideData SideAData
        {
            get { return _sideAData; }
            set { SetProperty(ref _sideAData, value); }
        }
        #endregion
        #region Side B
        /// 
        /// Side B模块名称
        /// 
        public string SideBModuleName
        {
            get { return _sideBModuleName; }
            set { SetProperty(ref _sideBModuleName, value); }
        }
        /// 
        /// Side B数据
        /// 
        public LoaderSideData SideBData
        {
            get { return _sideBData; }
            set { SetProperty(ref _sideBData, value); }
        }
        #endregion
        #region Common
        /// 
        /// Common模块名称
        /// 
        public string CommonModuleName
        {
            get { return _commonModuleName; }
            set { SetProperty(ref _commonModuleName, value); }
        }
        /// 
        /// 公共数据
        /// 
        public LoaderCommonData CommonData
        {
            get { return _commonData; }
            set { SetProperty(ref _commonData, value);}
        }
        /// 
        /// PlateOut Camera Enable
        /// 
        public Visibility PlateOutCameraVisibility
        {
            get { return _plateOutCameraVisibility; }
            set { SetProperty(ref _plateOutCameraVisibility, value);}
        }
        #endregion
        #endregion
        #region 指令
        [IgnorePropertyChange]
        public ICommand SideALoadCommand
        {
            get;
            private set;
        }
        [IgnorePropertyChange]
        public ICommand SideAUnloadCommand
        {
            get;
            private set;
        }
        [IgnorePropertyChange]
        public ICommand SideAInstallCRSCommand
        {
            get;private set;
        }
        [IgnorePropertyChange]
        public ICommand SideBLoadCommand
        {
            get;
            private set;
        }
        [IgnorePropertyChange]
        public ICommand SideBUnloadCommand
        {
            get;
            private set;
        }
        [IgnorePropertyChange]
        public ICommand SideBInstallCRSCommand
        {
            get; private set;
        }
        #endregion
        /// 
        /// 构造函数
        /// 
        public LoaderSetupViewModel()
        {
            SideALoadCommand=new DelegateCommand