using MECF.Framework.Common.DataCenter;
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 RecipeGasFlowM1ToM17_NoneSiH4 : RecipeGasFlowCalculatorBase
    {
        public virtual string NameVentPushFlow_M17 => "Mfc17.Ramp";

        protected override double GetTcsFlow(RecipeStep step, string chamberName)
        {
            var currentTcsBubbleHighFlow = ((DoubleParam)step.FindParamByControlName(NameTcsBubbleHighFlow)).Value;
            var currentTcsPushFlow = ((DoubleParam)step.FindParamByControlName(NameTcsPushFlow)).Value;
            var currentTcsFlowMode = ((ComboxParam)step.FindParamByControlName(NameTcsFlowMode)).Value;

            var tcsFlow = 0d;

            var tcsEff =
                (double)QueryDataClient.Instance.Service.GetConfig($"PM.{chamberName}.Efficiency.TCS-Eff");
            if (currentTcsFlowMode.Equals(FlowModeParam.FlowModeEnum.Run)
                || currentTcsFlowMode.Equals(FlowModeParam.FlowModeEnum.Vent))
            {
                tcsFlow = currentTcsBubbleHighFlow * tcsEff + currentTcsPushFlow;
            }
            else if (currentTcsFlowMode.Equals(FlowModeParam.FlowModeEnum.Purge))
            {
                tcsFlow = currentTcsBubbleHighFlow + currentTcsPushFlow;
            }

            return tcsFlow;
        }

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