#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
};
}
public static bool Matches(ReminderTask t, string? search, TaskFilterMode mode, bool hideDone, DateTime now)
{
if (hideDone && t.IsNotified) 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);
}
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;
public Comparer(Func? now = null) => _now = now ?? (() => DateTime.Now);
public int Compare(object? x, object? y)
{
if (x is not ReminderTask a || y is not ReminderTask b) return 0;
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);
}
}
}
}