using MECF.Framework.UI.Client.RecipeEditorLib.RecipeModel.Params;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MECF.Framework.UI.Client.RecipeEditorLib.RecipeModel
{
    public class NH3_18GasFlow : RecipeGasFlowM1ToM17_NoneSiH4
    {

        public override string NameN2FlowMode => "NH3_2Dilution.SetValve";

        public override string NameN2HighFlowMode => "N2NH3_2Doping.SetValve";

        public override string NameN2ActualFlow => "NH3_2ActualFlow";

        protected override void CalDopeFlow(RecipeStep currentStep)
        {
            var currentN2ActualFlow = ((DoubleParam)currentStep.FindParamByControlName(NameN2ActualFlow)).Value;
            var currentN2LowFlow = ((DoubleParam)currentStep.FindParamByControlName(NameN2LowFlow)).Value;
            var currentDiluFlowForN2 = ((DoubleParam)currentStep.FindParamByControlName(NameDilutionFlowForN2)).Value;
            var currentDilutedN2Flow = (currentN2LowFlow + currentDiluFlowForN2) * currentN2ActualFlow / currentN2LowFlow;

            ((DoubleParam)currentStep.FindParamByControlName(NameDilutedN2Flow)).Value = Math.Round(currentDilutedN2Flow, 1);


        }

        protected override void CalShSupplementFlow(RecipeStep currentStep)
        {
            var currentSiSourTotalFlow = ((DoubleParam)currentStep.FindParamByControlName(NameSiSourceTotalFlow)).Value;
            var currentCSourTotalFlow = ((DoubleParam)currentStep.FindParamByControlName(NameCSourceTotalFlow)).Value;


            var currentShTotalFlow = ((DoubleParam)currentStep.FindParamByControlName(NameShTotalFlow)).Value;
            var currentSurroundingFlow = ((DoubleParam)currentStep.FindParamByControlName(NameCarryGasFlow)).Value;


            var currentSiSourInnerFlow = ((DoubleParam)currentStep.FindParamByControlName(NameSiSourceInnerFlow)).Value;
            var currentSiSourMidFlow = ((DoubleParam)currentStep.FindParamByControlName(NameSiSourceMiddleFlow)).Value;

            var currentCSourInnerFlow = ((DoubleParam)currentStep.FindParamByControlName(NameCSourceInnerFlow)).Value;
            var currentCSourMidFlow = ((DoubleParam)currentStep.FindParamByControlName(NameCSourceMiddleFlow)).Value;



            var currentShTotalFlowSplitRatio = new double[3];
            if (currentStep.FindParamByControlName(NameShTotalFlowSplitRatio) is Sets3RatioParam shTotalRatio)
                currentShTotalFlowSplitRatio = shTotalRatio.Ratio;


            var currentShSuppTotalFlow = currentShTotalFlow - currentSurroundingFlow - currentSiSourTotalFlow -
                                         currentCSourTotalFlow;
            var sumShTotalFlowSplitRatio = currentShTotalFlowSplitRatio[0] + currentShTotalFlowSplitRatio[1] +
                                           currentShTotalFlowSplitRatio[2];
            var currentShInnerFlow = (currentShTotalFlow - currentSurroundingFlow) / sumShTotalFlowSplitRatio *
                                     currentShTotalFlowSplitRatio[0];
            var currentShMidFlow = (currentShTotalFlow - currentSurroundingFlow) / sumShTotalFlowSplitRatio *
                                   currentShTotalFlowSplitRatio[1];
            var currentShOutterFlow = (currentShTotalFlow - currentSurroundingFlow) / sumShTotalFlowSplitRatio *
                                      currentShTotalFlowSplitRatio[2];
            var currentInnerSuppFlow = currentShInnerFlow - currentSiSourInnerFlow - currentCSourInnerFlow;
            var currentMidSuppFlow = currentShMidFlow - currentSiSourMidFlow - currentCSourMidFlow;
            var currentOutterSuppFlow = currentShSuppTotalFlow - currentInnerSuppFlow - currentMidSuppFlow;

            ((DoubleParam)currentStep.FindParamByControlName(NameShPushTotalFlow)).Value = Math.Round(currentShSuppTotalFlow, 1);
            ((DoubleParam)currentStep.FindParamByControlName(NameShInnerFlow)).Value = Math.Round(currentShInnerFlow, 1);
            ((DoubleParam)currentStep.FindParamByControlName(NameShMiddleFlow)).Value = Math.Round(currentShMidFlow, 1);
            ((DoubleParam)currentStep.FindParamByControlName(NameShOuterFlow)).Value = Math.Round(currentShOutterFlow, 1);
            ((DoubleParam)currentStep.FindParamByControlName(NameInnerPushFlow)).Value = Math.Round(currentInnerSuppFlow, 1);
            ((DoubleParam)currentStep.FindParamByControlName(NameMiddlePushFlow)).Value = Math.Round(currentMidSuppFlow, 1);
            ((DoubleParam)currentStep.FindParamByControlName(NameOuterPushFlow)).Value = Math.Round(currentOutterSuppFlow, 1);
        }
    }
}
