using Aitex.Core.RT.Fsm;
using Aitex.Core.RT.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PunkHPX8_RT.Schedulers
{
    public class SchedulerPostMsg
    {
        private enum Step
        {
            PostMsg,
            CheckMsg
        }
        #region 内部变量
        private Step _step = Step.CheckMsg;
        private DateTime _postMsgTime = DateTime.Now;
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public SchedulerPostMsg()
        {
        }
        /// 
        /// 重置
        /// 
        public void Reset()
        {
            _step = Step.CheckMsg;
            _postMsgTime = DateTime.Now;
        }
        /// 
        /// 发送方法
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool PostMsg(Entity entity,int currentState,eEvent eEvent, string moduleName, int msg, int confirmState, params object[] args)
        {
            if (_step == Step.PostMsg)
            {
                bool result =entity.CheckToPostMessage(eEvent, moduleName, msg, args);
                if (result)
                {
                    _postMsgTime = DateTime.Now;
                    _step = Step.CheckMsg;
                }
            }
            else
            {
                if (currentState == confirmState)
                {
                    return true;
                }
                else if (DateTime.Now.Subtract(_postMsgTime).TotalMilliseconds >= 1000)
                {
                    _step = Step.PostMsg;
                }
            }
            return false;
        }
    }
}