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

namespace FLY.Thick.Base.UI.OnInit
{
    public class OnInitAutoScan : IOnInit
    {
        IDynAreaService dynAreaService;
        FLY.OBJComponents.IService.IWarningService warningService;
        ITDGageService tDGageService;
        int autoscan_tip_id = -1;
        public OnInitAutoScan(IDynAreaService dynAreaService, FLY.OBJComponents.IService.IWarningService warningService, ITDGageService tDGageService, int lv=1) 
        {
            Level = lv;
            this.dynAreaService = dynAreaService;
            this.warningService = warningService;
            this.tDGageService = tDGageService;
        }

        public int Level { get; }

        public void OnInit()
        {
            dynAreaService.DynArea.PropertyChanged += (s, e) =>
            {
                DynArea dynArea = s as DynArea;

                if (e.PropertyName == "ControllerState")
                {
                    if (dynArea.ControllerState != CTRL_STATE.AUTOSCAN)
                    {
                        if (autoscan_tip_id != -1)
                        {
                            FLY.ControlLibrary.Window_WarningTip.Stop(autoscan_tip_id);
                            autoscan_tip_id = -1;
                        }
                    }

                    if (dynArea.ControllerState == CTRL_STATE.PAUSE)
                    {
                        FLY.ControlLibrary.Window_WarningTip.Show(
                            "通知",
                            "暂停扫描",
                            TimeSpan.MaxValue);
                    }
                }
                else if (e.PropertyName == "AutoScanCounter")
                {
                    if (dynArea.ControllerState == CTRL_STATE.AUTOSCAN)
                    {
                        autoscan_tip_id = FLY.ControlLibrary.Window_WarningTip.Show(
                            "通知",
                            dynArea.AutoScanCounter.ToString() + "秒后,开始扫描",
                            TimeSpan.FromSeconds(dynArea.AutoScanCounter),
                            null,
                            warningService.Reset,
                            "取消",
                            ()=> { tDGageService.StartP2(STARTP2_MODE.STOP); });
                    }
                }

            };
        }
    }
}