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)
        {
            viewModel = new PgFlyAdVm();
            viewModel.Init(flyAdService, gageService);
            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; }
        #endregion

        public int TargetPos { get; set; }

        #region Command
        public RelayCommand ApplyCmd { 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;

        public PgFlyAdVm()
        {
            ApplyCmd = new RelayCommand(Apply);
            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)
        {
            this.flyAdService = flyAdService;
            this.gageService = gageService;

            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));

        }
        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;

            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);
        }
    }
}