using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using MECF.Framework.Common.CommonData.PUF;
using MECF.Framework.Common.DataCenter;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.Utilities;
using Prism.Mvvm;
namespace PunkHPX8_MainPages.ViewModels
{
    public class SRDMotionViewModel:BindableBase
    {
        #region 常量
        private const string MOTION_DATA = "MotionData";
        private const int ACCEL_FACTOR = 10;
        #endregion
        #region 内部变量   
        #region SRD1Rotation
        /// 
        /// SRD1Rotation模块名称
        /// 
        private string _sRD1RotationModuleName;
        /// 
        /// 数据
        /// 
        private CommandMotionData _sRD1RotationMotionData;
        #endregion
        #region SRD2Rotation
        /// 
        /// SRD2Rotation模块名称
        /// 
        private string _sRD2RotationModuleName;
        /// 
        /// 数据
        /// 
        private CommandMotionData _sRD2RotationMotionData;
        #endregion
        /// 
        /// 增加
        /// 
        private double _incrementValue = 0.01;
        /// 
        /// 定时器
        /// 
        DispatcherTimer _timer;
        /// 
        /// RT查询key集合
        /// 
        private List _rtDataKeys = new List();
        /// 
        /// rt查询key数值字典
        /// 
        private Dictionary _rtDataValueDic = new Dictionary();
        #endregion
        #region 属性
        #region SRD1Rotation
        /// 
        /// SRD1Rotation名称
        /// 
        public string SRD1RotaionModuleName
        {
            get { return _sRD1RotationModuleName; }
            set { SetProperty(ref _sRD1RotationModuleName, value); }
        }
        /// 
        /// 
        /// 
        public CommandMotionData SRD1RotationMotionData
        {
            get { return _sRD1RotationMotionData; }
            set { SetProperty(ref _sRD1RotationMotionData, value); }
        }
        #endregion
        #region SRD2Rotation
        /// 
        /// SRD2Rotation名称
        /// 
        public string SRD2RotaionModuleName
        {
            get { return _sRD2RotationModuleName; }
            set { SetProperty(ref _sRD2RotationModuleName, value); }
        }
        /// 
        /// 
        /// 
        public CommandMotionData SRD2RotationMotionData
        {
            get { return _sRD2RotationMotionData; }
            set { SetProperty(ref _sRD2RotationMotionData, value); }
        }
        #endregion
        /// 
        /// 步进
        /// 
        public double IncrementValue
        {
            get { return _incrementValue; }
            set { SetProperty(ref _incrementValue, value); }
        }
        #endregion
        /// 
        /// 加载数据
        /// 
        public void LoadData(string systemName)
        {
         
            SRD1RotaionModuleName = $"{ModuleName.SRD1}.Rotation";
            SRD2RotaionModuleName = $"{ModuleName.SRD2}.Rotation";
            _rtDataKeys.Clear();
            _rtDataKeys.Add($"{SRD1RotaionModuleName}.IsHomed");
            _rtDataKeys.Add($"{SRD1RotaionModuleName}.IsSwitchOn");
            _rtDataKeys.Add($"{SRD1RotaionModuleName}.{MOTION_DATA}");
            _rtDataKeys.Add($"{SRD2RotaionModuleName}.IsHomed");
            _rtDataKeys.Add($"{SRD2RotaionModuleName}.IsSwitchOn");
            _rtDataKeys.Add($"{SRD2RotaionModuleName}.{MOTION_DATA}");
            if (_timer == null)
            {
                _timer = new DispatcherTimer();
                _timer.Interval = TimeSpan.FromMilliseconds(500);
                _timer.Tick += Timer_Tick;
            }
            _timer.Start();
        }
        /// 
        /// 定时器执行
        /// 
        /// 
        /// 
        private void Timer_Tick(object sender, EventArgs e)
        {
            if (_rtDataKeys.Count != 0)
            {
                _rtDataValueDic = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
                if (_rtDataValueDic != null)
                {
                    CommandMotionData tmp1 = CommonFunction.GetValue(_rtDataValueDic, $"{SRD1RotaionModuleName}.{MOTION_DATA}");
                    if(tmp1 != null)
                    {
                        tmp1.ActualVelocity = Math.Round(tmp1.ActualVelocity , 2);  
                        tmp1.ProfileVelocity = Math.Round(tmp1.ProfileVelocity , 2);
                        tmp1.HomingVelocity = Math.Round(tmp1.HomingVelocity , 2);
                        tmp1.HomingVelocitySlow = Math.Round(tmp1.HomingVelocitySlow, 2);
                        tmp1.ProfileDecel = tmp1.ProfileDecel;
                        tmp1.ProfileAccel = tmp1.ProfileAccel;
                    }                    
                    SRD1RotationMotionData = tmp1;
                    CommandMotionData tmp2 = CommonFunction.GetValue(_rtDataValueDic, $"{SRD2RotaionModuleName}.{MOTION_DATA}");
                    if (tmp2 != null)
                    {
                        tmp2.ActualVelocity = Math.Round(tmp2.ActualVelocity, 2);
                        tmp2.ProfileVelocity = Math.Round(tmp2.ProfileVelocity, 2);
                        tmp2.HomingVelocity = Math.Round(tmp2.HomingVelocity, 2);
                        tmp2.HomingVelocitySlow = Math.Round(tmp2.HomingVelocitySlow, 2);
                        tmp2.ProfileDecel = tmp2.ProfileDecel;
                        tmp2.ProfileAccel = tmp2.ProfileAccel;
                    }
                    SRD2RotationMotionData = tmp2;
                }
            }
        }
        /// 
        /// 隐藏
        /// 
        public void Hide()
        {
            _timer.Stop();
        }
    }
}