laatzen/LaaProductionWeb/LaaProductionWeb.Blazor/Components/HeliumTester/Models/RecipesCollectionModel.cs
2025-03-17 16:03:24 +01:00

194 lines
7.0 KiB
C#

namespace LaaProductionWeb.Blazor.Components.HeliumTester.Models
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
public class RecipesCollectionModel
{
private readonly List<HeliumRecipeModel> heliumRecieps;
private readonly List<WaterRecipeModel> 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<HeliumRecipeModel> HeliumRecipeChanged;
public event Action<WaterRecipeModel> WaterRecipeChanged;
public HeliumRecipeModel VHeliumRecipe { get; }
public WaterRecipeModel VWaterRecipe { get; }
public IReadOnlyCollection<HeliumRecipeModel> HeliumRecipes
=> this.heliumRecieps;
public IReadOnlyCollection<WaterRecipeModel> WaterRecipes
=> this.waterRecieps;
public void LoadHeliumRecieps(IEnumerable<HeliumRecipeModel> heliumRecieps)
{
this.heliumRecieps.Clear();
this.heliumRecieps.AddRange(heliumRecieps);
foreach (var heliumRecipe in this.heliumRecieps)
{
heliumRecipe.RecipeChanged += this.OnHeliumRecipeChanged;
}
}
public IEnumerable<HeliumRecipeModel> 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<WaterRecipeModel> waterRecieps)
{
this.waterRecieps.Clear();
this.waterRecieps.AddRange(waterRecieps);
foreach (var waterRecipe in this.waterRecieps)
{
waterRecipe.RecipeChanged += this.OnWaterRecipeChanged;
}
}
public IEnumerable<WaterRecipeModel> 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);
}
}
}
}