using CommunityToolkit.Mvvm.Input;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Unity;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// Page_Initparam.xaml 的交互逻辑
    /// </summary>
    public partial class PgInitparam : Page
    {
        PgInitParamVm viewModel;
        public PgInitparam()
        {
            InitializeComponent();
        }
        [InjectionMethod]
        public void Init(IInitParamService initParamService)
        {
            viewModel = new PgInitParamVm();
            viewModel.Init(initParamService);
            this.DataContext = viewModel;
        }
    }
    public class PgInitParamVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        #region 参数
        /// <summary>
        /// 扫描架长
        /// </summary>
        public int PosLength { get; set; }

        public bool IsTimeGridAdvEnabled { get; set; }
        /// <summary>
        /// 自动归原点间距,次数
        /// </summary>
        public int AutoOrgInterval { get; set; }


        /// <summary>
        /// 编码器1 mm/pulse
        /// </summary>
        public double Encoder1_mmpp { get; set; }

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

        /// <summary>
        /// 线速度来源
        /// </summary>
        public FilmVSRC FilmVSrc { get; set; }

        /// <summary>
        /// 最小线速度 m/min
        /// </summary>
        public int FilmVThreshold { get; set; }
        /// <summary>
        /// 线速度:编码器2 mm/pulse
        /// </summary>
        public double Encoder2_mmpp { get; set; }

        /// <summary>
        /// 线速度:1圈多少mm
        /// </summary>
        public double MmOfR { get; set; }



        /// <summary>
        /// 数据有效源
        /// </summary>
        public DATAVALIDSRC DataValidSrc { get; set; }



        /// <summary>
        /// 当数据有效状态改变，自动按F1，F3
        /// </summary>
        public bool AutoF1F3 { get; set; }

        /// <summary>
        /// 当数据有效状态  无效->有效 ，等待多久重新扫描 ，单位s
        /// </summary>
        public int ReStartDelay { get; set; }

        /// <summary>
        /// 有按样标定硬件
        /// </summary>
        public bool HasProfileSample { get; set; }

        /// <summary>
        /// 有小托辊
        /// </summary>
        public bool HasHold { get; set; }
        /// <summary>
        /// 打孔器偏移量
        /// </summary>
        public int PunchOffset { get; set; }
        /// <summary>
        /// 有打孔器
        /// </summary>
        public bool HasPunch { get; set; }

        /// <summary>
        /// 有纵向光纤传感器
        /// </summary>
        public bool HasVSign { get; set; }

        /// <summary>
        /// 纵向光纤传感器与X光偏移 mm
        /// </summary>
        public int VSignOffset { get; set; }

        /// <summary>
        /// 有横向光纤传感器
        /// </summary>
        public bool HasHSign { get; set; }
        /// <summary>
        /// 有机架修正功能
        /// </summary>
        public bool HasScanCorr { get; set; }
        #region 速度

        /// <summary>
        /// 扫描时速度 Velocity Of Scan 
        /// </summary>
        public UInt32 VScan { get; set; }
        /// <summary>
        /// 调试时速度，向前走，向后走 Velocity Of JOG
        /// </summary>
        public UInt32 VJOG { get; set; }
        /// <summary>
        /// 精确速度 Velocity Of Accuracy  如: 机架修正, 样品取样, 机架信息获取
        /// </summary>
        public UInt32 VAccuracy { set; get; }
        /// <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

        #region Command
        public RelayCommand ApplyCmd { get; }
        #endregion


        IInitParamService initParamService;

        public PgInitParamVm()
        {
            ApplyCmd = new RelayCommand(Apply);
        }


        public void Init(IInitParamService initParamService)
        {
            this.initParamService = initParamService;

            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.PosLength), this, nameof(PosLength));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.IsTimeGridAdvEnabled), this, nameof(IsTimeGridAdvEnabled));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.AutoOrgInterval), this, nameof(AutoOrgInterval));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.Encoder1_mmpp), this, nameof(Encoder1_mmpp));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.Speed1Scale), this, nameof(Speed1Scale));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.FilmVSrc), this, nameof(FilmVSrc));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.FilmVThreshold), this, nameof(FilmVThreshold));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.Encoder2_mmpp), this, nameof(Encoder2_mmpp));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.MmOfR), this, nameof(MmOfR));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.DataValidSrc), this, nameof(DataValidSrc));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.AutoF1F3), this, nameof(AutoF1F3));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.ReStartDelay), this, nameof(ReStartDelay));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.HasProfileSample), this, nameof(HasProfileSample));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.HasHold), this, nameof(HasHold));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.VScan), this, nameof(VScan));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.VJOG), this, nameof(VJOG));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.VAccuracy), this, nameof(VAccuracy));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.SVelocity), this, nameof(SVelocity));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.ATime), this, nameof(ATime));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.DTime), this, nameof(DTime));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.HVelocity1), this, nameof(HVelocity1));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.HVelocity2), this, nameof(HVelocity2));

            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.PunchOffset), this, nameof(PunchOffset));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.HasPunch), this, nameof(HasPunch));

            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.VSignOffset), this, nameof(VSignOffset));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.HasVSign), this, nameof(HasVSign));

            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.HasHSign), this, nameof(HasHSign));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.HasScanCorr), this, nameof(HasScanCorr));
        }

        private void Apply()
        {
            if (!WdPassword.Authorize("InitParam"))
                return;

            initParamService.PosLength = this.PosLength;
            initParamService.IsTimeGridAdvEnabled = this.IsTimeGridAdvEnabled;
            initParamService.AutoOrgInterval = this.AutoOrgInterval;
            initParamService.Encoder1_mmpp = this.Encoder1_mmpp;
            initParamService.Speed1Scale = this.Speed1Scale;
            initParamService.FilmVSrc = this.FilmVSrc;
            initParamService.FilmVThreshold = this.FilmVThreshold;
            initParamService.Encoder2_mmpp = this.Encoder2_mmpp;
            initParamService.MmOfR = this.MmOfR;
            initParamService.DataValidSrc = this.DataValidSrc;
            initParamService.AutoF1F3 = this.AutoF1F3;
            initParamService.ReStartDelay = this.ReStartDelay;
            initParamService.HasProfileSample = this.HasProfileSample;
            initParamService.HasHold = this.HasHold;
            initParamService.VScan = this.VScan;
            initParamService.VJOG = this.VJOG;
            initParamService.VAccuracy = this.VAccuracy;
            initParamService.SVelocity = this.SVelocity;
            initParamService.ATime = this.ATime;
            initParamService.DTime = this.DTime;
            initParamService.HVelocity1 = this.HVelocity1;
            initParamService.HVelocity2 = this.HVelocity2;

            initParamService.HasPunch = this.HasPunch;
            initParamService.PunchOffset = this.PunchOffset;

            initParamService.HasVSign = this.HasVSign;
            initParamService.VSignOffset = this.VSignOffset;

            initParamService.HasHSign = this.HasHSign;
            initParamService.HasScanCorr = this.HasScanCorr;
            initParamService.Apply();

            string tit = (string)Application.Current.TryFindResource("str.PgInitparam.ApplySuccessfully");
            FLY.ControlLibrary.Window_Tip.Show(tit, null, TimeSpan.FromSeconds(2));

        }
    }
}
