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

184 lines
6.1 KiB
C#

namespace LaaProductionWeb.Blazor.Components.HeliumTester.Models
{
using LaaPackages.Data.HeliumTester;
using System;
using System.Collections.Generic;
using System.Linq;
public class MatchCollectionModel
{
protected readonly List<ReciepMatchItemModel<string>> regions = [];
protected readonly List<ReciepMatchItemModel<string>> meterSizes = [];
protected readonly List<ReciepMatchItemModel<int>> bodyLengths = [];
protected readonly List<ReciepMatchItemModel<bool>> pressureSensors = [];
public IReadOnlyCollection<ReciepMatchItemModel<string>> Regions
=> this.regions;
public IReadOnlyCollection<ReciepMatchItemModel<string>> MeterSizes
=> this.meterSizes;
public IReadOnlyCollection<ReciepMatchItemModel<int>> BodyLengths
=> this.bodyLengths;
public IReadOnlyCollection<ReciepMatchItemModel<bool>> PressureSensors
=> this.pressureSensors;
public event Action MatchesChanged;
public string RegionValue(string region)
{
var x = this.regions.FirstOrDefault(x => x.Key == region);
return x?.Value ?? region;
}
public string MeterSizeValue(string meterSize)
{
var x = this.meterSizes.FirstOrDefault(x => x.Key == meterSize);
return x?.Value ?? meterSize;
}
public string BodyLengthValue(string bodyLength)
{
var x = this.bodyLengths.FirstOrDefault(x => x.Key == bodyLength);
return x?.Value.ToString() ?? bodyLength;
}
public string PressureSensorValue(string presureSensor)
{
var x = this.pressureSensors.FirstOrDefault(x => x.Key == presureSensor);
return x?.Value.ToString() ?? presureSensor;
}
public void LoadVariants(IEnumerable<TestVariant> variants)
{
var regions = variants
.Select(x => new ReciepMatchItemModel<string>(x.RegionCode, x.Region))
.OrderBy(x => x.Key)
.ToHashSet();
this.UnbindSelectionChanged(this.regions);
this.regions.Clear();
this.regions.AddRange(regions);
var meterSizes = variants
.Select(x => new ReciepMatchItemModel<string>(x.MeterSizeCode, x.MeterSize))
.OrderBy(x => x.Key[1])
.ThenBy(x => x.Key[0])
.ToHashSet();
this.UnbindSelectionChanged(this.meterSizes);
this.meterSizes.Clear();
this.meterSizes.AddRange(meterSizes);
var bodyLengths = variants
.Select(x => new ReciepMatchItemModel<int>(x.BodyLengthCode, x.BodyLength))
.OrderBy(x => x.Key)
.ToHashSet();
this.UnbindSelectionChanged(this.bodyLengths);
this.bodyLengths.Clear();
this.bodyLengths.AddRange(bodyLengths);
var pressureSensors = variants
.Select(x => new ReciepMatchItemModel<bool>(x.PressureSensorCode, x.HasPressureSensor))
.OrderBy(x => x.Key)
.ToHashSet();
this.UnbindSelectionChanged(this.pressureSensors);
this.pressureSensors.Clear();
this.pressureSensors.AddRange(pressureSensors);
}
public void BindSelectionChanged()
{
this.BindSelectionChanged(this.regions);
this.BindSelectionChanged(this.meterSizes);
this.BindSelectionChanged(this.bodyLengths);
this.BindSelectionChanged(this.pressureSensors);
}
private void BindSelectionChanged(IEnumerable<MatchItemModel> items)
{
foreach (var item in items)
{
item.SelectionChanged += this.MatchSelectionChanged;
}
}
private void UnbindSelectionChanged(IEnumerable<MatchItemModel> items)
{
foreach (var item in items)
{
item.SelectionChanged -= this.MatchSelectionChanged;
}
}
public void MatchSelectionChanged()
=> this.MatchesChanged?.Invoke();
public void UpdateRegionsSelection(List<ReciepMatchItemModel<string>> regions)
{
foreach (var region in this.regions)
{
foreach (var match in regions)
{
if (region.Key == match.Key)
{
region.Selected = match.Selected;
}
}
}
}
public void UpdateMeterSizesSelection(List<ReciepMatchItemModel<string>> meterSizes)
{
foreach (var meterSize in this.meterSizes)
{
foreach (var match in meterSizes)
{
if (meterSize.Key == match.Key)
{
meterSize.Selected = match.Selected;
}
}
}
}
public void UpdateBodyLengthsSelection(List<ReciepMatchItemModel<int>> bodyLengths)
{
foreach (var bodyLength in this.bodyLengths)
{
foreach (var match in bodyLengths)
{
if (bodyLength.Key == match.Key)
{
bodyLength.Selected = match.Selected;
}
}
}
}
public void UpdatePressureSensorsSelection(List<ReciepMatchItemModel<bool>> pressureSensors)
{
foreach (var pressureSensor in this.pressureSensors)
{
foreach (var match in pressureSensors)
{
if (pressureSensor.Key == match.Key)
{
pressureSensor.Selected = match.Selected;
}
}
}
}
internal void BindMatchChanged(Action onMatchesChanged)
{
this.MatchesChanged -= onMatchesChanged;
this.MatchesChanged += onMatchesChanged;
}
}
}