using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using FlyADBase;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using System.Diagnostics;
using Misc;

namespace FLY.Thick.Base.Server
{
    public abstract class GM_Base : IGageMode, INotifyErrorArisen
    {
        /// <summary>
        /// 启动时出错，已经在运行了
        /// </summary>
        public const byte ERRNO_StartUp_IsRunning = 1;
        /// <summary>
        /// 启动时出错，AD卡连接断开
        /// </summary>
        public const byte ERRNO_StartUp_FlyADNoConnected = 2;
        /// <summary>
        /// 参数异常
        /// </summary>
        public const byte ERRNO_StartUp_Param = 3;
        /// <summary>
        /// 参数异常
        /// </summary>
        public const byte ERRNO_Running_Manual = 4;


        #region IGageMode 接口

        public CTRL_STATE GMState { get; protected set; }

        /// <summary>
        /// 运行中
        /// </summary>
        public bool IsRunning { get; protected set; }

        public event ErrorArisenEventHander ErrorArisenEvent;
        protected void NotifyError(byte errno) 
        {
            ErrorArisenEvent?.Invoke(this, new ErrorArisenEventArgs() { Errno = errno });
        }
        #endregion

        protected FlyAD7 mFlyAD;
        protected PollModule.PollHandler onpoll_func;
        public GM_Base()
        {

        }
        public virtual void Init(FlyAD7 flyad) {
            mFlyAD = flyad;
            onpoll_func = new PollModule.PollHandler(OnPoll);
        }
        protected virtual void OnPoll()
        {
            if (mFlyAD.IsFinish)
            {
                Stop();
            }
        }
        #region IGageMode 接口
        public virtual void Start()
        {
            if (IsRunning)
            {
                NotifyError(ERRNO_StartUp_IsRunning);
            }
            if (!mFlyAD.IsConnected)
            {
                NotifyError(ERRNO_StartUp_FlyADNoConnected);
            }
            IsRunning = true;
            PollModule.Current.Poll_Config(onpoll_func);
        }
        public virtual void Stop()
        {
            PollModule.Current.Poll_Config(
                PollModule.POLL_CONFIG.REMOVE,
                onpoll_func);

            IsRunning = false;
        }

        #endregion
        
        #region IGageMode 接口
        protected void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        #endregion

        /// <summary>
        /// 等待停止; 
        /// 正常停止返回true，没有停止及异常停止返回false
        /// </summary>
        /// <returns></returns>
        protected bool WaitFinish()
        {
            switch (mFlyAD.DriveStatus)
            {
                case DRIVE_MAN_STATUS.STOP://完成任务
                case DRIVE_MAN_STATUS.LIMIT:
                    {
                        return true;
                    }
                case DRIVE_MAN_STATUS.STOP_MANUAL://异常
                    {
                        NotifyError(ERRNO_Running_Manual);
                        Stop(); return false;
                    }
            }
            return false;
        }
    }














    

}
