using System;
using System.Collections.Generic;
using System.Diagnostics;
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;

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

        private int _heatTimeOut = 5;

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

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

        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);

                //设置模式为Power
                SetPSUHeatMode((int)RoutineStep.SetPSUMode, 0);
                SetSCRHeatMode((int)RoutineStep.SetSCRMode, 0);

                //设定功率为0
                SetPSUHeatRatio((int)RoutineStep.SetPSURatio, 0, 0, 0);
                SetSCRHeatRatio((int)RoutineStep.SetSCRRatio, 0, 0, 0);

                //停止旋转
                SetRotationValveAndNoWait((int)RoutineStep.SetRotation, 0);

                //M2、M9、M15、M19-M40 MFC 30s ramp 到 default 值
                SetMfcToDefaultByGroup((int)RoutineStep.SetM2toM40, MfcGroupName.M2toM42, _mfc2to40RampTime);
                SetMfcToDefaultByGroup((int)RoutineStep.SetM17M18, MfcGroupName.M17M18, _mfc2to40RampTime);

                //M1 - M16 MFC 30s ramp 到 default 值(M2、M9、M15除外)
                SetMfcToDefaultByGroup((int)RoutineStep.SetM1to16, MfcGroupName.M1to16, _mfc1to16RampTime);
                //M5 M2130s ramp 到 default 值
                SetMfcToDefaultByGroup((int)RoutineStep.SetM5M21, MfcGroupName.M5M21, _mfc1to16RampTime);

                //所有PC设定为默认值
                SetPcToDefault((int)RoutineStep.SetPC, _lstPcList);

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

                //打开V32
                SetIoValueByGroupNoWait((int)RoutineStep.SetV32, IoGroupName.V32, true);

                //打开V35,打开V36
                SetIoValueByGroupNoWait((int)RoutineStep.SetV35V36, IoGroupName.V35V36, true);

                //关闭B/C/E/F/H/K 阀门
                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.SetV100, IoGroupName.V100, false);

                //打开D/G 阀门
                SetIoValueByGroupNoWait((int)RoutineStep.SetGroupD, IoGroupName.D, true);
                SetIoValueByGroupNoWait((int)RoutineStep.SetGroupG, IoGroupName.G, true);

                //打开V65
                SetIoValueByGroupNoWait((int)RoutineStep.SetV65, IoGroupName.V65, true);

                //打开V68
                SetIoValueByGroupNoWait((int)RoutineStep.SetV68, IoGroupName.GasIn1, true);

                //关闭V72,打开V25
                SetIoValueByGroupNoWait((int)RoutineStep.VentPumpClose, IoGroupName.VentPump, false);

                TimeDelay((int)RoutineStep.TimeDelay2, 2);
                SetIoValueByGroupNoWait((int)RoutineStep.SetGroupV25, IoGroupName.V25, 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();
        }
    }
}