using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aitex.Core.RT.Event;
using Aitex.Core.RT.SCCore;
using Aitex.RT.Properties;
using Aitex.Triton160.Common;
using Aitex.Core.RT.IOCore;
using Aitex.Core.RT.Routine;
using Aitex.Triton160.RT.Device;

namespace Aitex.Triton160.RT.Routine.PM
{
    public class CyclePurgeRoutine : CommonRoutine
    {
         private enum RoutineStep
         {
             CheckRfOff,

             CheckPumping,

             CloseAllValves,

             Loop,

             Pump,

             PumpDelay,

             Vent,

             VentDelay,

             EndLoop,

             PumpDefault,

             End,
         };

        private int _purgeCycleCount;
        
        private int _purgeVentPressure;
        private int _purgePumpPressure;

        private int _purgeVentTimeLimit;
        private int _purgePumpTimeLimit;

        private int _purgeVentStableTime;
        private int _purgePumpStableTime;

        public CyclePurgeRoutine(string module, string name)
        {
            Module = module;
            Name = name;
            Display =  Resources.CyclePurgeRoutine_CyclePurgeRoutine_CyclePurge;

            bUINotify = true;
        }

        public bool Initialize()
        {
            InitCommon();
            return true;
        }

        public void Terminate()
        {
        }


        public Result Start(params object[] objs)
        {
            Reset();
            UpdateSCValue();

         _purgeCycleCount = (int)SC.GetValue<double>(SCName.ProcessConfig_PurgeCycleCount);
        
         _purgeVentPressure = (int)SC.GetValue<double>(SCName.ProcessConfig_PurgeVentPressure);
         _purgePumpPressure = (int)SC.GetValue<double>(SCName.ProcessConfig_PurgePumpPressure);

         _purgeVentTimeLimit = (int)SC.GetValue<double>(SCName.ProcessConfig_PurgeVentTimeLimit);
         _purgePumpTimeLimit = (int)SC.GetValue<double>(SCName.ProcessConfig_PurgePumpTimeLimit);

         _purgeVentStableTime = (int)SC.GetValue<double>(SCName.ProcessConfig_PurgeVentStableTime);
         _purgePumpStableTime = (int)SC.GetValue<double>(SCName.ProcessConfig_PurgePumpStableTime);
         
            return Result.RUN;
        }


        public Result Monitor()
        {
           

            try
            {
                CheckRfOff((int)RoutineStep.CheckRfOff, Resources.StopPumpRoutine_Monitor_CheckIfTheRfIsOn);

                CheckPumpingOn((int)RoutineStep.CheckPumping, Resources.CyclePurgeRoutine_Monitor_CheckIfThePumpValveOpen);

                //CheckGasFlowStopped((int)RoutineStep.CheckGasFlow, "Check if the gas stopped flowing");

                //关闭所有阀门
                CloseAllValve((int)RoutineStep.CloseAllValves, Resources.PumpDownRoutine_Monitor_CloseAllTheValves, 10, Notify, Stop);

                Loop((int)RoutineStep.Loop, Resources.CyclePurgeRoutine_Monitor_StartCyclePurge, _purgeCycleCount, Notify, Stop);

                CyclePump((int)RoutineStep.Pump, Resources.CyclePurgeRoutine_Monitor_Pumping, _purgePumpPressure, _purgePumpTimeLimit, true, Notify, Stop);

                Delay((int)RoutineStep.PumpDelay, string.Format("pump delay {0} seconds", _purgePumpStableTime),  _purgePumpStableTime, Notify, Stop);

                CycleVent((int)RoutineStep.Vent, Resources.CyclePurgeRoutine_Monitor_Venting, _purgeVentPressure, _purgeVentTimeLimit, true, Notify, Stop);

                Delay((int)RoutineStep.VentDelay, string.Format("vent delay {0} seconds", _purgeVentStableTime), _purgeVentStableTime, Notify, Stop);

                EndLoop((int)RoutineStep.EndLoop, Notify, Stop);

                CyclePump((int)RoutineStep.PumpDefault, Resources.CyclePurgeRoutine_Monitor_KeepInPumpingStatus, _purgePumpPressure, _purgePumpTimeLimit, false, Notify, Stop);

                End((int)RoutineStep.End, Resources.CyclePurgeRoutine_Monitor_CyclePurgeFinished, Notify, Stop);
            }
            catch (RoutineBreakException)
            {
                return Result.RUN;
            }
            catch (RoutineFaildException)
            {
                return Result.FAIL;
            }

            return Result.DONE;
        }


    }
}