using Microsoft.AspNetCore.Mvc.Rendering; using PlotLine.ViewModels; namespace PlotLine.Services; // Presentation of the existing chronological Story Point projection, not another // knowledge store. Selection never changes author records or generates facts. public static class WritingContextRelevance { public static IReadOnlyList SceneOptions(StoryPointViewModel narrative,int book,int current) { var chapters=narrative.Chapters.Where(c=>c.BookID==book).ToDictionary(c=>c.ChapterID); return narrative.Scenes.Where(s=>chapters.ContainsKey(s.ChapterID)).Select(s=>new SelectListItem( $"Chapter {chapters[s.ChapterID].ChapterNumber:0.##} ยท {s.SceneTitle}",s.SceneID.ToString(),s.SceneID==current)).ToArray(); } public static IReadOnlyList Nearby(StoryContextViewModel context) { if(context.Narrative?.Snapshot is not {} point||context.Scene?.StartDateTime is not {} date)return []; var chapters=context.Narrative.Chapters.Where(c=>c.BookID==point.Book.BookID).ToDictionary(c=>c.ChapterID); return context.Narrative.Scenes.Where(s=>s.SceneID!=point.Scene.SceneID&&point.Sources.ContainsKey(s.SceneID)&&chapters.ContainsKey(s.ChapterID)&& s.StartDateTime.HasValue&&Math.Abs((s.StartDateTime.Value-date).TotalDays)<=3) .OrderBy(s=>Math.Abs((s.StartDateTime!.Value-date).TotalSeconds)).Take(5) .Select(s=>new StoryCalendarSceneViewModel{SceneID=s.SceneID,BookID=point.Book.BookID,ChapterID=s.ChapterID,ChapterNumber=chapters[s.ChapterID].ChapterNumber, SceneNumber=s.SceneNumber,Title=s.SceneTitle,StartDateTime=s.StartDateTime}).ToArray(); } public static IReadOnlyList Threads(StoryContextViewModel context) { if(context.Narrative?.Snapshot is not {} point)return []; var current=context.Scene?.ThreadEvents.Select(e=>e.PlotThreadID).ToHashSet()??[]; var viewpoint=point.ViewpointKnowledge.Where(k=>k.PlotThreadID.HasValue).Select(k=>k.PlotThreadID!.Value).ToHashSet(); var people=point.Characters.SelectMany(c=>c.Knowledge).Where(k=>k.PlotThreadID.HasValue).Select(k=>k.PlotThreadID!.Value).ToHashSet(); var chapterScenes=context.Narrative.Scenes.Where(s=>s.ChapterID==point.Chapter.ChapterID).Select(s=>s.SceneID).ToHashSet(); int Score(StoryPointThread t)=>current.Contains(t.PlotThreadID)?4:viewpoint.Contains(t.PlotThreadID)?3:people.Contains(t.PlotThreadID)?2:t.Source!=null&&chapterScenes.Contains(t.Source.SceneID)?1:0; return point.Threads.Where(t=>Score(t)>0).OrderByDescending(Score).ThenBy(t=>t.Title).Take(6).ToArray(); } }