using Aitex.Core.RT.Device;
using Aitex.Core.RT.Device.Devices;
using Aitex.Core.RT.Routine;
using MECF.Framework.Common.Equipment;
using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.MFCs;
using SicModules.PMs.Routines.Base;
using System.Collections.Generic;

namespace SicModules.PMs.Routines
{
    public class PMToIsolationRoutine : PMBaseRoutine
    {
        private enum RoutineStep
        {
            RotationEnable,
            HeatEnable,
            VentPumpClose,
            ArSupply,

            SetGroupB,
            SetGroupC,
            SetGroupE,
            SetGroupF,
            SetGroupH,
            SetGroupD,
            SetGroupG,
            SetGroupK,

            SetM1to17,
            SetM18toM40,
            SetM17M18,
            SetPC,
            SetGroupI,      //V87-96
            SetM19to38,
            CloseFinal1,
            CloseFinal2,
            CloseEPV2,

            SetGasOut,
            SetTv,

            SetGasIn1,      //V65,V67,V68
            OpenFinal1,     //V91-96
            SetMfcFinal1,   //M32-38
            SetM19to26,

            SetGasIn2,
            SetGroupJOpen,

            OpenFinal2,
            SetMfcFinal2,

            SetEPV1,
            SetEPV2,
            SetTVCloseMode,
            SetTvPositionToZero,
            SetMFCMode,
            SetPCMode,

            SetV76,
            SetV75,
            SetV31,
            SetV32,
            SetV35V36,
            SetGroupV25,
            SetV65,
            CloseV68,
            SetV103,
            SetV105,

            CloseM32M35M37,
            CloseM36,
            CloseM38,
            CloseGasIn1,
            CloseFinanl1,

            TimeDelay1,
            TimeDelay2,
            TimeDelay3,
            TimeDelay4,
            TimeDelay5,
            TimeDelay6,
            TimeDelay7,
            TimeDelay8,
            TimeDelay9,
            TimeDelay10,
            TimeDelay11,
            TimeDelay12,
            TimeDelay14,
            TimeDelay18,
            TimeDelayForEPV1,
        }

        private PMModule _pm1Module;
        private IoThrottleValve2 _IoThrottle;

        private int _rotationCloseTimeout = 20;   //旋转停止超时
        private int _IoValueOpenCloseTimeout = 10; //开关超时时间
        private int _heatTimeOut = 5;             //Heat关闭等待Di反馈超时时间

        public PMToIsolationRoutine(ModuleName module, PMModule pm1) : base(module, pm1)
        {
            Module = module.ToString();
            _pm1Module = pm1;
            Name = "Isolation";

            _IoThrottle = DEVICE.GetDevice<IoThrottleValve2>($"{Module}.TV");
        }

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

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


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

                //关闭V76,打开V75
                SetIoValueByGroup((int)RoutineStep.SetV76, IoGroupName.V76, false, _IoValueOpenCloseTimeout);
                SetIoValueByGroup((int)RoutineStep.SetV75, IoGroupName.V75, true, _IoValueOpenCloseTimeout);

                //2、 旋转停止，加热停止
                SetHeatEnable((int)RoutineStep.HeatEnable, false, _heatTimeOut);
                SetRotationValve((int)RoutineStep.RotationEnable, 0, false, _rotationCloseTimeout);
                
                //3、 关闭 V72(GasBox.Vent.Pump） 。
                SetIoValueByGroup((int)RoutineStep.VentPumpClose, IoGroupName.VentPump, false, _IoValueOpenCloseTimeout);

                //4、 打开 V31(H2.Supply)、 V32(N2Supply)。
                SetIoValueByGroup((int)RoutineStep.SetV31, IoGroupName.V31, true, _IoValueOpenCloseTimeout);
                SetIoValueByGroup((int)RoutineStep.SetV32, IoGroupName.V32, true, _IoValueOpenCloseTimeout);

                //5、 打开 V35(H2N2.Line1)、V36(H2N2.Line2)，关闭 B/ C / E / F / H / KValves,打开D / G Valves。关闭 V68（ChamBody.Ar.Purge）
                SetIoValueByGroup((int)RoutineStep.SetV35V36, IoGroupName.V35V36, true, _IoValueOpenCloseTimeout);
                SetIoValueByGroup((int)RoutineStep.SetGroupB, IoGroupName.B, false, _IoValueOpenCloseTimeout);
                SetIoValueByGroup((int)RoutineStep.SetGroupC, IoGroupName.C, false, _IoValueOpenCloseTimeout);
                SetIoValueByGroup((int)RoutineStep.SetGroupE, IoGroupName.E, false, _IoValueOpenCloseTimeout);
                SetIoValueByGroup((int)RoutineStep.SetGroupF, IoGroupName.F, false, _IoValueOpenCloseTimeout);
                SetIoValueByGroup((int)RoutineStep.SetGroupH, IoGroupName.H, false, _IoValueOpenCloseTimeout);
                SetIoValueByGroup((int)RoutineStep.SetGroupK, IoGroupName.K, false, _IoValueOpenCloseTimeout);
                SetIoValueByGroup((int)RoutineStep.SetGroupD, IoGroupName.D, true, _IoValueOpenCloseTimeout);
                SetIoValueByGroup((int)RoutineStep.SetGroupG, IoGroupName.G, true, _IoValueOpenCloseTimeout);
                SetIoValueByGroup((int)RoutineStep.CloseV68, IoGroupName.V68, false, _IoValueOpenCloseTimeout);

                //6、 1 - M17 设定为 default 值，所有 PC 设定为 default 值(PC1 - PC9)，
                SetMfcToDefaultByGroup((int)RoutineStep.SetM1to17, MfcGroupName.M1toM17, 0);
                SetPcToDefault((int)RoutineStep.SetPC, _lstPcList);

                //7、 M18 - M40（除 M30\M34\M39）） MFC 5s ramp 到 0。
                SetMfcByGroup((int)RoutineStep.SetM18toM40, MfcGroupName.M18toM40No303439, 0, 5);

                //8、 依次关闭 Final valves；
                //先关闭 V93（ChamberPurge.Final）、 V94（RotationUpPurge.Final）、 V95（ConfinementRing.Final）、 V96（HeaterWF.Final）；
                //再关闭 V87（SHPurge.Final）、 V88（OuterGas.Final）、 V89（InnerGas.Final）、 V90（MiddleGas.Final）、 V91（OpticPurge.Final）、 V92（GasRingPurge.Final）、 V97（CarryGas.Final）；
                SetIoValueByGroup((int)RoutineStep.CloseFinal1, IoGroupName.Final1, false, _IoValueOpenCloseTimeout);
                SetIoValueByGroup((int)RoutineStep.CloseFinal2, IoGroupName.Final2, false, _IoValueOpenCloseTimeout);

                //9、 关闭蝶阀，再关闭 EPV2
                SetThrottleToCloseMode((int)RoutineStep.SetTVCloseMode, _IoThrottle, 8);
                SetThrottleDisable((int)RoutineStep.SetTvPositionToZero, _IoThrottle, 8);
                TimeDelay((int)RoutineStep.TimeDelay1, 1);
                SetIoValueByGroup((int)RoutineStep.CloseEPV2, IoGroupName.EPV2, false, _IoValueOpenCloseTimeout);
            }
            catch (RoutineBreakException)
            {
                return Result.RUN;
            }
            catch (RoutineFaildException)
            {
                return Result.FAIL;
            }

            Notify("Isolation Finished");

            return Result.DONE;
        }

        public override void Abort()
        {
            PmDevice.SetRotationServo(0, 0);

            base.Abort();
        }

    }
}

