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_6GasFlow: NH3_1GasFlow
    {
        public string V100 => "V100Value.SetValve";
        public  string M21 => "Mfc21.Ramp";

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

        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 v100 = ((ComboxParam)currentStep.FindParamByControlName(V100)).Value;
            double currentDilutedN2Flow = 0;
            //if (v100.ToString() == "Open")
            //{
            //    var m7 = ((DoubleParam)currentStep.FindParamByControlName(NameTmaBubbleFlow)).Value;
            //    var m8 = ((DoubleParam)currentStep.FindParamByControlName(NameTmaPushFlow)).Value;
            //    var m21 = ((DoubleParam)currentStep.FindParamByControlName(M21)).Value;

            //    currentN2LowFlow = m7 + m8 + m21;
            //}
            currentDilutedN2Flow = (currentN2LowFlow + currentDiluFlowForN2) * currentN2ActualFlow / currentN2LowFlow;

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

        protected override void CalVentFlow(RecipeStep currentStep, string chamberName)
        {
            var currentTotalVentFlow = ((DoubleParam)currentStep.FindParamByControlName(NameTotalVentFlow)).Value;
            var currentHClFlow = ((DoubleParam)currentStep.FindParamByControlName(NameHclFlow)).Value;
            var currentC2H4Flow = ((DoubleParam)currentStep.FindParamByControlName(NameCxHxFlow)).Value;

            var currentTcsFlowMode = (currentStep.FindParamByControlName(NameTcsFlowMode) as ComboxParam)?.Value;
            var currentHClFlowMode = (currentStep.FindParamByControlName(NameHclFlowMode) as ComboxParam)?.Value;
            var currentC2H4FlowMode = (currentStep.FindParamByControlName(NameCxHxFlowMode) as ComboxParam)?.Value;

            var tcsFlow = GetTcsFlow(currentStep, chamberName);


            var currentSiSourTotalFlowForPurge = (currentTcsFlowMode != null && currentTcsFlowMode.Equals(FlowModeParam.FlowModeEnum.Run) == false
                                                     ? tcsFlow
                                                     : 0d) +
                                                 (currentHClFlowMode != null && currentHClFlowMode.Equals(FlowModeParam.FlowModeEnum.Run) == false
                                                     ? currentHClFlow
                                                     : 0d);

            var currentCSourTotalFlowForPurge =
                currentC2H4FlowMode != null && currentC2H4FlowMode.Equals(FlowModeParam.FlowModeEnum.Run) == false
                    ? currentC2H4Flow
                    : 0d;

            var currentVentPushFlow = currentTotalVentFlow - currentSiSourTotalFlowForPurge - currentCSourTotalFlowForPurge;

            ((DoubleParam)currentStep.FindParamByControlName(NameVentPushFlow_M17)).Value = Math.Round(currentVentPushFlow, 1);
        }
    }
}
