using Aitex.Core.UI.MVVM;
using Aitex.Core.Utilities;
using MECF.Framework.Common.Beckhoff.AxisProvider;
using MECF.Framework.Common.CommonData.PUF;
using MECF.Framework.Common.OperationCenter;
using PunkHPX8_Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace PunkHPX8_Themes.UserControls
{
    /// 
    /// MotionControl.xaml 的交互逻辑
    /// 
    public partial class MotionControl : UserControl
    {
        #region 属性
        public static readonly DependencyProperty ModuleNameProperty = DependencyProperty.Register(
          "ModuleName", typeof(string), typeof(MotionControl),new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// 模块名称
        /// 
        public string ModuleName
        {
            get
            {
                return (string)this.GetValue(ModuleNameProperty);
            }
            set
            {
                this.SetValue(ModuleNameProperty, value);
            }
        }
        public static readonly DependencyProperty ModuleTitleProperty = DependencyProperty.Register(
            "ModuleTitle", typeof(string), typeof(MotionControl),new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// 标题
        /// 
        public string ModuleTitle
        {
            get
            {
                return (string)this.GetValue(ModuleTitleProperty);
            }
            set
            {
                this.SetValue(ModuleTitleProperty, value);
            }
        }
        public static readonly DependencyProperty ModuleUnitProperty = DependencyProperty.Register(
           "ModuleUnit", typeof(string), typeof(MotionControl), new FrameworkPropertyMetadata("deg", FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// 标题
        /// 
        public string ModuleUnit
        {
            get
            {
                return (string)this.GetValue(ModuleUnitProperty);
            }
            set
            {
                this.SetValue(ModuleUnitProperty, value);
            }
        }
        public static readonly DependencyProperty InputSpeedProperty = DependencyProperty.Register(
           "InputSpeed", typeof(double), typeof(MotionControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// InSpeed
        /// 
        public double InputSpeed
        {
            get
            {
                return (double)this.GetValue(InputSpeedProperty);
            }
            set
            {
                this.SetValue(InputSpeedProperty, value);
            }
        }
        public static readonly DependencyProperty InputAccelerationProperty = DependencyProperty.Register(
          "InputAcceleration", typeof(double), typeof(MotionControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// InSpeed
        /// 
        public double InputAcceleration
        {
            get
            {
                return (double)this.GetValue(InputAccelerationProperty);
            }
            set
            {
                this.SetValue(InputAccelerationProperty, value);
            }
        }
        public static readonly DependencyProperty InputDecelerationProperty = DependencyProperty.Register(
         "InputDeceleration", typeof(double), typeof(MotionControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// InputDeceleration
        /// 
        public double InputDeceleration
        {
            get
            {
                return (double)this.GetValue(InputDecelerationProperty);
            }
            set
            {
                this.SetValue(InputDecelerationProperty, value);
            }
        }
        public static readonly DependencyProperty InputHomeSpeedSwitchProperty = DependencyProperty.Register(
        "InputHomeSpeedSwitch", typeof(double), typeof(MotionControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// InputHomeSpeedSwitch
        /// 
        public double InputHomeSpeedSwitch
        {
            get
            {
                return (double)this.GetValue(InputHomeSpeedSwitchProperty);
            }
            set
            {
                this.SetValue(InputHomeSpeedSwitchProperty, value);
            }
        }
        public static readonly DependencyProperty InputHomeSpeedIndexProperty = DependencyProperty.Register(
        "InputHomeSpeedIndex", typeof(double), typeof(MotionControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// InputHomeSpeedIndex
        /// 
        public double InputHomeSpeedIndex
        {
            get
            {
                return (double)this.GetValue(InputHomeSpeedIndexProperty);
            }
            set
            {
                this.SetValue(InputHomeSpeedIndexProperty, value);
            }
        }
        public static readonly DependencyProperty JogValueIndexProperty = DependencyProperty.Register(
        "JogValue", typeof(double), typeof(MotionControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// JogValue
        /// 
        public double JogValue
        {
            get
            {
                return (double)this.GetValue(JogValueIndexProperty);
            }
            set
            {
                this.SetValue(JogValueIndexProperty, value);
            }
        }
        public static readonly DependencyProperty InputHomeAccelDecelProperty = DependencyProperty.Register(
        "InputHomeAccelDecel", typeof(double), typeof(MotionControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// InputHomeAccelDecel
        /// 
        public double InputHomeAccelDecel
        {
            get
            {
                return (double)this.GetValue(InputHomeAccelDecelProperty);
            }
            set
            {
                this.SetValue(InputHomeAccelDecelProperty, value);
            }
        }
        public static readonly DependencyProperty MotionDataProperty = DependencyProperty.Register(
          "MotionData", typeof(CommandMotionData), typeof(MotionControl), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnItemsSourceChanged)));
        /// 
        /// MotionData
        /// 
        public CommandMotionData MotionData
        {
            get
            {
                return (CommandMotionData)this.GetValue(MotionDataProperty);
            }
            set
            {
                this.SetValue(MotionDataProperty, value);
            }
        }
        private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if(e.NewValue!=null)
            {
                CommandMotionData data = (CommandMotionData)e.NewValue;
                d.SetValue(InputSpeedProperty,data.FileProfileVelocity);
                d.SetValue(InputAccelerationProperty, data.FileAcceleration);
                d.SetValue(InputDecelerationProperty, data.FileDeceleration);
                d.SetValue(InputHomeSpeedSwitchProperty, data.FileHomingVelocity);
                d.SetValue(InputHomeSpeedIndexProperty, data.FileHomingVelocitySlow);
                d.SetValue(InputHomeAccelDecelProperty, data.FileHomingAccel);
            }
        }
        public static readonly DependencyProperty IncrementValueProperty = DependencyProperty.Register("IncrementValue", typeof(double), typeof(MotionControl));
        /// 
        /// 当前位置
        /// 
        public double IncrementValue
        {
            get
            {
                return (double)this.GetValue(IncrementValueProperty);
            }
            set
            {
                this.SetValue(IncrementValueProperty, Math.Round(value, 2));
            }
        }
        [IgnorePropertyChange]
        public ICommand KeyDownCommand
        {
            get;
            private set;
        }
        #endregion
        /// 
        /// 构造函数
        /// 
        public MotionControl()
        {
            KeyDownCommand=new DelegateCommand