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

namespace FLY.Thick.Base.Server
{
    public class GM_Disconnected : GM_Base
    {
        GM_AutoScan mGMAutoScan;
        DynArea mDynArea;
        Stopwatch stopwatch = new Stopwatch();
        bool isReadyToAutoScan = false;

        public GM_Disconnected()
        {
            GMState = CTRL_STATE.DISCONNECTED;

        }
        public void Init(IFlyADClientAdv flyad, DynArea dynarea, GM_AutoScan gmAutoScan)
        {
            base.Init(flyad);
            mDynArea = dynarea;
            mGMAutoScan = gmAutoScan;
            mDynArea.PropertyChanged += new PropertyChangedEventHandler(mDynArea_PropertyChanged);
        }

        void mDynArea_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(mDynArea.FLYADIsConnect))
            {
                if (!mDynArea.FLYADIsConnect)
                {
                    //AD卡已经断开

                    if (mDynArea.ControllerState == CTRL_STATE.SCAN ||
                        mDynArea.ControllerState == CTRL_STATE.AUTOSCAN)
                    {
                        //之前在扫描中
                        isReadyToAutoScan = true;
                        stopwatch.Restart();
                    }
                    else
                    {
                        isReadyToAutoScan = false;
                        stopwatch.Stop();
                    }
                    Start();
                }
                else
                {
                    //AD盒已经重新连接上
                    if (IsRunning)//还在 CTRL_STATE.DISCONNECTED
                    {
                        if (isReadyToAutoScan)
                        {
                            //需要自动扫描
                            if (stopwatch.Elapsed < TimeSpan.FromSeconds(30)) {
                                isReadyToAutoScan = false;
                                stopwatch.Stop();
                                //30秒内又连上了，直接重启
                                mGMAutoScan.Start(0);
                            }
                            else if (stopwatch.Elapsed < TimeSpan.FromMinutes(3))
                            {
                                isReadyToAutoScan = false;
                                stopwatch.Stop();
                                //断开连接，到现在重新连上，只过了3min，重新执行扫描
                                mGMAutoScan.Start(5);
                            }
                            else
                            {
                                Stop();
                            }
                        }
                        else
                        {
                            Stop();
                        }
                    }
                }
            }
        }
        public override void Start()
        {
            IsRunning = true;
        }
        public override void Stop()
        {
            IsRunning = false;
            isReadyToAutoScan = false;
            stopwatch.Stop();
        }
    }
}
