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 CyberX8_MainPages.ViewModels
{
    public class SRDMotionViewModel:BindableBase
    {
        #region 常量
        private const string MOTION_DATA = "MotionData";
        private const int ACCEL_FACTOR = 10;
        #endregion

        #region 内部变量

        #region SRD1Arm
        /// <summary>
        /// SRD1Arm模块名称
        /// </summary>
        private string _sRD1ArmModuleName;
        /// <summary>
        /// 数据
        /// </summary>
        private CommandMotionData _sRD1ArmMotionData;
        #endregion

        #region SRD1Rotation
        /// <summary>
        /// SRD1Rotation模块名称
        /// </summary>
        private string _sRD1RotationModuleName;
        /// <summary>
        /// 数据
        /// </summary>
        private CommandMotionData _sRD1RotationMotionData;
        #endregion

        #region SRD2Arm
        /// <summary>
        /// SRD2Arm模块名称
        /// </summary>
        private string _sRD2ArmModuleName;
        /// <summary>
        /// 数据
        /// </summary>
        private CommandMotionData _sRD2ArmMotionData;
        #endregion

        #region SRD2Rotation
        /// <summary>
        /// SRD2Rotation模块名称
        /// </summary>
        private string _sRD2RotationModuleName;
        /// <summary>
        /// 数据
        /// </summary>
        private CommandMotionData _sRD2RotationMotionData;
        #endregion

        /// <summary>
        /// 增加
        /// </summary>
        private double _incrementValue = 0.01;
        /// <summary>
        /// 定时器
        /// </summary>
        DispatcherTimer _timer;
        /// <summary>
        /// RT查询key集合
        /// </summary>
        private List<string> _rtDataKeys = new List<string>();
        /// <summary>
        /// rt查询key数值字典
        /// </summary>
        private Dictionary<string, object> _rtDataValueDic = new Dictionary<string, object>();

        #endregion

        #region 属性

        #region SRD1Arm
        /// <summary>
        /// SRD1Arm名称
        /// </summary>
        public string SRD1ArmModuleName
        {
            get { return _sRD1ArmModuleName; }
            set { SetProperty(ref _sRD1ArmModuleName, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        public CommandMotionData SRD1ArmMotionData
        {
            get { return _sRD1ArmMotionData; }
            set { SetProperty(ref _sRD1ArmMotionData, value); }
        }
        #endregion

        #region SRD1Rotation
        /// <summary>
        /// SRD1Rotation名称
        /// </summary>
        public string SRD1RotaionModuleName
        {
            get { return _sRD1RotationModuleName; }
            set { SetProperty(ref _sRD1RotationModuleName, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        public CommandMotionData SRD1RotationMotionData
        {
            get { return _sRD1RotationMotionData; }
            set { SetProperty(ref _sRD1RotationMotionData, value); }
        }
        #endregion

        #region SRD2Arm
        /// <summary>
        /// SRD2Arm名称
        /// </summary>
        public string SRD2ArmModuleName
        {
            get { return _sRD2ArmModuleName; }
            set { SetProperty(ref _sRD2ArmModuleName, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        public CommandMotionData SRD2ArmMotionData
        {
            get { return _sRD2ArmMotionData; }
            set { SetProperty(ref _sRD2ArmMotionData, value); }
        }
        #endregion

        #region SRD2Rotation
        /// <summary>
        /// SRD2Rotation名称
        /// </summary>
        public string SRD2RotaionModuleName
        {
            get { return _sRD2RotationModuleName; }
            set { SetProperty(ref _sRD2RotationModuleName, value); }
        }
        /// <summary>
        /// 
        /// </summary>
        public CommandMotionData SRD2RotationMotionData
        {
            get { return _sRD2RotationMotionData; }
            set { SetProperty(ref _sRD2RotationMotionData, value); }
        }
        #endregion

        /// <summary>
        /// 步进
        /// </summary>
        public double IncrementValue
        {
            get { return _incrementValue; }
            set { SetProperty(ref _incrementValue, value); }
        }

        #endregion

        /// <summary>
        /// 加载数据
        /// </summary>
        public void LoadData(string systemName)
        {
            SRD1ArmModuleName = $"{ModuleName.SRD1}.Arm";
            SRD1RotaionModuleName = $"{ModuleName.SRD1}.Rotation";
            SRD2ArmModuleName = $"{ModuleName.SRD2}.Arm";
            SRD2RotaionModuleName = $"{ModuleName.SRD2}.Rotation";
            _rtDataKeys.Clear();
            _rtDataKeys.Add($"{SRD1ArmModuleName}.IsHomed");
            _rtDataKeys.Add($"{SRD1ArmModuleName}.IsSwitchOn");
            _rtDataKeys.Add($"{SRD1ArmModuleName}.{MOTION_DATA}");

            _rtDataKeys.Add($"{SRD1RotaionModuleName}.IsHomed");
            _rtDataKeys.Add($"{SRD1RotaionModuleName}.IsSwitchOn");
            _rtDataKeys.Add($"{SRD1RotaionModuleName}.{MOTION_DATA}");

            _rtDataKeys.Add($"{SRD2ArmModuleName}.IsHomed");
            _rtDataKeys.Add($"{SRD2ArmModuleName}.IsSwitchOn");
            _rtDataKeys.Add($"{SRD2ArmModuleName}.{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();
        }
        /// <summary>
        /// 定时器执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_Tick(object sender, EventArgs e)
        {
            if (_rtDataKeys.Count != 0)
            {
                _rtDataValueDic = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
                if (_rtDataValueDic != null)
                {
                    SRD1ArmMotionData = CommonFunction.GetValue<CommandMotionData>(_rtDataValueDic, $"{SRD1ArmModuleName}.{MOTION_DATA}");

                    CommandMotionData tmp1 = CommonFunction.GetValue<CommandMotionData>(_rtDataValueDic, $"{SRD1RotaionModuleName}.{MOTION_DATA}");
                    if(tmp1 != null)
                    {
                        tmp1.ActualVelocity = Math.Round(tmp1.ActualVelocity / 6, 2);
                        tmp1.ProfileVelocity = Math.Round(tmp1.ProfileVelocity / 6, 2);
                        tmp1.HomingVelocity = Math.Round(tmp1.HomingVelocity / 6, 2);
                        tmp1.HomingVelocitySlow = Math.Round(tmp1.HomingVelocitySlow / 6, 2);
                        tmp1.ProfileDecel = tmp1.ProfileDecel * ACCEL_FACTOR;
                        tmp1.ProfileAccel = tmp1.ProfileAccel * ACCEL_FACTOR;
                    }                    
                    SRD1RotationMotionData = tmp1;

                    SRD2ArmMotionData = CommonFunction.GetValue<CommandMotionData>(_rtDataValueDic, $"{SRD2ArmModuleName}.{MOTION_DATA}");
                    CommandMotionData tmp2 = CommonFunction.GetValue<CommandMotionData>(_rtDataValueDic, $"{SRD2RotaionModuleName}.{MOTION_DATA}");
                    if (tmp2 != null)
                    {
                        tmp2.ActualVelocity = Math.Round(tmp2.ActualVelocity / 6, 2);
                        tmp2.ProfileVelocity = Math.Round(tmp2.ProfileVelocity / 6, 2);
                        tmp2.HomingVelocity = Math.Round(tmp2.HomingVelocity / 6, 2);
                        tmp2.HomingVelocitySlow = Math.Round(tmp2.HomingVelocitySlow / 6, 2);
                        tmp2.ProfileDecel = tmp2.ProfileDecel * ACCEL_FACTOR;
                        tmp2.ProfileAccel = tmp2.ProfileAccel * ACCEL_FACTOR;
                    }
                    SRD2RotationMotionData = tmp2;

                }
            }
        }
        /// <summary>
        /// 隐藏
        /// </summary>
        public void Hide()
        {
            _timer.Stop();
        }




    }
}