using MainShell.Common;
using MainShell.Hardware;
using MwFramework.Controls.UIControl;
using MwFramework.Device;
using MwFramework.ManagerService;
using Stylet;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MainShell.ParaSetting.Model
{
    [Export(typeof(IParameter))]
    public class SpeedSetting : ParameterBase
    {
        [Export(typeof(IParameter))]
        public class SpeedParaSysSetting : ParameterBase
        {
            [WatchValue("速度参数")]
            public SpeedTypeItemCollection SpeedTypeItemCollection { get; set; } = new SpeedTypeItemCollection();
            public SpeedType CurrentSpeedType { get; set; } = SpeedType.Low;
            public override void Copy(IParameter source)
            {
                if (source is SpeedParaSysSetting setting)
                {
                    ReflectionExtension.Copy(SpeedTypeItemCollection, setting.SpeedTypeItemCollection);
                    CurrentSpeedType = setting.CurrentSpeedType;
                }
            }
           
        }
      
        public class SpeedTypeItemCollection : PropertyChangedBase, IParameterItem
        {

            private ObservableCollection<SpeedTypeItem> _speedTypeItemList = new ObservableCollection<SpeedTypeItem>();
            [WatchValue("SpeedTypeItemList")]
            public ObservableCollection<SpeedTypeItem> SpeedTypeItemList
            {
                get { return _speedTypeItemList; }
                set { if (_speedTypeItemList != value) { _speedTypeItemList = value; OnPropertyChanged(nameof(SpeedTypeItemList)); } }
            }

            private SpeedTypeItem _currentSelectSpeedTypeItemList = new SpeedTypeItem();
            public SpeedTypeItem CurrentSelectSpeedTypeItemList
            {
                get { return _currentSelectSpeedTypeItemList; }
                set { if (_currentSelectSpeedTypeItemList != value) { _currentSelectSpeedTypeItemList = value; OnPropertyChanged(nameof(CurrentSelectSpeedTypeItemList)); } }
            }

            public IParameterItem Clone()
            {
                return this.MemberwiseClone() as IParameterItem;
            }
        }
        public class SpeedTypeItem : PropertyChangedBase, IParameterItem
        {

            private string _axisName;
            [WatchValue("轴名称")]
            public string AxisName
            {
                get { return _axisName; }
                set { SetAndNotify(ref _axisName, value); }
            }

            private double _speed = 10.0;
            [WatchValue("速度")]
            public double Speed
            {
                get { return _speed; }
                set { if (_speed != value) { _speed = value; OnPropertyChanged(nameof(Speed)); } }
            }
            private double _acc = 100.0;
            [WatchValue("加速度")]
            public double Acc
            {
                get { return _acc; }
                set { if (_acc != value) { _acc = value; OnPropertyChanged(nameof(Acc)); } }
            }
            private double _dec = 100.0;
            [WatchValue("减速度")]
            public double Dec
            {
                get { return _dec; }
                set { if (_dec != value) { _dec = value; OnPropertyChanged(nameof(Dec)); } }
            }
            private double _jerk = 1000.0;
            [WatchValue("加加速度")]
            public double Jerk
            {
                get { return _jerk; }
                set { if (_jerk != value) { _jerk = value; OnPropertyChanged(nameof(Jerk)); } }
            }
            private double _lowPercent = 20;
            [WatchValue("低速")]
            public double LowPercent
            {
                get { return _lowPercent; }
                set { LimitPercent(ref value); SetAndNotify(ref _lowPercent, value); }
            }

            private double _medianPercent = 50;
            [WatchValue("中速")]
            public double MedianPercent
            {
                get { return _medianPercent; }
                set { LimitPercent(ref value); SetAndNotify(ref _medianPercent, value); }
            }

            private double _highPercent = 100;
            [WatchValue("高速")]
            public double HighPercent
            {
                get { return _highPercent; }
                set { LimitPercent(ref value); SetAndNotify(ref _highPercent, value); }
            }

            private void LimitPercent(ref double percent)
            {
                if (percent < 0) percent = 1;
                else if (percent > 100) percent = 100;

            }
            public IParameterItem Clone()
            {
                return this.MemberwiseClone() as IParameterItem;
            }
        }
    }
}
