using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using Aitex.Core.RT.SCCore;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.Routine;
using CyberX8_Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CyberX8_RT.Devices.Loader
{
    public class LoaderReLeakFlowTestRoutine : RoutineBase, IRoutine
    {
        private enum LeakFlowTestStep
        {
            FirstDelay,
            FlowClampOff,
            VacuumOff,
            LeakFlowTest, 
            CheckLeakFlowState,
            End
        }
        #region 内部变量
        private LoaderCommonDevice _loaderCommon;
        private int _retryDelayms = 5;
        private LoaderLeakFirstFlowTestRoutine _routine;
        #endregion
        public LoaderReLeakFlowTestRoutine(string module) : base(module)
        {
        }
        public void Abort()
        {
            Runner.Stop("Manual Abort");
        }
        public RState Monitor()
        {
            Runner.Delay(LeakFlowTestStep.FirstDelay,_retryDelayms*1000)
                  .Run(LeakFlowTestStep.FlowClampOff, FlowClampOff, _delay_1ms)
                  .Run(LeakFlowTestStep.VacuumOff, VacuumOff, _delay_1ms)
                  .Run(LeakFlowTestStep.LeakFlowTest, FlowTest, NullFun, 100)
                  .WaitWithStopCondition(LeakFlowTestStep.CheckLeakFlowState,CheckFlowTestStatus,CheckFlowTestStop)
                  .End(LeakFlowTestStep.End, NullFun);
            return Runner.Status; 
        }
        /// 
        /// Flow Clamp off
        /// 
        /// 
        private bool FlowClampOff()
        {
            return _loaderCommon.LeakFlowClampOffAction();
        }
        /// 
        /// Vacuum Off
        /// 
        /// 
        private bool VacuumOff()
        {
            return _loaderCommon.LeakVacuumOffAction();
        }
        /// 
        /// 确认结束状态
        /// 
        /// 
        private bool CheckEndState()
        {
            return _loaderCommon.Status == RState.End;
        }
        /// 
        /// Flow Test
        /// 
        /// 
        private bool FlowTest()
        {
            return _routine.Start() == RState.Running;
        }
        /// 
        /// 检验FlowTest Routine结束状态
        /// 
        /// 
        private bool CheckFlowTestStatus()
        {
            return _routine.Monitor() == RState.End;
        }
        /// 
        /// 检验FlowTest Routine 失败状态
        /// 
        /// 
        private bool CheckFlowTestStop()
        {
            RState state = _routine.Monitor();
            return state == RState.Failed||state==RState.Timeout;
        }
        public RState Start(params object[] objs)
        {
            if (SC.ContainsItem($"{Module}.LeakTestRetryWait"))
            {
                _retryDelayms = SC.GetValue($"{Module}.LeakTestRetryWait");
            }
            _loaderCommon = DEVICE.GetDevice($"{Module}.Common");
            _routine = new LoaderLeakFirstFlowTestRoutine(Module);
            Runner.Start(Module, "Re Leak Flow Test");
            return RState.Running;
        }
    }
}