using Aitex.Common.Util;
using Aitex.Core.Common;
using Aitex.Core.RT.Log;
using Aitex.Core.Util;
using PunkHPX8_Core;
using PunkHPX8_RT.Modules;
using PunkHPX8_RT.Modules.SRD;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.RecipeCenter;
using MECF.Framework.Common.SubstrateTrackings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PunkHPX8_RT.Modules.VpwMain;
using PunkHPX8_RT.Modules.VpwCell;
using PunkHPX8_RT.Modules.PlatingCell;
using static PunkHPX8_RT.Modules.PlatingCell.PlatingCellEntity;
namespace PunkHPX8_RT.Schedulers.Srd
{
    public class SchedulerPlatingCell : SchedulerModule
    {
        #region 内部变量
        private PlatingCellEntity _entity;
        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 SchedulerPlatingCell(ModuleName moduleName) : base(moduleName.ToString())
        {
            _entity = Singleton.Instance.GetModule(moduleName.ToString());
        }
        /// 
        /// 执行
        /// 
        /// 
        /// 
        public override bool RunProcess(object recipe, object parameter, List syncModuleMessages)
        {
            if (!(recipe is DepRecipe))
            {
                _state = RState.Failed;
                LOG.WriteLog(eEvent.ERR_PLATINGCELL, Module.ToString(), "recipe is invalid");
                return false;
            }
            _isStartRunRecipe = false;
            DepRecipe depRecipe = (DepRecipe)recipe;
            bool result = _entity.CheckToPostMessage(eEvent.INFO_PLATINGCELL, Module.ToString(), (int)PlatingCellMsg.RunRecipe, depRecipe, 1);
            if (result)
            {
                _state = RState.Running;
            }
            return result;
        }
        /// 
        /// 监控执行
        /// 
        /// 
        public override bool MonitorProcess(SchedulerSequence schedulerSequence,bool hasMatchWafer)
        {
            if (!_isStartRunRecipe)
            {
                _isStartRunRecipe = _entity.State == (int)PlatingCellState.RunReciping;
            }
            if (_isStartRunRecipe && _entity.IsIdle)
            {
                _state = RState.End;
                _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 (_entity.IsBusy)
            {
                reason = $"{_entity.Module} is busy";
                return false;
            }
            if(WaferManager.Instance.CheckNoWafer(Module,0))
            {
                reason = $"{_entity.Module} has no wafer";
                return false;
            }
            string matcher = ModuleMatcherManager.Instance.GetMatcherByModule(Module.ToString());
            if (string.IsNullOrEmpty(matcher))
            {
                reason = $"{Module} has no matcher";
                return false;
            }
            PlatingCellEntity matcherEntity = Singleton.Instance.GetModule(matcher);
            if (matcherEntity == null)
            {
                reason = $"{matcher} has no matcher";
                return false;
            }
            if (WaferManager.Instance.CheckNoWafer(matcher, 0))
            {
                reason = $"{matcher} has no wafer";
                return false;
            }
            return true;
        }
    }
}