#nullable enable using System; using System.Collections.Generic; using System.Globalization; using ReminderOrganizer.Core; namespace ReminderOrganizer.Models { /// Sidebar entry for a category (count = open anniversaries). public record CategoryNavItem(string Id, string Name, string Glyph, string Color, int Count, bool IsActive); /// /// Kind of anniversary (birthday, wedding, jubilee …). Text uses {0} = years, {o} = ordinal ("10." / "10th"). /// public class Category : ViewModelBase { public const string BirthdayId = "birthday"; public const string WeddingId = "wedding"; public const string JubileeId = "jubilee"; public const string MemorialId = "memorial"; /// Segoe MDL2 glyphs offered in the editor (theme-colored, no emojis). public static readonly string[] Glyphs = { "", // balloon "", // heart "", // star "", // people "", // message "", // home "", // flag "", // history "", // calendar "", // music "", // pet (dog/paw-like) "", // light bulb }; private string _id = Guid.NewGuid().ToString("N"); private string _name = ""; private string _glyph = Glyphs[0]; private string _color = Folder.DefaultColor; private string _format = "{0}"; private bool _isWedding; public string Id { get => _id; set => SetProperty(ref _id, value); } public string Name { get => _name; set => SetProperty(ref _name, value); } public string Glyph { get => _glyph; set => SetProperty(ref _glyph, value); } public string Color { get => _color; set => SetProperty(ref _color, value); } /// e.g. "turns {0}", "{o} anniversary", "{0} years ago". public string Format { get => _format; set => SetProperty(ref _format, value); } /// Adds the traditional wedding name (Silver, Gold …). public bool IsWedding { get => _isWedding; set => SetProperty(ref _isWedding, value); } public bool IsPreset => Id is BirthdayId or WeddingId or JubileeId or MemorialId; public Category Clone() => new() { Id = Id, Name = Name, Glyph = Glyph, Color = Color, Format = Format, IsWedding = IsWedding }; /// Default categories in the given language. public static List Defaults(Func L) => new() { new() { Id = BirthdayId, Name = L("CatBirthday"), Glyph = "", Color = "#EC4899", Format = L("CatBirthdayFormat") }, new() { Id = WeddingId, Name = L("CatWedding"), Glyph = "", Color = "#F59E0B", Format = L("CatWeddingFormat"), IsWedding = true }, new() { Id = JubileeId, Name = L("CatJubilee"), Glyph = "", Color = "#3B82F6", Format = L("CatJubileeFormat") }, new() { Id = MemorialId, Name = L("CatMemorial"), Glyph = "", Color = "#64748B", Format = L("CatMemorialFormat") }, }; /// /// Presets that still carry a default text of some language get the texts of the new language /// (user-edited names/formats stay untouched). /// public static void RelocalizePresets(IEnumerable categories, Func L, IEnumerable> allLanguages) { var langs = new List>(allLanguages); foreach (var c in categories) { if (!c.IsPreset) continue; var key = c.Id switch { BirthdayId => "CatBirthday", WeddingId => "CatWedding", JubileeId => "CatJubilee", _ => "CatMemorial" }; if (langs.Exists(d => d.TryGetValue(key, out var n) && n == c.Name)) c.Name = L(key); if (langs.Exists(d => d.TryGetValue(key + "Format", out var f) && f == c.Format)) c.Format = L(key + "Format"); } } /// Years for which a wedding anniversary has a traditional name (key "Wedding{n}"). public static readonly int[] WeddingYears = { 1, 5, 10, 15, 20, 25, 30, 40, 50, 60, 65, 70 }; /// "10th" (en) / "10." (de). public static string Ordinal(int n, CultureInfo culture) { if (culture.TwoLetterISOLanguageName != "en") return n + "."; var suffix = (n % 100) is 11 or 12 or 13 ? "th" : (n % 10) switch { 1 => "st", 2 => "nd", 3 => "rd", _ => "th" }; return n + suffix; } /// Applies the format; for weddings appends the traditional name if known. public string FormatYears(int years, CultureInfo culture, Func L) { var text = (Format ?? "{0}") .Replace("{o}", Ordinal(years, culture)) .Replace("{0}", years.ToString(culture)); if (IsWedding && Array.IndexOf(WeddingYears, years) >= 0) { var name = L("Wedding" + years); if (!string.IsNullOrEmpty(name) && name != "Wedding" + years) text += " · " + name; } return text; } } }