using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Aitex.Core.Common;
using LiveCharts;
using System.Windows;
using MECF.Framework.Common.CommonData.PUF;
using MECF.Framework.Common.DataCenter;
using MECF.Framework.Common.Equipment;
using Prism.Mvvm;
using System.Timers;
using Aitex.Core.RT.DataCenter;
using MECF.Framework.Common.Utilities;
using MECF.Framework.Common.CommonData.Loader;
using MECF.Framework.Common.OperationCenter;
using Aitex.Core.Utilities;
using System.Windows.Input;
using Aitex.Core.UI.MVVM;
using System.Windows.Threading;
namespace PunkHPX8_MainPages.ViewModels
{
    public class SRDStationSetupViewModel:BindableBase
    {
        #region 常量
        private const string MOTION_DATA = "MotionData";
        private const string IS_SWITCH_ON = "IsSwitchOn";
        private const string CURRENT_STATION = "CurrentStation";
        #endregion
        #region 内部变量
        #region SRD1Arm
        /// 
        /// sRD1Arm模块名称
        /// 
        private string _sRD1ArmModuleName;
        /// 
        /// sRD1Arm运动数据
        /// 
        private CommandMotionData _sRD1ArmMotionData;
        /// 
        /// sRD1Arm当前位置
        /// 
        private string _sRD1ArmCurrentStation;
        #endregion
        #region SRD1Rotation
        /// 
        /// sRD1Rotation模块名称
        /// 
        private string _sRD1RotationModuleName;
        /// 
        /// sRD1Rotation运动数据
        /// 
        private CommandMotionData _sRD1RotationMotionData;
        /// 
        /// sRD1Rotation当前位置
        /// 
        private string _sRD1RotationCurrentStation;
        #endregion
        #region SRD2Arm
        /// 
        /// sRD2Arm模块名称
        /// 
        private string _sRD2ArmModuleName;
        /// 
        /// sRD2Arm运动数据
        /// 
        private CommandMotionData _sRD2ArmMotionData;
        /// 
        /// sRD2Arm当前位置
        /// 
        private string _sRD2ArmCurrentStation;
        #endregion
        #region SRD2Rotation
        /// 
        /// sRD2Rotation模块名称
        /// 
        private string _sRD2RotationModuleName;
        /// 
        /// sRD2Rotation运动数据
        /// 
        private CommandMotionData _sRD2RotationMotionData;
        /// 
        /// sRD2Rotation当前位置
        /// 
        private string _sRD2RotationCurrentStation;
        #endregion
        #region 系统数据
        /// 
        /// 定时器
        /// 
        DispatcherTimer _timer;
        /// 
        /// 查询后台数据集合
        /// 
        private List _rtDataKeys = new List();
        /// 
        /// rt查询key数值字典
        /// 
        private Dictionary _rtDataValueDic = new Dictionary();
        #endregion
        #region AWC
        /// 
        /// AWC ModuleName
        /// 
        private ModuleName _awcModuleName;
        /// 
        /// Is AWC Cycle runnning
        /// 
        private bool _isAWCCycling;
        #endregion
        #endregion
        #region 属性
        #region SRD1Arm
        /// 
        /// SRD1Arm名称
        /// 
        public string SRD1ArmModuleName
        {
            get { return _sRD1ArmModuleName; }
            set { SetProperty(ref _sRD1ArmModuleName, value); }
        }
        /// 
        /// SRD1Arm运动数据
        /// 
        public CommandMotionData SRD1ArmMotionData
        {
            get { return _sRD1ArmMotionData; }
            set { SetProperty(ref _sRD1ArmMotionData, value); }
        }
        /// 
        /// SRD1Arm当前位置
        /// 
        public string SRD1ArmCurrentStation
        {
            get { return _sRD1ArmCurrentStation; }
            set { SetProperty(ref _sRD1ArmCurrentStation, value); }
        }
        #endregion
        #region SRD1Rotation
        /// 
        /// SRD1Rotation名称
        /// 
        public string SRD1RotationModuleName
        {
            get { return _sRD1RotationModuleName; }
            set { SetProperty(ref _sRD1RotationModuleName, value); }
        }
        /// 
        /// SRD1Rotation运动数据
        /// 
        public CommandMotionData SRD1RotationMotionData
        {
            get { return _sRD1RotationMotionData; }
            set { SetProperty(ref _sRD1RotationMotionData, value); }
        }
        /// 
        /// SRD1Rotation当前位置
        /// 
        public string SRD1RotationCurrentStation
        {
            get { return _sRD1RotationCurrentStation; }
            set { SetProperty(ref _sRD1RotationCurrentStation, value); }
        }
        #endregion
        #region SRD2Arm
        /// 
        /// SRD2Arm名称
        /// 
        public string SRD2ArmModuleName
        {
            get { return _sRD2ArmModuleName; }
            set { SetProperty(ref _sRD2ArmModuleName, value); }
        }
        /// 
        /// SRD2Arm运动数据
        /// 
        public CommandMotionData SRD2ArmMotionData
        {
            get { return _sRD2ArmMotionData; }
            set { SetProperty(ref _sRD2ArmMotionData, value); }
        }
        /// 
        /// SRD2Arm当前位置
        /// 
        public string SRD2ArmCurrentStation
        {
            get { return _sRD2ArmCurrentStation; }
            set { SetProperty(ref _sRD2ArmCurrentStation, value); }
        }
        #endregion
        #region SRD2Rotation
        /// 
        /// SRD2Rotation名称
        /// 
        public string SRD2RotationModuleName
        {
            get { return _sRD2RotationModuleName; }
            set { SetProperty(ref _sRD2RotationModuleName, value); }
        }
        /// 
        /// SRD1Rotation运动数据
        /// 
        public CommandMotionData SRD2RotationMotionData
        {
            get { return _sRD2RotationMotionData; }
            set { SetProperty(ref _sRD2RotationMotionData, value); }
        }
        /// 
        /// SRD2Rotation当前位置
        /// 
        public string SRD2RotationCurrentStation
        {
            get { return _sRD2RotationCurrentStation; }
            set { SetProperty(ref _sRD2RotationCurrentStation, value); }
        }
        #endregion
        #region AWC
        /// 
        /// AWC ModuleName
        /// 
        public ModuleName AWCModuleName
        {
            get { return _awcModuleName; }
            set { SetProperty(ref _awcModuleName, value); }
        }
        /// 
        /// Is AWC Cycle runnning
        /// 
        public bool IsAWCCycling
        {
            get { return _isAWCCycling; }
            set { SetProperty(ref _isAWCCycling, value); }
        }
        #endregion
        #endregion
        /// 
        /// 构造函数
        /// 
        public SRDStationSetupViewModel()
        {
        }
        /// 
        /// 加载数据
        /// 
        public void LoadData(string systemName)
        {
            SRD1ArmModuleName = $"{ModuleName.SRD1}.Arm";
            SRD1RotationModuleName = $"{ModuleName.SRD1}.Rotation";
            SRD2ArmModuleName = $"{ModuleName.SRD2}.Arm";
            SRD2RotationModuleName = $"{ModuleName.SRD2}.Rotation";
            AddDataKeys();
            if (_timer == null)
            {
                _timer = new DispatcherTimer();
                _timer.Interval = TimeSpan.FromMilliseconds(200);
                _timer.Tick += Timer_Tick; ;
            }
            _timer.Start();
        }
        /// 
        /// 定时器执行
        /// 
        /// 
        /// 
        private void Timer_Tick(object sender, EventArgs e)
        {
            OnTimer();
        }
        /// 
        /// 隐藏
        /// 
        public void Hide()
        {
            if (_timer != null)
            {
                _timer.Stop();
            }
        }
        /// 
        /// 初始化查询数据集合
        /// 
        private void AddDataKeys()
        {
            _rtDataKeys.Clear();
            _rtDataKeys.Add($"{SRD1ArmModuleName}.{MOTION_DATA}");
            _rtDataKeys.Add($"{SRD1ArmModuleName}.{IS_SWITCH_ON}");
            _rtDataKeys.Add($"{SRD1ArmModuleName}.{CURRENT_STATION}");
            _rtDataKeys.Add($"{SRD2ArmModuleName}.{MOTION_DATA}");
            _rtDataKeys.Add($"{SRD2ArmModuleName}.{IS_SWITCH_ON}");
            _rtDataKeys.Add($"{SRD2ArmModuleName}.{CURRENT_STATION}");
            _rtDataKeys.Add($"{SRD1RotationModuleName}.{MOTION_DATA}");
            _rtDataKeys.Add($"{SRD1RotationModuleName}.{IS_SWITCH_ON}");
            _rtDataKeys.Add($"{SRD1RotationModuleName}.{CURRENT_STATION}");
            _rtDataKeys.Add($"{SRD2RotationModuleName}.{MOTION_DATA}");
            _rtDataKeys.Add($"{SRD2RotationModuleName}.{IS_SWITCH_ON}");
            _rtDataKeys.Add($"{SRD2RotationModuleName}.{CURRENT_STATION}");
            _rtDataKeys.Add($"{SRD2RotationModuleName}.{CURRENT_STATION}");
            _rtDataKeys.Add($"SRD1.IsAWCCycling");
            _rtDataKeys.Add($"SRD2.IsAWCCycling");
        }
        /// 
        /// 定时器
        /// 
        /// 
        private Boolean OnTimer()
        {
            if(_rtDataKeys.Count != 0)
            {
                _rtDataValueDic = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
                if (_rtDataValueDic != null)
                {
                    SRD1ArmMotionData = CommonFunction.GetValue(_rtDataValueDic, $"{SRD1ArmModuleName}.{MOTION_DATA}");
                    SRD1ArmCurrentStation = CommonFunction.GetCurrentStationLastContent(CommonFunction.GetValue(_rtDataValueDic, $"{SRD1ArmModuleName}.{CURRENT_STATION}"), SRD1ArmModuleName);
                    SRD2ArmMotionData = CommonFunction.GetValue(_rtDataValueDic, $"{SRD2ArmModuleName}.{MOTION_DATA}");
                    SRD2ArmCurrentStation = CommonFunction.GetCurrentStationLastContent(CommonFunction.GetValue(_rtDataValueDic, $"{SRD2ArmModuleName}.{CURRENT_STATION}"), SRD2ArmModuleName);
                    SRD1RotationMotionData = CommonFunction.GetValue(_rtDataValueDic, $"{SRD1RotationModuleName}.{MOTION_DATA}");
                    SRD1RotationCurrentStation = CommonFunction.GetCurrentStationLastContent(CommonFunction.GetValue(_rtDataValueDic, $"{SRD1RotationModuleName}.{CURRENT_STATION}"), SRD1RotationModuleName);
                    SRD2RotationMotionData = CommonFunction.GetValue(_rtDataValueDic, $"{SRD2RotationModuleName}.{MOTION_DATA}");
                    SRD2RotationCurrentStation = CommonFunction.GetCurrentStationLastContent(CommonFunction.GetValue(_rtDataValueDic, $"{SRD2RotationModuleName}.{CURRENT_STATION}"), SRD2RotationModuleName);  
                    //判断AWC是否在running
                    if(AWCModuleName == ModuleName.SRD1)
                    {
                        IsAWCCycling = CommonFunction.GetValue(_rtDataValueDic, $"SRD1.IsAWCCycling"); ;
                    }else if(AWCModuleName == ModuleName.SRD2)
                    {
                        IsAWCCycling = CommonFunction.GetValue(_rtDataValueDic, $"SRD2.IsAWCCycling"); ;
                    }
                    else
                    {
                        IsAWCCycling = false;
                    }
                }
            }
            return true;
        }
    }
}