using System.Security.Claims; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.ViewEngines; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using PlotLine.Controllers; using PlotLine.Data; using PlotLine.Models; using PlotLine.Services; using PlotLine.ViewModels; // Isolated, loopback-only host: renders real compiled Razor chrome, never starts app workers or connects to a database. internal static class ToolbarBrowserHost { public static async Task RunAsync() { var builder = WebApplication.CreateBuilder(new WebApplicationOptions { ApplicationName = typeof(ToolbarController).Assembly.FullName }); builder.Services.AddControllersWithViews().AddApplicationPart(typeof(ToolbarController).Assembly); builder.Services.AddSingleton(); builder.Services.AddHttpContextAccessor(); builder.Services.AddSingleton(); builder.Services.AddScoped(services => new ToolbarTestUser(int.Parse(services.GetRequiredService().HttpContext!.User.FindFirstValue(ClaimTypes.NameIdentifier)!))); var app = builder.Build(); app.Urls.Add("http://127.0.0.1:5238"); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.GetFullPath("PlotLine/wwwroot")) }); app.Use(async (context, next) => { var user = context.Request.Cookies["fixture-user"] == "5" ? "5" : "4"; context.User = new ClaimsPrincipal(new ClaimsIdentity([new Claim(ClaimTypes.NameIdentifier, user), new Claim(ClaimTypes.Name, "Fixture Author")], "Fixture")); await next(); }); app.UseRouting(); app.UseAuthorization(); app.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}"); app.MapGet("/fixture", async context => { var model = new ApplicationNavigationViewModel { ActiveSection = "Calendar", ShowCompanionPresence = true }; if (context.Request.Query["project"] != "none") { model.Project = new Project { ProjectID = 7, ProjectName = "The Glass Harbour", AccessRole = "Owner" }; model.Book = new Book { BookID = 12, ProjectID = 7, BookTitle = "A Distant Shore", BookNumber = 1 }; model.Books = [model.Book, new Book { BookID = 13, ProjectID = 7, BookTitle = "The Return", BookNumber = 2 }]; } var route = new RouteData(); route.Values["controller"] = "StoryCalendar"; route.Values["action"] = "Index"; var action = new ActionContext(context, route, new ActionDescriptor()); async Task Render(string name, object? viewModel = null) { var view = context.RequestServices.GetRequiredService().GetView(null, $"/Views/{name}.cshtml", false); if (!view.Success) throw new Exception(string.Join(",", view.SearchedLocations)); using var writer = new StringWriter(); var data = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { Model = viewModel ?? model }; var temp = new TempDataDictionary(context, context.RequestServices.GetRequiredService()); await view.View.RenderAsync(new ViewContext(action, view.View, data, temp, writer, new HtmlHelperOptions())); return writer.ToString(); } var menu = await Render("Shared/_ApplicationNavigation"); var toolbar = await Render("Shared/_ApplicationToolbar"); var calendar = new StoryCalendarViewModel { Project = model.Project ?? new(), SelectedBookID = model.Book?.BookID, SelectedMonth = new DateOnly(2026, 10, 1), EarliestStoryDate = new DateOnly(2026, 10, 3), LatestStoryDate = new DateOnly(2026, 10, 3), BookOptions = [new SelectListItem("A Distant Shore", "12"), new SelectListItem("The Return", "13")], MonthOptions = [new SelectListItem("October 2026", "2026-10")], DatedScenes = [new StoryCalendarSceneViewModel { Title = "Arrival", StoryDate = new DateOnly(2026, 10, 3) }], Weeks = Enumerable.Range(0, 5).Select(week => new StoryCalendarWeekViewModel { Days = Enumerable.Range(0, 7).Select(day => new StoryCalendarDayViewModel { Date = new DateOnly(2026, 9, 28).AddDays(week * 7 + day), IsInSelectedMonth = week * 7 + day >= 3 }).ToList() }).ToList() }; var surface = await Render("StoryCalendar/_StoryCalendarSurface", calendar); context.Response.ContentType = "text/html; charset=utf-8"; await context.Response.WriteAsync("
" + menu + toolbar + "
" + surface + "
"); }); await app.RunAsync(); } }