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

namespace FLY.Thick.FilmCasting.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; }


        #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

        /// <summary>
        /// 数据库保存的月数
        /// </summary>
        public int DBKeepMonth { get; set; }
        /// <summary>
        /// 数据库路径
        /// </summary>
        public string DbPath { get; set; }
        #endregion

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


        IInitParamService initParamService;

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


        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.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.DBKeepMonth), this, nameof(DBKeepMonth));
            Misc.BindingOperations.SetBinding(initParamService, nameof(initParamService.DbDirPath), this, nameof(DbPath));
        }

        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.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.DBKeepMonth = this.DBKeepMonth;
            initParamService.DbDirPath = this.DbPath;

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

        }

        private void SelectDbPath()
        {

            System.Windows.Forms.FolderBrowserDialog open = new System.Windows.Forms.FolderBrowserDialog();
            
            open.SelectedPath = DbPath;

            if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DbPath = open.SelectedPath;
            }
        }
    }
}
