using Aitex.Core.RT.Routine;
using Aitex.Core.RT.SCCore;
using Venus_RT.Devices;
using MECF.Framework.Common.Routine;
using Venus_Core;
using Aitex.Core.RT.OperationCenter;
using System.Linq;

namespace Venus_RT.Modules.PMs
{
    enum GasStep
    {
        kStartGas,
        kEnd,
    }
    class GasFlowRoutine : PMRoutineBase, IRoutine
    {
        public bool _gasStatus = false;
        private double[] _mfcSetPoint;
        private ValveType[] vlvs;
        public GasFlowRoutine(JetPMBase chamber) : base(chamber)
        {
            Name = "Gas Flow";
            switch (chamber.ChamberType)
            {
                case JetChamber.VenusSE:
                case JetChamber.VenusDE:
                    _mfcSetPoint = new double[12];
                    vlvs = new ValveType[] {
                        ValveType.PV11, ValveType.PV21, ValveType.PV31,
                        ValveType.PV41, ValveType.PV51, ValveType.PV61,
                        ValveType.PV71, ValveType.PV81, ValveType.PV91,
                        ValveType.PVA1, ValveType.PVB1, ValveType.PVC1
                    };
                    break;
                default:
                    _mfcSetPoint = new double[8];
                    vlvs = new ValveType[] { ValveType.PV11, ValveType.PV21, ValveType.PV31, ValveType.PV41 };
                    break;
            }

        }

        public RState Start(params object[] objs)
        {
            //if (!_chamber.IsFastPumpOpened)
            //{
            //    StopFlow();
            //    Stop("Pump 阀没有打开");
            //    return RState.Failed;
            //}

            if (!CheckTurboPump())
            {
                return RState.Failed;
            }

            if (_chamber.GetPVPosition() <= 0)
            {
                Stop("钟摆阀没有打开");
                return RState.Failed;
            }

            Reset();


            // open process final valve and flow
            Notify("Open valve and flow mfc");
            if (RtInstance.ConfigType == ConfigType.Kepler2200)
            {

            }
            else
            {
                _chamber.OpenValve(ValveType.GasFinal, true);
            }


            int i = 0;
            foreach (object o in objs)
            {
                _mfcSetPoint[i++] = (double)o;
            }
            if (RtInstance.ConfigType == ConfigType.Kepler2200 && _mfcSetPoint.Length==8)
            {
                double sum = 0;
                for (int j = 0; j < 7; j++)
                {
                    sum += _mfcSetPoint[j];
                }
                if (sum > 0)
                {
                    _chamber.OpenValve(ValveType.GasFinal, true);
                }
            }
            else
            {
                _chamber.OpenValve(ValveType.GasFinal, true);
            }
            _gasStatus = true;
            Reset();
            return Runner.Start(Module, Name);
        }
        public RState Monitor()
        {
            Runner.Run(GasStep.kStartGas, FlowMfc, CheckRange, 10 * 24 * 60 * 60 * 1000)
                .End(GasStep.kEnd, NullFun, _delay_50ms);

            return Runner.Status;
        }

        public void Abort()
        {
            StopFlow();
        }

        public bool FlowMfc()
        {
            for (int index = 0; index < _mfcSetPoint.Length; index++)
            {
                if (_mfcSetPoint[index] > 0)
                    OpenPVNVlv(index, true);

                _chamber.FlowGas(index, _mfcSetPoint[index]);

            }

            return true;
        }

        private void OpenPVNVlv(int mfcIndex, bool on)
        {

            if (mfcIndex < vlvs.Length)
            {
                _chamber.OpenValve(vlvs[mfcIndex], on);
            }
        }

        private bool CheckRange()
        {
            if (_chamber.HasGasOutOfRange)
            {
                Stop("流气率越界");
                _gasStatus = false;
                return true;
            }

            return false;
        }

        public void StopFlow()
        {
            Notify("Close valve and stop to flow MFC");

            _chamber.OpenValve(ValveType.GasFinal, false);
            _chamber.StopAllGases();
            for (int i = 0; i < vlvs.Length; i++)
            {
                OpenPVNVlv(i, false);
            }
        }

    }
}