namespace LaaProductionWeb.Blazor.Components.HeliumTester.Models { using System; using System.Collections.Generic; using System.Linq; using System.Reflection; public class RecipesCollectionModel { private readonly List heliumRecieps; private readonly List waterRecieps; private MatchCollectionModel matchCollectionModel; public RecipesCollectionModel() { this.VHeliumRecipe = new HeliumRecipeModel(); this.VWaterRecipe = new WaterRecipeModel(); this.heliumRecieps = []; this.waterRecieps = []; this.VHeliumRecipe.PropertyChanged += this.VHeliumRecipePropertyChanged; this.VWaterRecipe.PropertyChanged += this.VWaterRecipePropertyChanged; } public event Action HeliumRecipeChanged; public event Action WaterRecipeChanged; public HeliumRecipeModel VHeliumRecipe { get; } public WaterRecipeModel VWaterRecipe { get; } public IReadOnlyCollection HeliumRecipes => this.heliumRecieps; public IReadOnlyCollection WaterRecipes => this.waterRecieps; public void LoadHeliumRecieps(IEnumerable heliumRecieps) { this.heliumRecieps.Clear(); this.heliumRecieps.AddRange(heliumRecieps); foreach (var heliumRecipe in this.heliumRecieps) { heliumRecipe.RecipeChanged += this.OnHeliumRecipeChanged; } } public IEnumerable LoadHeliumRecieps(MatchCollectionModel matchCollection) { var heliumRecieps = this.heliumRecieps.AsEnumerable(); if (heliumRecieps.Any()) { var selectedRegionKeys = matchCollection .Regions .Where(x => x.Selected) .Select(x => x.Key); if (selectedRegionKeys.Any()) { heliumRecieps = heliumRecieps.Where(x => selectedRegionKeys.Any(r => r == x.MatchRegion)); } var selectedMeterSizesKeys = matchCollection .MeterSizes .Where(x => x.Selected) .Select(x => x.Key); if (selectedMeterSizesKeys.Any()) { heliumRecieps = heliumRecieps.Where(x => selectedMeterSizesKeys.Any(r => r == x.MatchMeterSize)); } var selectedBodyLengthKeys = matchCollection .BodyLengths .Where(x => x.Selected) .Select(x => x.Key); if (selectedBodyLengthKeys.Any()) { heliumRecieps = heliumRecieps.Where(x => selectedBodyLengthKeys.Any(r => r == x.MatchBodyLength)); } var selectedPressureSensorKeys = matchCollection .PressureSensors .Where(x => x.Selected) .Select(x => x.Key); if (selectedPressureSensorKeys.Any()) { heliumRecieps = heliumRecieps.Where(x => selectedPressureSensorKeys.Any(r => r == x.MatchPressureSensor)); } } return heliumRecieps.ToList(); } public void LoadWaterRecieps(IEnumerable waterRecieps) { this.waterRecieps.Clear(); this.waterRecieps.AddRange(waterRecieps); foreach (var waterRecipe in this.waterRecieps) { waterRecipe.RecipeChanged += this.OnWaterRecipeChanged; } } public IEnumerable LoadWaterRecieps(MatchCollectionModel matchCollection) { var waterRecieps = this.waterRecieps.AsEnumerable(); if (waterRecieps.Any()) { var selectedRegionKeys = matchCollection .Regions .Where(x => x.Selected) .Select(x => x.Key); if (selectedRegionKeys.Any()) { waterRecieps = waterRecieps.Where(x => selectedRegionKeys.Any(r => r == x.MatchRegion)); } var selectedMeterSizesKeys = matchCollection .MeterSizes .Where(x => x.Selected) .Select(x => x.Key); if (selectedMeterSizesKeys.Any()) { waterRecieps = waterRecieps.Where(x => selectedMeterSizesKeys.Any(r => r == x.MatchMeterSize)); } var selectedBodyLengthKeys = matchCollection .BodyLengths .Where(x => x.Selected) .Select(x => x.Key); if (selectedBodyLengthKeys.Any()) { waterRecieps = waterRecieps.Where(x => selectedBodyLengthKeys.Any(r => r == x.MatchBodyLength)); } var selectedPressureSensorKeys = matchCollection .PressureSensors .Where(x => x.Selected) .Select(x => x.Key); if (selectedPressureSensorKeys.Any()) { waterRecieps = waterRecieps.Where(x => selectedPressureSensorKeys.Any(r => r == x.MatchPressureSensor)); } } return waterRecieps.ToList(); } internal void LoadMatchCollectionModel(MatchCollectionModel matchCollectionModel) => this.matchCollectionModel = matchCollectionModel; private void OnWaterRecipeChanged(WaterRecipeModel recipe) => this.WaterRecipeChanged?.Invoke(recipe); private void OnHeliumRecipeChanged(HeliumRecipeModel recipe) => this.HeliumRecipeChanged?.Invoke(recipe); private void VHeliumRecipePropertyChanged(string property) { var propertyInfo = typeof(HeliumRecipeModel).GetProperty(property, BindingFlags.Public | BindingFlags.Instance); var value = propertyInfo.GetValue(this.VHeliumRecipe); foreach (var heliumRecipe in this.LoadHeliumRecieps(this.matchCollectionModel)) { propertyInfo.SetValue(heliumRecipe, value); this.HeliumRecipeChanged?.Invoke(heliumRecipe); } } private void VWaterRecipePropertyChanged(string property) { var propertyInfo = typeof(WaterRecipeModel).GetProperty(property, BindingFlags.Public | BindingFlags.Instance); var value = propertyInfo.GetValue(this.VWaterRecipe); foreach (var waterRecipe in this.LoadWaterRecieps(this.matchCollectionModel)) { propertyInfo.SetValue(waterRecipe, value); this.WaterRecipeChanged?.Invoke(waterRecipe); } } } }