using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.RecipeCenter;
using Aitex.Core.RT.Routine;
using MECF.Framework.Common.RecipeCenter;
using MECF.Framework.Common.Routine;
using CyberX8_Core;
using CyberX8_RT.Devices.LinMot;
using CyberX8_RT.Devices.Prewet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CyberX8_RT.Modules.Prewet
{
    public class PrewetManualProcessRecipeRoutine : RoutineBase, IRoutine
    {
        private enum ProcessRecipeStep
        {
            Process,
            WaitProcess,
            End
        }
        private enum ProcessAbortStep
        {
            PumpValvaClose,
            AbortLinmot,
            TerminateStateMachine,
            End
        }
        #region 内部变量
        /// 
        /// LintMot Axis
        /// 
        private LinMotAxis _linmotAxis;
        /// 
        /// recipe对象
        /// 
        private PwtRecipe _recipe;
        /// 
        /// Process状态机
        /// 
        private PrewetProcessStateMachine _processStateMachine;
        public PrewetProcessStateMachine prewetProcessStateMachine { get { return _processStateMachine; } }
        /// 
        /// prewetDevice对象
        /// 
        private PrewetDevice _prewetDevice;
        
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public PrewetManualProcessRecipeRoutine(string module,LinMotAxis linMotAxis) : base(module)
        {
            _linmotAxis = linMotAxis;
            _prewetDevice=DEVICE.GetDevice(Module);
        }
        /// 
        /// 中止
        /// 
        public void Abort()
        {
            Runner.Stop("Manual abort");
            _processStateMachine.Terminate();
            _linmotAxis.AbortCurrentRoutine();
            _prewetDevice.PumpValveClose();
        }
        /// 
        /// 监控
        /// 
        /// 
        public RState Monitor()
        {
            Runner.Run(ProcessRecipeStep.Process, StartProcess, _delay_1ms)
                .WaitWithStopCondition(ProcessRecipeStep.WaitProcess, WaitProcessComplete, CheckProcessError)
                .End(ProcessRecipeStep.End, NullFun, _delay_1ms);
            return Runner.Status;
        }
        /// 
        /// 启动Process
        /// 
        /// 
        private bool StartProcess()
        {
            return _processStateMachine.CheckToPostMessage(eEvent.ERR_PREWET, Module.ToString(), 
                (int)PrewetProcessStateMachine.PrewetProcessMsg.ProcessStart, _recipe);
        }
        /// 
        /// 等待Process结束
        /// 
        /// 
        private bool WaitProcessComplete()
        {
            bool retult= _processStateMachine.State == PrewetProcessStateMachine.PrewetProcessState.Idle.ToString();
            return  retult;
        }
        /// 
        /// 检验Process过程是否出现错误
        /// 
        /// 
        private bool CheckProcessError()
        {
            return _processStateMachine.State==PrewetProcessStateMachine.PrewetProcessState.Error.ToString();
        }
        /// 
        /// 启动
        /// 
        /// 
        /// 
        public RState Start(params object[] objs)
        {
            string recipe = objs[0].ToString();
            PwtRecipe pwtRecipe = RecipeFileManager.Instance.LoadGenericityRecipe(recipe);
            if (pwtRecipe == null)
            {
                LOG.WriteLog(eEvent.ERR_PREWET, Module, $"invalid {recipe} recipe");
                return RState.Failed;
            }
            _recipe = pwtRecipe;
            _processStateMachine = new PrewetProcessStateMachine(Module, _linmotAxis);
            _processStateMachine.Initialize();
            return Runner.Start(Module, "Manual Process Recipe");
        }
    }
}