using System.Diagnostics; using CodeLiturgy.Views.Controllers; using CodeLiturgy.Views.Languages; using Microsoft.AspNetCore.Mvc.ViewFeatures; namespace CodeLiturgy.Views.Utils; internal class LayoutCache { #region Route Tree private static readonly RouteRecord NonLoggedInRoot = new RouteRecord( AuthLoginRoute, AuthLoginKeyName, nameof(AuthController), new List()); private static readonly RouteRecord Root = new RouteRecord( RootKeyName, RootLocation, nameof(HomeController), new List(), ViewType.Root); #endregion Route Tree #region Routing Utils internal static readonly RouteRecord SystemRoute = Root.Children.FirstOrDefault(x => x.ViewType == ViewType.System)!; internal static readonly RouteRecord DataRouteRecord = Root.Children.FirstOrDefault(x => x.ViewType == ViewType.Data)!; internal static readonly RouteRecord AccountRouteRecord = Root.Children.FirstOrDefault(x => x.ViewType == ViewType.Account)!; #endregion Routing Utils #region Internal Menus internal static List GetDefaultFooterMenu(ViewDataDictionary dictionary) { var location = GetUserLanguage(dictionary); var menu = LayoutCache .Root .Children; if (dictionary[FooterMenuViewDataId] is List footerMenu) { menu = footerMenu; } return menu .Select(x => { if (SiteContent.RouteTitle[x.routeKey].ContainsKey(location)) { return new RouteView(SiteContent.RouteTitle[x.routeKey][location], location); } return new RouteView(SiteContent.RouteTitle[x.routeKey][DefaultCultureName], x.location); }) .ToList(); } internal static List GetDefaultHeaderMenu(ViewDataDictionary dictionary, bool userAuthenticated = false) { if (!userAuthenticated) { var menuToShow = new List(); menuToShow.Add(new RouteView("Blue West", "/")); return menuToShow; } var location = GetUserLanguage(dictionary); var menu = LayoutCache .Root .Children; if (dictionary[HeaderMenuId] is List footerMenu) { menu = footerMenu; } return menu .Select(x => { if (SiteContent.RouteTitle[x.routeKey].ContainsKey(location)) { return new RouteView(SiteContent.RouteTitle[x.routeKey][location], x.Location); } return new RouteView(SiteContent.RouteTitle[x.routeKey][DefaultCultureName], x.location); }) .ToList(); } [Conditional("DEBUG")] internal static void IsDevMode(ref bool isDebug) { isDebug = true; } internal static string GetUserLanguage(ViewDataDictionary dictionary) { return (dictionary[LanguageViewStorage] as string)!; } #endregion Internal Menus }