using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using MultiLayout.UiModule;
using Misc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using FLY.Thick.Base.UI;

namespace FLY.Thick.Base.UI.OnInit
{
    public class OnInitGageCommand : IOnInit
    {
        Window window;
        ITDGageService tDGageService;
        IDynAreaService dynAreaService;
        ParamDictionary paramDictionary;

        public int Level { get; }
        public OnInitGageCommand(ITDGageService tDGageService, IDynAreaService dynAreaService, ParamDictionary paramDictionary, int lv=1) 
        {
            Level = lv;

            this.window = Application.Current.MainWindow;
            this.tDGageService = tDGageService;
            this.dynAreaService = dynAreaService;
            this.paramDictionary = paramDictionary;
        }
        public void OnInit()
        {
            CommandBinding commandBinding = new CommandBinding()
            {
                Command = GageCommands.Scan
            };
            commandBinding.Executed += CommandBindingScan_Executed;

            window.CommandBindings.Add(commandBinding);

            commandBinding = new CommandBinding()
            {
                Command = GageCommands.Stop
            };
            commandBinding.Executed += CommandBindingStop_Executed;

            window.CommandBindings.Add(commandBinding);


            commandBinding = new CommandBinding()
            {
                Command = GageCommands.Org
            };
            commandBinding.Executed += CommandBindingOrg_Executed;

            window.CommandBindings.Add(commandBinding);

            commandBinding = new CommandBinding()
            {
                Command = GageCommands.Forward
            };
            commandBinding.Executed += CommandBindingForw_Executed;

            window.CommandBindings.Add(commandBinding);

            commandBinding = new CommandBinding()
            {
                Command = GageCommands.Backward
            };
            commandBinding.Executed += CommandBindingBackw_Executed;

            window.CommandBindings.Add(commandBinding);

        }

        private void CommandBindingScan_Executed(object sender, ExecutedRoutedEventArgs e)
        {

            if (dynAreaService.DynArea.SecuteLock)
            {
                
                FLY.ControlLibrary.Window_WarningTip.Show(
                    "警告",
                    "使用码到期,请联系枫莱尔公司",
                    TimeSpan.FromSeconds(5),
                    paramDictionary.GetValue<string>(ParamDistItemKeys.WarningTipPath));
            }
            else
            {
                tDGageService.StartP1();
            }

        }
        private void CommandBindingStop_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            tDGageService.StartP2(STARTP2_MODE.STOP);
        }
        private void CommandBindingForw_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            tDGageService.StartP2(STARTP2_MODE.FORW);
        }
        private void CommandBindingBackw_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            tDGageService.StartP2(STARTP2_MODE.BACKW);
        }
        private void CommandBindingOrg_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            tDGageService.StartP2(STARTP2_MODE.ORG);
        }
    }
}