using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using FLY.Thick.Base.UI.Converter;
using System.Net;

using FLY.Thick.Base.Client;
using FLY.Thick.Base.Common;
using Unity;
using FLY.Thick.Base.IService;
using System.ComponentModel;
using GalaSoft.MvvmLight.Command;

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 int 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; }
        #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, "HasCRC", this, "HasCRC");
            Misc.BindingOperations.SetBinding(this.flyAdService, "EP", this, "EP");
            Misc.BindingOperations.SetBinding(this.flyAdService, "PosOfGrid", this, "PosOfGrid");
            Misc.BindingOperations.SetBinding(this.flyAdService, "MotorType", this, "MotorType");
            Misc.BindingOperations.SetBinding(this.flyAdService, "Ratio01", this, "Ratio01");
            Misc.BindingOperations.SetBinding(this.flyAdService, "Ratio02", this, "Ratio02");
            Misc.BindingOperations.SetBinding(this.flyAdService, "PosOffset", this, "PosOffset");
            Misc.BindingOperations.SetBinding(this.flyAdService, "JogVelocity", this, "JogVelocity");
            Misc.BindingOperations.SetBinding(this.flyAdService, "GridSmooth", this, "GridSmooth");
        }
        private void Apply()
        {
            if (!WdPassword.Authorize("FlyAd"))
                return;

            flyAdService.HasCRC=this.HasCRC;
            flyAdService.EP=this.EP;
            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.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);
        }
    }
}
