#nullable enable using System; using System.Collections; using System.Globalization; using ReminderOrganizer.Models; namespace ReminderOrganizer.Core { public enum TaskFilterMode { All, Today, Overdue } /// Groups in the "Upcoming" view, in display order. public enum TaskBucket { Overdue, Today, Tomorrow, ThisWeek, Later, Done } /// Pure filter/sort/group rules for the task list. public static class TaskQuery { public static TaskBucket Bucket(ReminderTask t, DateTime now) { if (t.IsNotified) return TaskBucket.Done; if (t.ReminderDate < now) return TaskBucket.Overdue; var days = (t.ReminderDate.Date - now.Date).Days; return days switch { 0 => TaskBucket.Today, 1 => TaskBucket.Tomorrow, < 7 => TaskBucket.ThisWeek, _ => TaskBucket.Later }; } /// Category filter value for "appointments only" (everything that is not an anniversary). public const string AppointmentsFilter = "_appointments"; public static bool MatchesCategory(ReminderTask t, string? categoryFilter) => categoryFilter switch { null or "" => true, AppointmentsFilter => !t.IsAnniversary, _ => t.IsAnniversary && (t.CategoryId ?? Category.BirthdayId) == categoryFilter }; /// Group key for month grouping: first day of the month. public static DateTime MonthKey(ReminderTask t) => new(t.ReminderDate.Year, t.ReminderDate.Month, 1); public static bool Matches(ReminderTask t, string? search, TaskFilterMode mode, bool hideDone, DateTime now, string? categoryFilter = null) { if (hideDone && t.IsNotified) return false; if (!MatchesCategory(t, categoryFilter)) return false; switch (mode) { case TaskFilterMode.Today when t.ReminderDate.Date != now.Date: return false; case TaskFilterMode.Overdue when t.IsNotified || t.ReminderDate >= now: return false; } if (string.IsNullOrWhiteSpace(search)) return true; var s = search.Trim(); return Contains(t.Name, s) || Contains(t.Description, s) || Contains(t.FolderName, s) || Contains(t.Category?.Name, s); } private static bool Contains(string? text, string s) => text != null && CultureInfo.CurrentCulture.CompareInfo.IndexOf(text, s, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace) >= 0; /// Sort: bucket (overdue, today, … done), then date, then name. public sealed class Comparer : IComparer { private readonly Func _now; private readonly bool _chronological; /// true = plain date order (month grouping), false = overdue/today/…/done. public Comparer(Func? now = null, bool chronological = false) { _now = now ?? (() => DateTime.Now); _chronological = chronological; } public int Compare(object? x, object? y) { if (x is not ReminderTask a || y is not ReminderTask b) return 0; if (_chronological) { int d = a.ReminderDate.CompareTo(b.ReminderDate); return d != 0 ? d : string.Compare(a.Name, b.Name, StringComparison.CurrentCultureIgnoreCase); } var now = _now(); int c = Bucket(a, now).CompareTo(Bucket(b, now)); if (c != 0) return c; c = a.IsNotified ? b.ReminderDate.CompareTo(a.ReminderDate) // done: newest first : a.ReminderDate.CompareTo(b.ReminderDate); return c != 0 ? c : string.Compare(a.Name, b.Name, StringComparison.CurrentCultureIgnoreCase); } } } }