using MECF.Framework.Common.CommonData.PUF;
using MECF.Framework.Common.DataCenter;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.Utilities;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
namespace PunkHPX8_MainPages.ViewModels
{
    public class VPWStationSetupViewModel : BindableBase
    {
        #region 常量
        private const string MOTION_DATA = "MotionData";
        private const string IS_SWITCH_ON = "IsSwitchOn";
        private const string CURRENT_STATION = "CurrentStation";
        #endregion
        #region 内部变量
        #region VPW1Rotation
        /// 
        /// VPW1Rotation模块名称
        /// 
        private string _vPW1RotationModuleName;
        /// 
        /// VPW1Rotation运动数据
        /// 
        private CommandMotionData _vPW1RotationMotionData;
        /// 
        /// VPW1Rotation当前位置
        /// 
        private string _vPW1RotationCurrentStation;
        #endregion
        #region VPW2Rotation
        /// 
        /// VPW2Rotation模块名称
        /// 
        private string _vPW2RotationModuleName;
        /// 
        /// VPW1Rotation运动数据
        /// 
        private CommandMotionData _vPW2RotationMotionData;
        /// 
        /// VPW1Rotation当前位置
        /// 
        private string _vPW2RotationCurrentStation;
        #endregion
        #region 系统数据
        /// 
        /// 定时器
        /// 
        DispatcherTimer _timer;
        /// 
        /// 查询后台数据集合
        /// 
        private List _rtDataKeys = new List();
        /// 
        /// rt查询key数值字典
        /// 
        private Dictionary _rtDataValueDic = new Dictionary();
        #endregion
        #endregion
        #region 属性
        #region VPW1Rotation
        /// 
        /// VPW1Rotation名称
        /// 
        public string VPW1RotationModuleName
        {
            get { return _vPW1RotationModuleName; }
            set { SetProperty(ref _vPW1RotationModuleName, value); }
        }
        /// 
        /// VPW1Rotation运动数据
        /// 
        public CommandMotionData VPW1RotationMotionData
        {
            get { return _vPW1RotationMotionData; }
            set { SetProperty(ref _vPW1RotationMotionData, value); }
        }
        /// 
        /// VPW1Rotation当前位置
        /// 
        public string VPW1RotationCurrentStation
        {
            get { return _vPW1RotationCurrentStation; }
            set { SetProperty(ref _vPW1RotationCurrentStation, value); }
        }
        #endregion
        #region VPW2Rotation
        /// 
        /// VPW2Rotation名称
        /// 
        public string VPW2RotationModuleName
        {
            get { return _vPW2RotationModuleName; }
            set { SetProperty(ref _vPW2RotationModuleName, value); }
        }
        /// 
        /// VPW2Rotation运动数据
        /// 
        public CommandMotionData VPW2RotationMotionData
        {
            get { return _vPW2RotationMotionData; }
            set { SetProperty(ref _vPW2RotationMotionData, value); }
        }
        /// 
        /// VPW1Rotation当前位置
        /// 
        public string VPW2RotationCurrentStation
        {
            get { return _vPW2RotationCurrentStation; }
            set { SetProperty(ref _vPW2RotationCurrentStation, value); }
        }
        #endregion
        #endregion
        /// 
        /// 加载数据
        /// 
        public void LoadData(string systemName)
        {
            VPW1RotationModuleName = $"{ModuleName.VPW1}.Rotation";
            VPW2RotationModuleName = $"{ModuleName.VPW2}.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($"{VPW1RotationModuleName}.{MOTION_DATA}");
            _rtDataKeys.Add($"{VPW1RotationModuleName}.{IS_SWITCH_ON}");
            _rtDataKeys.Add($"{VPW1RotationModuleName}.{CURRENT_STATION}");
            _rtDataKeys.Add($"{VPW2RotationModuleName}.{MOTION_DATA}");
            _rtDataKeys.Add($"{VPW2RotationModuleName}.{IS_SWITCH_ON}");
            _rtDataKeys.Add($"{VPW2RotationModuleName}.{CURRENT_STATION}");
            _rtDataKeys.Add($"{VPW2RotationModuleName}.{CURRENT_STATION}");
        }
        /// 
        /// 定时器
        /// 
        /// 
        private Boolean OnTimer()
        {
            if (_rtDataKeys.Count != 0)
            {
                _rtDataValueDic = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
                if (_rtDataValueDic != null)
                {
                    VPW1RotationMotionData = CommonFunction.GetValue(_rtDataValueDic, $"{VPW1RotationModuleName}.{MOTION_DATA}");
                    VPW1RotationCurrentStation = CommonFunction.GetCurrentStationLastContent(CommonFunction.GetValue(_rtDataValueDic, $"{VPW1RotationModuleName}.{CURRENT_STATION}"), VPW1RotationModuleName);
                    VPW2RotationMotionData = CommonFunction.GetValue(_rtDataValueDic, $"{VPW2RotationModuleName}.{MOTION_DATA}");
                    VPW2RotationCurrentStation = CommonFunction.GetCurrentStationLastContent(CommonFunction.GetValue(_rtDataValueDic, $"{VPW2RotationModuleName}.{CURRENT_STATION}"), VPW2RotationModuleName); 
                }
            }
            return true;
        }
    }
}