using FLY.Thick.Base.Common;
using FlyADBase;
using System;

namespace FLY.Thick.Base.Server
{
    public class GM_AutoScan : GM_Base
    {
        public int Delay { get; set; }

        public int Counter { get; set; } = 5;

        Action ReScan;
        DateTime dtLast = DateTime.MinValue;
        public GM_AutoScan()
        {
            GMState = CTRL_STATE.AUTOSCAN;
            Delay = 5;
            Counter = Delay;
        }
        public void Init(IFlyADClientAdv flyad, Action reScan)
        {
            base.Init(flyad);
            ReScan = reScan;
        }
        protected override void OnPoll()
        {
            if ((DateTime.Now - dtLast).TotalSeconds >= 1)
            {
                dtLast = DateTime.Now;
                if (Counter > 0)
                    Counter--;

                if (Counter <= 0)
                {
                    Stop();
                    ReScan();
                }
            }
        }
        public void Start(int delay)
        {
            if (delay <= 0)
            {
                ReScan();
                return;
            }
            else
            {
                Delay = delay;
                Start();
            }
        }
        public override void Start()
        {
            IsRunning = true;
            Counter = Delay;
            dtLast = DateTime.Now;
            FObjBase.PollModule.Current.Poll_Config(onpoll_func);
            
        }
    }
}
