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

namespace FLY.Thick.FilmCasting.UI
{
    /// <summary>
    /// PgScanWarning.xaml 的交互逻辑
    /// </summary>
    public partial class PgScanWarning : Page
    {
        PgScanWarningVm viewModel;
        public PgScanWarning()
        {
            InitializeComponent();
        }
        [InjectionMethod]
        public void Init(IScanWarningService scanWarningService)
        {
            viewModel = new PgScanWarningVm();
            viewModel.Init(scanWarningService);
            this.DataContext = viewModel;
        }
    }
    public class PgScanWarningVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        #region 参数
        /// <summary>
        /// 使能
        /// </summary>
        public bool Enable { get; set; }

        /// <summary>
        /// 连续N个点，大于规格线（公差）才算报警
        /// </summary>
        public int AlarmCnt { get; set; }

        /// <summary>
        /// 是公差的多少倍 报警 
        /// </summary>
        public double AlarmPercent { get; set; }
        /// <summary>
        /// 混合数
        /// </summary>
        public int Mix { get; set; }
        #endregion
        #region Command
        public RelayCommand ApplyCmd { get; }
        #endregion

        IScanWarningService scanWarningService;

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

        public void Init(IScanWarningService scanWarningService)
        {
            this.scanWarningService = scanWarningService;

            Misc.BindingOperations.SetBinding(scanWarningService, nameof(scanWarningService.Enable), this, nameof(Enable));
            Misc.BindingOperations.SetBinding(scanWarningService, nameof(scanWarningService.AlarmCnt), this, nameof(AlarmCnt));
            Misc.BindingOperations.SetBinding(scanWarningService, nameof(scanWarningService.AlarmPercent), this, nameof(AlarmPercent));
            Misc.BindingOperations.SetBinding(scanWarningService, nameof(scanWarningService.Mix), this, nameof(Mix));
        }
        private void Apply()
        {
            if (!WdPassword.Authorize("ScanWarning"))
                return;

            scanWarningService.Enable = this.Enable;
            scanWarningService.AlarmCnt = this.AlarmCnt;
            scanWarningService.AlarmPercent = this.AlarmPercent;
            scanWarningService.Mix = this.Mix;
            scanWarningService.Apply();

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