using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.RecipeCenter;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.Utilities;
using PunkHPX8_Core;
using PunkHPX8_RT.Devices.VpwCell;
using PunkHPX8_RT.Devices.VpwMain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace PunkHPX8_RT.Modules.VpwCell
{
    public class VpwManualRecipeRoutine : RoutineBase, IRoutine
    {
        private enum RecipeStep
        {
            Prepare,
            WaitPrepare,
            VacuumPrewet,
            WaitVacuumPrewet,
            VentPrewet,
            WaitVentPrewet,
            ExtendClean,
            WaitExtendClean,
            SpinOff,
            WaitSpinOff,
            StopRotation,
            WaitStop,
            End
        }
        #region 内部变量
        /// 
        /// 手动Prepare routine
        /// 
        private VpwManualPrepareRoutine _manualPrepareRoutine;
        /// 
        /// Vacuum prewet routine
        /// 
        private VpwVacuumPrewetRoutine _vacuumPrewetRoutine;
        /// 
        /// Vent Prewet
        /// 
        private VpwVentPrewetRoutine _ventPrewetRoutine;
        /// 
        /// Extend clean
        /// 
        private VpwExtendCleanRoutine _extendCleanRoutine;
        /// 
        /// Spin Off Routine
        /// 
        private VpwSpinOffRoutine _spinOffRoutine;
        /// 
        /// recipe
        /// 
        private VpwRecipe _recipe;
        /// 
        /// Cell device
        /// 
        private VpwCellDevice _vpwCellDevice;
        /// 
        /// Main device
        /// 
        private VpwMainDevice _mainDevice;
        #endregion
        /// 
        /// 构造函数
        /// 
        /// 
        public VpwManualRecipeRoutine(string module) : base(module)
        {
            _manualPrepareRoutine = new VpwManualPrepareRoutine(module);
            _vacuumPrewetRoutine=new VpwVacuumPrewetRoutine(module);
            _ventPrewetRoutine = new VpwVentPrewetRoutine(module);
            _extendCleanRoutine= new VpwExtendCleanRoutine(module);
            _spinOffRoutine=new VpwSpinOffRoutine(module);
        }
        /// 
        /// 中止
        /// 
        public void Abort()
        {
            Runner.Stop("Manual stop");
            _mainDevice.VPWBoostPumpTarget = VpwMain.VPWBoostPumpTarget.Pressure;
        }
        /// 
        /// 监控
        /// 
        /// 
        public RState Monitor()
        {
            Runner.Run(RecipeStep.Prepare, Prepare, _delay_1ms)
                .WaitWithStopCondition(RecipeStep.WaitPrepare, () => CommonFunction.CheckRoutineEndState(_manualPrepareRoutine),
                    () => { return CheckSubRoutineError(_manualPrepareRoutine, _manualPrepareRoutine, 0); })
                 .Run(RecipeStep.VacuumPrewet, VacuumPrewet)
                 .WaitWithStopCondition(RecipeStep.WaitVacuumPrewet, () => CommonFunction.CheckRoutineEndState(_vacuumPrewetRoutine),
                    () => { return CheckSubRoutineError(_vacuumPrewetRoutine, _vacuumPrewetRoutine, 1); })
                 .Run(RecipeStep.VentPrewet, VentPrewet)
                 .WaitWithStopCondition(RecipeStep.WaitVentPrewet, () => CommonFunction.CheckRoutineEndState(_ventPrewetRoutine),
                    () => { return CheckSubRoutineError(_ventPrewetRoutine, _ventPrewetRoutine, 2); })
                 .Run(RecipeStep.ExtendClean, ExtendClean)
                 .WaitWithStopCondition(RecipeStep.WaitExtendClean, () => CommonFunction.CheckRoutineEndState(_extendCleanRoutine),
                    () => { return CheckSubRoutineError(_extendCleanRoutine, _extendCleanRoutine, 3); })
                 .Run(RecipeStep.SpinOff, SpinOff)
                 .WaitWithStopCondition(RecipeStep.WaitSpinOff, () => CommonFunction.CheckRoutineEndState(_spinOffRoutine),
                    () => { return CheckSubRoutineError(_spinOffRoutine, _spinOffRoutine, 4); })
                .Run(RecipeStep.StopRotation, StopRotation, _delay_1ms)
                .WaitWithStopCondition(RecipeStep.WaitStop, CheckStopEndStatus, CheckStopErrorStatus)
                .End(RecipeStep.End, End, _delay_1ms);
            return Runner.Status;
        }
        /// 
        /// Prepare
        /// 
        /// 
        private bool Prepare()
        {
            return _manualPrepareRoutine.Start(_recipe) == RState.Running;
        }
        /// 
        /// Vacuum Prewet
        /// 
        /// 
        private bool VacuumPrewet()
        {
            return _vacuumPrewetRoutine.Start(_recipe) == RState.Running;
        }
        /// 
        /// Vent Prewet
        /// 
        /// 
        private bool VentPrewet()
        {
            return _ventPrewetRoutine.Start(_recipe) == RState.Running;
        }
        /// 
        /// Extend Clean
        /// 
        /// 
        private bool ExtendClean()
        {
            return _extendCleanRoutine.Start(_recipe) == RState.Running;
        }
        /// 
        /// Vacuum Prewet
        /// 
        /// 
        private bool SpinOff()
        {
            return _spinOffRoutine.Start(_recipe) == RState.Running;
        }
        /// 
        /// 检验子routine异常
        /// 
        /// 
        /// 
        /// 
        /// 
        private bool CheckSubRoutineError(IRoutine routine,RoutineBase routineBase,int index)
        {
            bool result = CommonFunction.CheckRoutineStopState(routine);
            if (result)
            {
                NotifyError(eEvent.ERR_VPW, routineBase.ErrorMsg, index);
            }
            return result;
        }
        /// 
        /// 停止rotation
        /// 
        /// 
        private bool StopRotation()
        {
            bool result = _vpwCellDevice.StopProfilePosition();
            if (!result)
            {
                NotifyError(eEvent.ERR_VPW, "Stop rotation failed", 0);
            }
            return result;
        }
        /// 
        /// 检验停止完成状态
        /// 
        /// 
        private bool CheckStopEndStatus()
        {
            return _vpwCellDevice.CheckRotationEndStatus();
        }
        /// 
        /// 检验停止异常
        /// 
        /// 
        private bool CheckStopErrorStatus()
        {
            bool result = _vpwCellDevice.CheckRotationStopStatus();
            if (result)
            {
                NotifyError(eEvent.ERR_VPW, "Stop rotation failed", 0);
            }
            return result;
        }
        /// 
        /// 结束
        /// 
        /// 
        private bool End()
        {
            _mainDevice.VPWBoostPumpTarget = VpwMain.VPWBoostPumpTarget.Pressure;
            _mainDevice.CommonData.BoosterPumpSpeedAuto = false;
            return true;
        }
        /// 
        /// 启动
        /// 
        /// 
        /// 
        public RState Start(params object[] objs)
        {
            _recipe = objs[0] as VpwRecipe;
            _vpwCellDevice = DEVICE.GetDevice(Module);
            _mainDevice = DEVICE.GetDevice(ModuleName.VPWMain1.ToString());
            return Runner.Start(Module, "start run manual recipe");
        }
    }
}