using Aitex.Core.RT.Device;
using Aitex.Core.RT.Event;
using Aitex.Core.RT.Routine;
using Aitex.Core.RT.SCCore;
using MECF.Framework.Common.Equipment;

namespace SicModules.TMs.Routines
{
    public class TMServoRoutine : TMBaseRoutine
    {
        enum RoutineStep
        {
            ChamberBalance,
            OpenTMToLLVent,
            CheckPressureCondition,

            SetSlitValve,
            LLRoutine,

            Delay,
            Delay1,
            CloseTMToLLVent,
            PumpDownToBase,
        }

        private TMPressureBalanceRoutine _balanceRoutine;

        private SicTM _tm;

        private bool _needPressureBalance;

        public TMServoRoutine()
        {
            Module = ModuleName.TM.ToString();
            Name = "ServoPressure";
            _tm = DEVICE.GetDevice<SicTM>($"{ ModuleName.System.ToString()}.{ ModuleName.TM.ToString()}");
        }

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

            _balanceRoutine = new TMPressureBalanceRoutine();
            _balanceRoutine.Init(ModuleName.PM1);

            _needPressureBalance = SC.GetValue<bool>("TM.NeedPressureBalance");

            ModuleName[] modules = new ModuleName[] { ModuleName.LoadLock, ModuleName.PM1, ModuleName.PM2};
            foreach (var moduleName in modules)
            {
                if (!_tm.CheckSlitValveClose(moduleName))
                {
                    EV.PostAlarmLog(Module, $"Can not pump, {moduleName} slit valve not closed");
                    return Result.FAIL;
                }
            }

            string reason;
            if (!_tm.SetFastPumpValve(false, out reason) || !_tm.SetFastVentValve(false, out reason))
            {
                EV.PostAlarmLog(Module, $"Can not turn off valves, {reason}");
                return Result.FAIL;
            }

            if (SC.GetValue<bool>("System.IsATMMode"))
            {
                return Result.DONE;
            }

            Notify("Start");

            return Result.RUN;
        }


        public override Result Monitor()
        {
            try
            {
                  ExecuteRoutine((int)RoutineStep.ChamberBalance, _balanceRoutine);
            }
            catch (RoutineBreakException)
            {
                return Result.RUN;
            }
            catch (RoutineFaildException)
            {
                return Result.FAIL;
            }


            Notify("Finished");

            return Result.DONE;
        }

        public override void Abort()
        {
              _balanceRoutine.Abort();
        }
    }
}
