Thor/Thor.Shared/Model/TemplateRegister.cs
2025-03-21 07:19:48 +01:00

86 lines
2.7 KiB
C#

namespace Thor.Shared.Model;
public class TemplateRegister
: RegisterBase<TemplateEntry, Item>
{
public List<Item> Forge()
{
Dictionary<string, List<TemplateEntry>> entriesByTarget = [];
foreach (var i in GetEntriesOrdered())
{
var target = i.Target;
if (entriesByTarget.TryGetValue(target, out var list))
list.Add(i);
else
entriesByTarget.Add(target, [i]);
}
List<Item> items = [];
foreach(var kv in entriesByTarget)
{
var item = Forge(kv.Key, kv.Value);
if(item is not null)
items.Add(item);
}
return items.OrderBy(x => x.OrderId).ToList();
}
private Item? Forge(string id, IEnumerable<TemplateEntry> entries)
{
bool? IsTitle = null;
string Identifier = id;
string? ParentId = null;
string? OrderId = null;
string? Label = null;
string? Description = null;
string? References = null;
int restauration = 1;
foreach(var entry in entries)
{
if(entry.Type == TemplateEntry.EntryType.Delete)
{
restauration--;
continue;
}
if (entry.Type == TemplateEntry.EntryType.Restore)
{
restauration++;
continue;
}
//ignore comments
if (entry.Type != TemplateEntry.EntryType.Set)
continue;
if (entry.Field == TemplateEntry.FieldType.Type && IsTitle is null)
IsTitle = entry.Content == TemplateEntry.TYPE_TITLE;
else if(entry.Field == TemplateEntry.FieldType.Reference && References is null)
References = entry.Content;
else if(entry.Field == TemplateEntry.FieldType.Label && Label is null)
Label = entry.Content;
else if(entry.Field == TemplateEntry.FieldType.Parent && ParentId is null)
ParentId = entry.Content;
else if(entry.Field == TemplateEntry.FieldType.Description && Description is null)
Description = entry.Content;
else if(entry.Field == TemplateEntry.FieldType.Order && OrderId is null)
OrderId = entry.Content;
}
if (IsTitle is null)
return null;
Item ret = IsTitle == true ? new ItemTitle() : new ItemVerification();
ret.Identifier = Identifier;
ret.ParentId = ParentId ?? String.Empty;
ret.OrderId = OrderId ?? String.Empty;
ret.Label = Label ?? String.Empty;
ret.Description = Description ?? String.Empty;
ret.References = References ?? String.Empty;
return ret;
}
}