PlotDirector/PlotLine/Services/ProjectBackupTimelineState.cs

60 lines
3.8 KiB
C#

using System.Text.Json;
using System.Text.Json.Nodes;
using PlotLine.Models;
namespace PlotLine.Services;
internal static class ProjectBackupTimelineState
{
private static readonly Dictionary<string,string> References = new(StringComparer.OrdinalIgnoreCase)
{
["ProjectID"]="Project", ["BookID"]="Books", ["SelectedSceneID"]="Scenes",
["ChapterFromID"]="Chapters", ["ChapterToID"]="Chapters", ["SceneFromID"]="Scenes", ["SceneToID"]="Scenes",
["PlotLineID"]="PlotLines", ["PlotThreadID"]="PlotThreads", ["StoryAssetID"]="StoryAssets",
["CharacterID"]="Characters", ["LocationID"]="Locations", ["POVCharacterID"]="Characters"
};
private static string? FocusCollection(string? type) => type?.Trim().ToLowerInvariant() switch
{
"character"=>"Characters", "asset"=>"StoryAssets", "plotline"=>"PlotLines", "plotthread"=>"PlotThreads",
"location"=>"Locations", "scene"=>"Scenes", _=>null
};
private static JsonObject Read(string json) => JsonNode.Parse(json,new(){PropertyNameCaseInsensitive=true}) as JsonObject
?? throw new JsonException("Timeline filter must be an object.");
public static string? Validate(ProjectBackupExport backup)
{
var ids=new Dictionary<string,HashSet<int>> {
["Project"]=[backup.Project!.ProjectID], ["Books"]=backup.Data.Books.Select(x=>x.BookID).ToHashSet(),
["Chapters"]=backup.Data.Chapters.Select(x=>x.ChapterID).ToHashSet(), ["Scenes"]=backup.Data.Scenes.Select(x=>x.SceneID).ToHashSet(),
["Characters"]=backup.Data.Characters.Select(x=>x.CharacterID).ToHashSet(), ["Locations"]=backup.Data.Locations.Select(x=>x.LocationID).ToHashSet(),
["StoryAssets"]=backup.Data.StoryAssets.Select(x=>x.StoryAssetID).ToHashSet(), ["PlotLines"]=backup.Data.PlotLines.Select(x=>x.PlotLineID).ToHashSet(),
["PlotThreads"]=backup.Data.PlotThreads.Select(x=>x.PlotThreadID).ToHashSet()
};
bool Invalid(string? collection,int? id)=>collection is not null && id.HasValue && !ids[collection].Contains(id.Value);
try
{
foreach(var preset in backup.Data.TimelineViewPresets)
{
if(Invalid(FocusCollection(preset.FocusType),preset.FocusID))return "Timeline preset focus references a missing or foreign record.";
var filter=Read(preset.FilterJson);
foreach(var reference in References)
if(filter[reference.Key] is JsonNode value && Invalid(reference.Value,value.GetValue<int>()))return "Timeline preset filter references a missing or foreign record.";
if(Invalid(FocusCollection(filter["FocusType"]?.GetValue<string>()),filter["FocusID"]?.GetValue<int>()))return "Timeline preset filter focus references a missing or foreign record.";
}
}
catch(Exception ex) when(ex is JsonException or InvalidOperationException or FormatException or ArgumentException)
{return "Timeline preset contains invalid filter JSON.";}
return null;
}
public static void Remap(TimelineViewPreset preset,int projectId,Func<string,int?,int?> remap)
{
int? Map(string collection,int? id)=>collection=="Project" ? projectId : remap(collection,id);
if(FocusCollection(preset.FocusType) is string focus)preset.FocusID=Map(focus,preset.FocusID);
var filter=Read(preset.FilterJson);
foreach(var reference in References)
if(filter[reference.Key] is JsonNode value)filter[reference.Key]=Map(reference.Value,value.GetValue<int>());
if(FocusCollection(filter["FocusType"]?.GetValue<string>()) is string embeddedFocus && filter["FocusID"] is JsonNode focusId)
filter["FocusID"]=Map(embeddedFocus,focusId.GetValue<int>());
preset.FilterJson=filter.ToJsonString();
}
}