71 lines
2.8 KiB
C#

using System.Text.Json.Serialization;
namespace EnvelopeGenerator.Web.Models;
public class AnnotationParams
{
[JsonIgnore]
public Annotation? DefaultAnnotation { get; init; }
private readonly IEnumerable<Annotation> _annots = new List<Annotation>();
public Annotation this[string name] => _annots.First(a => a.Name == name);
public bool TryGet(string name, out Annotation annotation)
{
#pragma warning disable CS8601 // Possible null reference assignment.
annotation = _annots.FirstOrDefault(a => a.Name == name);
#pragma warning restore CS8601 // Possible null reference assignment.
return annotation is not null;
}
[JsonIgnore]
public IEnumerable<string> Names => _annots.Select(annot => annot.Name);
public required IEnumerable<Annotation> Annotations
{
get => _annots;
init
{
_annots = value;
foreach (var name in Names)
{
#region set default values
if(DefaultAnnotation is not null)
{
// To set default value, annotation must have default (0) value but default must has non-default value
if (this[name]._marginLeft == default && DefaultAnnotation.MarginLeft != default)
this[name]._marginLeft = DefaultAnnotation.MarginLeft;
if (this[name]._marginTop == default && DefaultAnnotation.MarginTop != default)
this[name]._marginTop = DefaultAnnotation.MarginTop;
if (this[name]._width == default && DefaultAnnotation.Width != default)
this[name]._width = DefaultAnnotation.Width;
if (this[name]._height == default && DefaultAnnotation.Height != default)
this[name]._height = DefaultAnnotation.Height;
}
#endregion
#region set bound annotations
// horizontal
if (this[name].HorBoundAnnotName is string horBoundAnnotName)
if (TryGet(horBoundAnnotName, out var horBoundAnnot))
this[name].HorBoundAnnot = horBoundAnnot;
else
throw new InvalidOperationException($"{horBoundAnnotName} added as bound anotation. However, it is not defined.");
// vertical
if (this[name].VerBoundAnnotName is string verBoundAnnotName)
if (TryGet(verBoundAnnotName, out var verBoundAnnot))
this[name].VerBoundAnnot = verBoundAnnot;
else
throw new InvalidOperationException($"{verBoundAnnotName} added as bound anotation. However, it is not defined.");
#endregion
}
}
}
}