using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using GalaSoft.MvvmLight.Command;
using System;
using System.ComponentModel;
using System.Net;
using System.Windows.Controls;
using Unity;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// Page_FlyAD.xaml 的交互逻辑
    /// </summary>
    public partial class PgFlyAd : Page
    {
        PgFlyAdVm viewModel;
        public PgFlyAd()
        {
            InitializeComponent();
        }

        [InjectionMethod]
        public void Init(
            IUnityContainer container,
            IFlyADService flyAdService,
            ITDGageService gageService,
            IInitParamService initParamService)
        {
            viewModel = new PgFlyAdVm();
            viewModel.Init(flyAdService, gageService, initParamService);
            this.DataContext = viewModel;
            container.BuildUp(mircoGage);
        }


    }
    public class PgFlyAdVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        #region 参数
        public bool HasCRC { get; set; }
        public IPEndPoint EP { get; set; }
        public UInt16 PosOfGrid { get; set; }
        public MOTORTYPE MotorType { set; get; }
        public UInt16 Ratio01 { get; set; }
        public UInt16 Ratio02 { get; set; }
        public Int16 PosOffset { set; get; }//脉冲平移
        public UInt32 JogVelocity { set; get; }
        public int GridSmooth { get; set; }
        public bool IsCalSpeed { get; set; }
        public int ADLag { get; set; }
        public bool HasPosMaxMin { get; set; }
        public int PosMin { get; set; }
        public int PosMax { get; set; }
        #endregion

        public int Version { get; set; }
        public int HardwareVersion { get; set; }

        #region initparam
        public double Encoder1_mmpp { get; set; }
        public int PosLength { get; set; }
        /// <summary>
        /// 设置的速度，与 实际速度比例 Speed1 = VScan*Speed1Scale
        /// </summary>
        public double Speed1Scale { get; set; }
        #region 速度


        /// <summary>
        /// 调试时速度，向前走，向后走 Velocity Of JOG
        /// </summary>
        public UInt32 VJOG { get; set; }

        /// <summary>
        /// 开始速度 Start Velocity
        /// </summary>
        public UInt32 SVelocity { set; get; }
        /// <summary>
        /// 加速时间
        /// </summary>
        public UInt32 ATime { set; get; }
        /// <summary>
        /// 减速时间
        /// </summary>
        public UInt32 DTime { set; get; }
        /// <summary>
        /// 归0速度1
        /// </summary>
        public UInt32 HVelocity1 { set; get; }
        /// <summary>
        /// 归0速度2
        /// </summary>
        public UInt32 HVelocity2 { set; get; }

        #endregion
        #endregion
        public int TargetPos { get; set; }

        #region Command
        public RelayCommand ApplyCmd { get; }
        public RelayCommand Apply2Cmd { get; }
        public RelayCommand RunToCmd { get; }
        public RelayCommand BackwCmd { get; }
        public RelayCommand ForwCmd { get; }
        public RelayCommand OrgCmd { get; }
        public RelayCommand StopCmd { get; }
        #endregion

        IFlyADService flyAdService;
        ITDGageService gageService;
        IInitParamService initParamService;

        public PgFlyAdVm()
        {
            ApplyCmd = new RelayCommand(Apply);
            Apply2Cmd = new RelayCommand(Apply2);
            RunToCmd = new RelayCommand(RunTo);
            BackwCmd = new RelayCommand(Backw);
            ForwCmd = new RelayCommand(Forw);
            OrgCmd = new RelayCommand(Org);
            StopCmd = new RelayCommand(Stop);
        }

        public void Init(
            IFlyADService flyAdService,
            ITDGageService gageService,
            IInitParamService initParamService)
        {
            this.flyAdService = flyAdService;
            this.gageService = gageService;
            this.initParamService = initParamService;
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.HasCRC), this, nameof(HasCRC));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.Addr), () =>
            {
                EP = Misc.StringConverter.ToIPEndPoint(this.flyAdService.Addr);
            });
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.PosOfGrid), this, nameof(PosOfGrid));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.MotorType), this, nameof(MotorType));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.Ratio01), this, nameof(Ratio01));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.Ratio02), this, nameof(Ratio02));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.PosOffset), this, nameof(PosOffset));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.JogVelocity), this, nameof(JogVelocity));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.GridSmooth), this, nameof(GridSmooth));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.IsCalSpeed), this, nameof(IsCalSpeed));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.ADLag), this, nameof(ADLag));
            
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.HasPosMaxMin), this, nameof(HasPosMaxMin));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.PosMin), this, nameof(PosMin));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.PosMax), this, nameof(PosMax));

            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.Verson), this, nameof(Version));
            Misc.BindingOperations.SetBinding(this.flyAdService, nameof(this.flyAdService.HardwareVersion), this, nameof(HardwareVersion));


            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.Encoder1_mmpp), this, nameof(Encoder1_mmpp));
            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.Speed1Scale), this, nameof(Speed1Scale));
            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.PosLength), this, nameof(PosLength));
            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.VJOG), this, nameof(VJOG));
            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.SVelocity), this, nameof(SVelocity));
            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.ATime), this, nameof(ATime));
            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.DTime), this, nameof(DTime));
            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.HVelocity1), this, nameof(HVelocity1));
            Misc.BindingOperations.SetBinding(this.initParamService, nameof(this.initParamService.HVelocity2), this, nameof(HVelocity2));
        }
        private void Apply()
        {
            if (this.EP == null)
            {
                FLY.ControlLibrary.Window_WarningTip.Show("警告",
                   "地址不能为空",
                   TimeSpan.FromSeconds(2));
            }
            if (!WdPassword.Authorize("FlyAd"))
                return;

            flyAdService.HasCRC = this.HasCRC;
            flyAdService.Addr = this.EP.ToString();
            flyAdService.PosOfGrid = this.PosOfGrid;
            flyAdService.MotorType = this.MotorType;
            flyAdService.Ratio01 = this.Ratio01;
            flyAdService.Ratio02 = this.Ratio02;
            flyAdService.PosOffset = this.PosOffset;
            flyAdService.JogVelocity = this.JogVelocity;
            flyAdService.GridSmooth = this.GridSmooth;
            flyAdService.IsCalSpeed = this.IsCalSpeed;
            flyAdService.ADLag = this.ADLag;

            flyAdService.HasPosMaxMin = this.HasPosMaxMin;
            flyAdService.PosMin = this.PosMin;
            flyAdService.PosMax = this.PosMax;

            flyAdService.HardwareVersion = HardwareVersion;

            FLY.ControlLibrary.Window_Tip.Show("应用成功",
                null,
                TimeSpan.FromSeconds(2));

        }

        private void Apply2()
        {

            if (!WdPassword.Authorize("InitParam"))
                return;

            initParamService.Encoder1_mmpp = Encoder1_mmpp;
            initParamService.PosLength = PosLength;
            initParamService.VJOG = this.VJOG;
            initParamService.SVelocity = this.SVelocity;
            initParamService.ATime = this.ATime;
            initParamService.DTime = this.DTime;
            initParamService.HVelocity1 = this.HVelocity1;
            initParamService.HVelocity2 = this.HVelocity2;
            initParamService.Apply();
            FLY.ControlLibrary.Window_Tip.Show("应用成功",
                null,
                TimeSpan.FromSeconds(2));

        }

        private void RunTo()
        {
            gageService.StartP2(STARTP2_MODE.RUNTO, TargetPos);
        }
        private void Stop()
        {
            gageService.StartP2(STARTP2_MODE.STOP);
        }
        private void Org()
        {
            gageService.StartP2(STARTP2_MODE.ORG);
        }
        private void Forw()
        {
            gageService.StartP2(STARTP2_MODE.FORW_UNTIL_STOP);
        }
        private void Backw()
        {
            gageService.StartP2(STARTP2_MODE.BACKW_UNTIL_STOP);
        }
    }
}
