using Aitex.Core.RT.Device.Devices;
using Aitex.Core.RT.Event;
using Aitex.Core.RT.Routine;
using Aitex.Core.RT.SCCore;
using MECF.Framework.Common.Equipment;
using SicModules.PMs.Routines.Base;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace SicModules.PMs.Routines
{
    public class PMProcessAbortRoutine : PMBaseRoutine
    {
        private enum RoutineStep
        {
            HeatEnable,
            SetPSUMode,
            SetSCRMode,
            SetPSURatio,
            SetSCRRatio,
            SetRotation,
            SetM18toM40,
            SetM17M18,
            SetM1toM17,
            SetM5M21,
            SetPC,
            SetV32,
            SetV35V36,
            SetV68,
            SetV65,
            SetGroupB,
            SetGroupC,
            SetGroupE,
            SetGroupF,
            SetGroupH,
            SetGroupK,
            SetGroupD,
            SetGroupG,
            VentPumpClose,
            SetGroupV25,
            SetV34,
            SetV100,
            TimeDelay1,
            TimeDelay2, 
            SetGroupJ
        }

        private int _heatTimeOut = 5;

        private int _mfc1to16RampTime = 30;
        private int _mfc2to40RampTime = 30;

        private int _IoValueOpenCloseTimeout = 10; 
                 
        private int _routineTimeOut = 10;

        private Stopwatch _swTimer = new Stopwatch();
        public PMProcessAbortRoutine(ModuleName module, PMModule pm1) : base(module, pm1)
        {
            Module = module.ToString();
            Name = "ProcessAbort";
        }

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

            _mfc1to16RampTime = SC.GetValue<int>($"PM.{Module}.ProcessIdle.MFC1to16RampTime");
            _mfc2to40RampTime = SC.GetValue<int>($"PM.{Module}.ProcessIdle.MFC19to40RampTime");

            _routineTimeOut = SC.GetValue<int>($"PM.{Module}.ProcessIdle.RoutineTimeOut");

            _swTimer.Restart();
            Notify("Start");
            return Result.RUN;
        }


        public override Result Monitor()
        {
            try
            {
                //CheckRoutineTimeOut();

                //停止加热、停止旋转
                SetHeatEnable((int)RoutineStep.HeatEnable, false, _heatTimeOut);
                SetRotationValveAndNoWait((int)RoutineStep.SetRotation, 0);

                //2、 M18-M40（除 M30\M34\M39）） MFC 30s ramp 到 default 值（时间可配置）
                SetMfcToDefaultByGroup((int)RoutineStep.SetM18toM40, MfcGroupName.M18toM40No303439, _mfc2to40RampTime);

                //3、 M1-M17 MFC 30s ramp 到 default 值(时间可配置） ， 所有 PC 设定为 default 值（PC1-PC9）
                SetMfcToDefaultByGroup((int)RoutineStep.SetM1toM17, MfcGroupName.M1toM17, _mfc1to16RampTime);
                SetPcToDefault((int)RoutineStep.SetPC, _lstPcList);

                //等待MFC等流量
                TimeDelay((int)RoutineStep.TimeDelay1, Math.Max(_mfc2to40RampTime, _mfc1to16RampTime));

                //4、打开 V32(N2.Supply）
                SetIoValueByGroupNoWait((int)RoutineStep.SetV32, IoGroupName.V32, true);

                //5、 打开 V35(H2N2.Line1)、 V36(H2N2.Line2)，关闭 B/C/E/F/H/KValves,打开 D/G Valves；打开 J valve。
                SetIoValueByGroupNoWait((int)RoutineStep.SetV35V36, IoGroupName.V35V36, true);
                SetIoValueByGroupNoWait((int)RoutineStep.SetGroupB, IoGroupName.B, false);
                SetIoValueByGroupNoWait((int)RoutineStep.SetGroupC, IoGroupName.C, false);
                SetIoValueByGroupNoWait((int)RoutineStep.SetGroupE, IoGroupName.E, false);
                SetIoValueByGroupNoWait((int)RoutineStep.SetGroupF, IoGroupName.F, false);
                SetIoValueByGroupNoWait((int)RoutineStep.SetGroupH, IoGroupName.H, false);
                SetIoValueByGroupNoWait((int)RoutineStep.SetGroupK, IoGroupName.K, false);
                SetIoValueByGroupNoWait((int)RoutineStep.SetGroupD, IoGroupName.D, true);
                SetIoValueByGroupNoWait((int)RoutineStep.SetGroupG, IoGroupName.G, true);
                SetIoValueByGroupNoWait((int)RoutineStep.SetGroupJ, IoGroupName.V65, true);

            }

            catch (RoutineBreakException)
            {
                return Result.RUN;
            }
            catch (RoutineFaildException)
            {
                return Result.FAIL;
            }

            Notify($"Finished ! Elapsed time: {(int)(_swTimer.ElapsedMilliseconds / 1000)} s");
            _swTimer.Stop();

            return Result.DONE;
        }


        private void CheckRoutineTimeOut()
        {
            if (_routineTimeOut > 10)
            {
                if ((int)(_swTimer.ElapsedMilliseconds / 1000) > _routineTimeOut)
                {
                    EV.PostAlarmLog(Module, $"Routine TimeOut! over {_routineTimeOut} s");
                    throw (new RoutineFaildException());
                }
            }
        }

        public override void Abort()
        {
            PmDevice._ioThrottleValve.StopRamp();
            PmDevice.SetMfcStopRamp(PmDevice.GetMfcListByGroupName(MfcGroupName.All));
            PmDevice.SetHeaterStopRamp();
            PmDevice.SetRotationStopRamp();

            base.Abort();
        }
    }
}