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

namespace Aitex.Triton160.RT.Routine.PM
{
    public class PumpDownRoutine : CommonRoutine
    {
        private enum PumpDown
        {
            CheckDoor,
            CheckDryPump,
            
            CloseAllVavle,

            OpenPumpingValve,

            CheckVAC,

            End,
        };

        private double _basePressure;
        private double _pumpTimeLimit;

        public PumpDownRoutine(string module, string name)
        {
            Module = module;
            Name = name;
            Display =  Resources.PumpDownRoutine_PumpDownRoutine_PumpingDown;

            bUINotify = true;
        }

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

        public void Terminate()
        {
        }


        public Result Start( )
        {
            Reset();
            UpdateSCValue();

            _basePressure = SC.GetSC<double>(SCName.System_PumpBasePressure).Value;
            _pumpTimeLimit = SC.GetSC<double>(SCName.System_PumpTimeLimit).Value;
      
            return Result.RUN;
        }


        public Result Monitor()
        {
            try
            {
                //检查Door 
                CheckDoor((int)PumpDown.CheckDoor,  Resources.PumpDownRoutine_Monitor_CheckDoorStatus, Notify, Stop);

                //检查 Dry Pump 
                CheckDryPump((int)PumpDown.CheckDryPump,  Resources.PumpDownRoutine_Monitor_CheckPumpStatus, Notify, Stop);

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

                //打开
                OpenPumpingValve((int)PumpDown.OpenPumpingValve,  Resources.PumpDownRoutine_Monitor_OpenPumpingValve, 10, Notify, Stop);

                //检查VAC
                CheckVAC((int)PumpDown.CheckVAC, string.Format( Resources.PumpDownRoutine_Monitor_WaitChamberPressureUntilBelow0, _basePressure), _basePressure, (int)_pumpTimeLimit, Notify, Stop);
     
                End((int)PumpDown.End,  Resources.PumpDownRoutine_Monitor_PumpingDownCompleted, Notify, Stop);
            }
            catch (RoutineBreakException)
            {
                return Result.RUN;
            }
            catch (RoutineFaildException)
            {
                return Result.FAIL;
            }

            return Result.DONE;
        }

        public new Result Abort()
        {
            base.Abort();

            string reason = string.Empty;
 

            if (!DeviceModel.ValveChamberPumping.TurnValve(false, out reason))
            {
                Notify(reason);
                return Result.FAIL;
            }

            return Result.DONE;
        }
    }
}