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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CyberX8_RT.Modules.Prewet;
using MECF.Framework.Common.CommonData;
using Aitex.Core.RT.Fsm;
using Aitex.Core.RT.SCCore;
using CyberX8_RT.Modules.Rinse;
using CyberX8_RT.Dispatch;
using CyberX8_RT.Schedulers.Transporter;
namespace CyberX8_RT.Schedulers.Prewet
{
    public class SchedulerPrewet : SchedulerModule
    {
        private enum PrewetOperation
        {
            None,
            RunRecipe,
            WaitForPick,
            Keepwet
        }
        #region 内部变量
        /// 
        /// Prewet实体
        /// 
        private PrewetEntity _prewetEntity;
        /// 
        /// keep wet次数
        /// 
        private int _keepwetCount = 0;
        /// 
        /// keep wet限制次数
        /// 
        private int _keepwetLimit = 0;
        /// 
        /// 当前操作
        /// 
        private PrewetOperation _currentOperation=PrewetOperation.None;
        #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 SchedulerPrewet(ModuleName moduleName) : base(moduleName.ToString())
        {
            _prewetEntity = Singleton.Instance.GetModule(moduleName.ToString());
        }
        /// 
        /// 执行
        /// 
        /// 
        /// 
        public override bool RunProcess(object recipe, object parameter, List syncModuleMessages)
        {
            if (!(recipe is PwtRecipe))
            {
                _state = RState.Failed;
                LOG.WriteLog(eEvent.ERR_PREWET, Module.ToString(), "recipe is invalid");
                return false;
            }
            _keepwetCount = 0;
            _keepwetLimit = SC.GetValue("Prewet.KeepWetMaxLimit");
            PwtRecipe pwtRecipe = (PwtRecipe)recipe;
            bool result = _prewetEntity.CheckToPostMessage(eEvent.ERR_PREWET, Module.ToString(), (int)PrewetMsg.RunRecipe, pwtRecipe,1);
            if (result)
            {
                _state = RState.Running;
                _currentOperation = PrewetOperation.RunRecipe;
            }
            return result;
        }
        /// 
        /// 监控执行
        /// 
        /// 
        public override bool MonitorProcess(SchedulerSequence schedulerSequence, bool hasMatchWafer)
        {
            bool exsitEnableCell = false;
            if (_prewetEntity.IsError)
            {
                _state=RState.Failed;
                return true;
            }
            if (_prewetEntity.State == (int)PrewetState.WaitForPick || (_prewetEntity.IsIdle && _prewetEntity.WaferHolderInfo == null))
            {
                _state = RState.End;
                _currentOperation = PrewetOperation.None;
                return true;
            }
            string waferHolderId = _prewetEntity.WaferHolderInfo?.Id;
            if (_prewetEntity.State==(int)PrewetState.RunRecipeComplete&&_currentOperation==PrewetOperation.RunRecipe)
            {
                CheckNextModuleCondition(schedulerSequence,waferHolderId,ref exsitEnableCell);
            }
            else if((_prewetEntity.State==(int)PrewetState.DelayKeepwetComplete)&&_currentOperation==PrewetOperation.Keepwet)
            {
                if(_keepwetCount>_keepwetLimit)
                {
                    LOG.WriteLog(eEvent.ERR_PREWET, Module.ToString(), $"keepwet count {_keepwetCount} over limit {_keepwetLimit}");
                    _state = RState.Failed;
                }
                else
                {
                    CheckNextModuleTimeToReady(schedulerSequence,waferHolderId,ref exsitEnableCell);
                }
            }
            else if (_prewetEntity.State == (int)PrewetState.DelayKeepweting && _currentOperation == PrewetOperation.Keepwet)
            {
                CheckNextModuleTimeToReady(schedulerSequence,waferHolderId, ref exsitEnableCell);
            }
            return true;
        }
        /// 
        /// 检验下一个
        /// 
        /// 
        /// 
        private void CheckNextModuleCondition(SchedulerSequence sequence, string waferHolderId, ref bool existEnableCell)
        {
            PwtRecipe pwtRecipe = (PwtRecipe)sequence.Recipe;
            if(!pwtRecipe.KeepWet)
            {
                NotifyPrewetReadyForPick("prewet no keepwet");
                return;
            }
            CheckNextModuleTimeToReady(sequence,waferHolderId,ref existEnableCell);
        }
        /// 
        /// 检验下一模块剩余时间
        /// 
        /// 
        private void CheckNextModuleTimeToReady(SchedulerSequence sequence,string waferHolderId, ref bool existEnableCell)
        {
            int idleKeepwetPauseBetweenScanSeconds = SC.GetValue("Prewet.IdleKeepwetPauseBetweenScanSeconds");
            int transporterTransferSeconds = SC.GetValue("Transporter.TransporterTransferSeconds");
            ModuleType nextModuleType = sequence.NextModuleType;
            if (nextModuleType == ModuleType.Metal)
            {
                if (!(sequence.NextRecipe is DepRecipe))
                {
                    NotifyPrewetReadyForPick("sequence nextRecipe is not deprecipe");
                }
                else
                {
                    bool isQdrCheckConflict = SchedulerManager.Instance.IsQdrCheckConflict;
                    DepRecipe depRecipe = sequence.NextRecipe as DepRecipe;
                    ModuleName metalModule = ModuleName.Unknown;
                    SchedulerSequence metalSequence = WaferHolderTaskManager.Instance.GetNextMetalSequence(sequence.SequenceIndex, waferHolderId);
                    if (!isQdrCheckConflict || metalSequence == null || metalSequence.NextRecipe == null || metalSequence.NextModuleType != ModuleType.Rinse)
                    {
                        metalModule = SchedulerSequenceManager.Instance.CalculateAvaibleMetalCellByChemistry(depRecipe.Chemistry, "", sequence.SequenceType,sequence.WaferSize, ref existEnableCell);
                    }
                    else
                    {
                        QdrRecipe qdrRecipe = (QdrRecipe)metalSequence.NextRecipe;
                        int depRecipeTimeLength = depRecipe.CalculateRecipeTotalTime();
                        int qdrRecipeTimeLength = qdrRecipe.CalculateRunRecipeTime();
                        WaferHolderTask waferHolderTask = WaferHolderTaskManager.Instance.GetWaferHolderTaskByWaferHolderId(waferHolderId);
                        if (waferHolderTask == null)
                        {
                            return;
                        }
                        SchedulerSequence transporterSequence = WaferHolderTaskManager.Instance.GetNextTransporterlSequence(sequence.SequenceIndex, waferHolderId);
                        if (transporterSequence == null)
                        {
                            return;
                        }
                        bool result = true;
                        if(!(transporterSequence.Parameters is TransporterAction))
                        {
                            return;
                        }
                        TransporterAction transporterAction=transporterSequence.Parameters as TransporterAction;
                        WaferHolderMoveItem waferHolderMoveItem = transporterAction.Parameter as WaferHolderMoveItem;
                        if (waferHolderMoveItem.DestModule == ModuleName.Unknown)
                        {
                            result = SchedulerModuleTimeManager.Instance.ConfirmAllMetalQdrAndDryer(waferHolderTask.SchedulerSequences, waferHolderId, waferHolderTask.GetCurrentSchedulerIndex(), true, DateTime.Now);
                        }
                        if (result)
                        {
                            CheckNextModuleAvaible(waferHolderMoveItem.DestModule.ToString(), transporterTransferSeconds);
                        }
                        else
                        {
                            NotifyPrewetKeepWet();
                        }
                        return;
                    }
                    if (metalModule != ModuleName.Unknown)
                    {
                        CheckNextModuleAvaible(metalModule.ToString(), transporterTransferSeconds);
                    }
                    else
                    {
                        if (existEnableCell)
                        {
                            NotifyPrewetKeepWet();
                        }
                        else
                        {
                            NotifyPrewetReadyForPick($"no enable metal");
                        }
                    }
                }
            }
            else
            {
                ModuleName moduleName = SchedulerSequenceManager.Instance.GetAvaibleEmptyModuleCell(nextModuleType, sequence.SequenceType);
                if (moduleName != ModuleName.Unknown)
                {
                    CheckNextModuleAvaible(moduleName.ToString(), idleKeepwetPauseBetweenScanSeconds);
                }
                else
                {
                    NotifyPrewetKeepWet();
                }
            }
        }
        /// 
        /// 检验下一个模块是否可用
        /// 
        /// 
        /// 
        private void CheckNextModuleAvaible(string moduleName, int resSecond)
        {
            IModuleEntity moduleEntity = Singleton.Instance.GetModule(moduleName);
            if (moduleEntity.IsIdle)
            {
                NotifyPrewetReadyForPick($"{moduleEntity.Module} is idle");
            }
            else
            {
                if (moduleEntity.TimeToReady <= resSecond)
                {
                    NotifyPrewetReadyForPick($"{moduleEntity.Module} remain seconds {moduleEntity.TimeToReady}");
                }
                else
                {
                    NotifyPrewetKeepWet();
                }
            }
        }
        /// 
        /// 通知Prewet准备Process for pick
        /// 
        private void NotifyPrewetReadyForPick(string reason)
        {
            bool result= _prewetEntity.CheckToPostMessage(eEvent.ERR_PREWET, Module.ToString(),
                (int)PrewetMsg.PrepareToPick);
            if (result)
            {
                _currentOperation = PrewetOperation.WaitForPick;
                LOG.WriteLog(eEvent.INFO_PREWET, Module.ToString(), reason);
            }
        }
        /// 
        /// 通知Prewet准备Keep Wet
        /// 
        private void NotifyPrewetKeepWet()
        {
            if (_prewetEntity.State == (int)PrewetState.DelayKeepweting)
            {
                return;
            }
            bool result= _prewetEntity.CheckToPostMessage(eEvent.ERR_PREWET, ModuleName.Prewet1.ToString(),
                (int)PrewetMsg.DelayKeepwet);
            if(result)
            {
                if (_currentOperation != PrewetOperation.Keepwet)
                {
                    _currentOperation = PrewetOperation.Keepwet;
                }
                _keepwetCount++;
            }
        }
        /// 
        /// 检验前置条件
        /// 
        /// 
        /// 
        /// 
        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 (_prewetEntity.State!=(int)PrewetState.WaitForPlace)
            {
                reason = "prewet state is not WaitForPlace";
                return false;
            }
            if(_prewetEntity.WaferHolderInfo==null)
            {
                reason = "prewet has no wafer shuttle";
                return false;
            }
            if (_prewetEntity.WaferHolderInfo.Id != materialId)
            {
                reason = $"{_prewetEntity.Module} wafer shuttle {_prewetEntity.WaferHolderInfo.Id} is not matched with {materialId}";
                return false;
            }
            _currentOperation = PrewetOperation.None;
            return true;
        }
    }
}