using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aitex.Core.RT.Fsm;
using Aitex.Core.Util;
using Aitex.Core.RT.SCCore;
using Aitex.Sorter.Common;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.Schedulers;
using MECF.Framework.Common.SubstrateTrackings;
using PunkHPX8_RT.Modules;
using MECF.Framework.Common.CommonData;
using PunkHPX8_Core;
using Aitex.Core.RT.Log;
using Aitex.Core.Common;
using MECF.Framework.Common.RecipeCenter;
namespace PunkHPX8_RT.Schedulers.Aligner
{
    public class SchedulerAligner : SchedulerModule
    {
        #region 内部变量
        private EfemEntity _efemEntity;
        #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 SchedulerAligner(ModuleName module) : base(module.ToString())
        {
            _efemEntity = Singleton.Instance.EFEM;
        }
        /// 
        /// 执行
        /// 
        /// 
        /// 
        public override bool RunProcess(object recipe, object parameter, List syncModuleMessages)
        {
            if (_efemEntity.IsBusy)
            {
                return false;
            }
            if (recipe == null)
            {
                return false;
            }
            if(!(recipe is SequenceRecipe))
            {
                return false;
            }
            SequenceRecipe sequenceRecipe=recipe as SequenceRecipe;
            int angle = int.Parse(sequenceRecipe.AlignmentAngle.ToString());
            int platType = sequenceRecipe.PlatType;
            return _efemEntity.CheckToPostMessage(eEvent.ERR_EFEM_COMMON_FAILED,
                Module.ToString(), (int)EfemEntity.MSG.Align, ModuleName.Aligner1.ToString(), 0, angle,platType);
        }
        /// 
        /// 监控执行
        /// 
        /// 
        public override bool MonitorProcess(SchedulerSequence schedulerSequence,bool hasMatchWafer)
        {
            if (_efemEntity.IsBusy)
            {
                _state = RState.Running;
            }
            if (_efemEntity.IsIdle)
            {
                _state = RState.End;
            }
            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 (WaferManager.Instance.CheckHasWafer(Module, 0))
            {
                WaferInfo waferInfo = WaferManager.Instance.GetWafer(Module, 0);
                bool result= waferInfo.WaferID==materialId;
                if (!result)
                {
                    reason = $"aligner wafer id {waferInfo.WaferID} is not matched with {materialId}";
                }
                return result;
            }
            else
            {
                reason = "aligner has no wafer";
                return false;
            }
        }
    }
}