using Aitex.Common.Util;
using Aitex.Core.RT.ConfigCenter;
using Aitex.Core.RT.Log;
using Aitex.Core.Util;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.RecipeCenter;
using CyberX8_Core;
using CyberX8_RT.Modules;
using CyberX8_RT.Modules.Dryer;
using CyberX8_RT.Modules.Loader;
using CyberX8_RT.Modules.PUF;
using CyberX8_RT.Schedulers.EfemRobot;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CyberX8_RT.Modules.Metal;
namespace CyberX8_RT.Schedulers.Dryer
{
    public class SchedulerDryer : SchedulerModule
    {
        #region 内部变量
        private DryerEntity _dryerEntity;
        private bool _isStartRunRecipe = false;
        #endregion
        #region 属性
        /// 
        /// 是否空闲
        /// 
        public override bool IsIdle
        {
            get { return _state == RState.End; }
        }
        /// 
        /// 是否错误
        /// 
        public override bool IsError
        {
            get { return _state == RState.Failed || _state == RState.Timeout; }
        }
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public SchedulerDryer(ModuleName moduleName) : base(moduleName.ToString())
        {
            _dryerEntity = Singleton.Instance.GetModule(moduleName.ToString());
        }
        /// 
        /// 执行
        /// 
        /// 
        /// 
        public override bool RunProcess(object recipe, object parameter, List syncModuleMessages)
        {
            if(!(recipe is HvdRecipe))
            {
                _state = RState.Failed;
                LOG.WriteLog(eEvent.ERR_DRYER, Module.ToString(), "recipe is invalid");
                return false;
            }
            _isStartRunRecipe = false;
            HvdRecipe hvdRecipe = (HvdRecipe)recipe;
            bool result= _dryerEntity.CheckToPostMessage(eEvent.ERR_DRYER,Module.ToString(),(int)DryerMsg.RunRecipe, recipe,1);
            if(result)
            {
                _state = RState.Running;
            }
            return result;
        }
        /// 
        /// 监控执行
        /// 
        /// 
        public override bool MonitorProcess(SchedulerSequence schedulerSequence, bool hasMatchWafer)
        {
            if (!_isStartRunRecipe)
            {
                _isStartRunRecipe = _dryerEntity.State == (int)DryerState.RunReciping;
            }
            if(_isStartRunRecipe&&_dryerEntity.IsIdle)
            {
                _state = RState.End;
                _isStartRunRecipe= false;
            }
            if (_dryerEntity.IsError)
            {
                _state = RState.Failed;
                _isStartRunRecipe=false;
            }
            return true;
        }
        /// 
        /// 检验前置条件
        /// 
        /// 
        /// 
        /// 
        public override bool CheckPrecondition(List schedulerSequences, int sequenceIndex, object parameter, string materialId,ref string reason)
        {
            if (_state == RState.Running)
            {
                reason = "scheduler module is already running";
                return false;
            }
            if (_dryerEntity.WaferHolderInfo==null)
            {
                reason = $"{_dryerEntity.Module} has no wafer shuttle";
                return false;
            }
            if(_dryerEntity.IsBusy)
            {
                reason = $"{_dryerEntity.Module} is busy";
                return false;
            }
            if (_dryerEntity.WaferHolderInfo.Id != materialId)
            {
                reason = $"{_dryerEntity.Module} wafer shuttle {_dryerEntity.WaferHolderInfo.Id} is not matched with {materialId}";
                return false;
            }
            return true;
        }
    }
}