#nullable enable
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using ReminderOrganizer.Core;
namespace ReminderOrganizer.Models
{
public class ReminderTask : ViewModelBase
{
private string _id = Guid.NewGuid().ToString();
private string _name = "New Task";
private string _description = "";
private DateTime _reminderDate = DateTime.Now.AddHours(1);
private bool _isNotified = false;
private bool _isRecurring = false;
private RecurrenceType _recurrenceType = RecurrenceType.Yearly;
public string Id
{
get => _id;
set => SetProperty(ref _id, value);
}
public string Name
{
get => _name;
set => SetProperty(ref _name, value);
}
public string Description
{
get => _description;
set => SetProperty(ref _description, value);
}
public DateTime ReminderDate
{
get => _reminderDate;
set { if (SetProperty(ref _reminderDate, value)) RaiseDisplay(); }
}
public bool IsNotified
{
get => _isNotified;
set { if (SetProperty(ref _isNotified, value)) OnPropertyChanged(nameof(RelativeDate)); }
}
public bool IsRecurring
{
get => _isRecurring;
set { if (SetProperty(ref _isRecurring, value)) OnPropertyChanged(nameof(AgeText)); }
}
public RecurrenceType RecurrenceType
{
get => _recurrenceType;
set { if (SetProperty(ref _recurrenceType, value)) OnPropertyChanged(nameof(AgeText)); }
}
private int _leadDays;
/// Extra reminder this many days before the date (0 = none).
public int LeadDays
{
get => _leadDays;
set => SetProperty(ref _leadDays, Math.Max(0, value));
}
private bool _isPreNotified;
/// Lead reminder already shown for the current occurrence.
public bool IsPreNotified
{
get => _isPreNotified;
set => SetProperty(ref _isPreNotified, value);
}
private int? _birthYear;
/// Optional, for yearly birthdays: shows "turns 40".
public int? BirthYear
{
get => _birthYear;
set { if (SetProperty(ref _birthYear, value)) OnPropertyChanged(nameof(AgeText)); }
}
/// Age reached on the next date (yearly tasks with birth year only).
[System.Text.Json.Serialization.JsonIgnore]
public int? UpcomingAge =>
BirthYear is int y && IsRecurring && RecurrenceType == RecurrenceType.Yearly && ReminderDate.Year > y
? ReminderDate.Year - y : null;
/// Set once by the app: (task, years) → "turns 40" / "10th anniversary · Tin" …
public static Func? AgeFormatter { get; set; }
[System.Text.Json.Serialization.JsonIgnore]
public string? AgeText => UpcomingAge is int a ? (AgeFormatter?.Invoke(this, a) ?? a.ToString()) : null;
private bool _isAnniversary;
/// Anniversary (birthday, wedding …): yearly, date only, reminded at .
/// Stored as "IsBirthday" for compatibility with older files.
[System.Text.Json.Serialization.JsonPropertyName("IsBirthday")]
public bool IsAnniversary
{
get => _isAnniversary;
set { if (SetProperty(ref _isAnniversary, value)) RaiseDisplay(); }
}
private string? _categoryId;
/// Anniversary category (see ); null = birthday.
public string? CategoryId
{
get => _categoryId;
set { if (SetProperty(ref _categoryId, value)) RaiseDisplay(); }
}
/// Set once by the app: id → category.
public static Func? CategoryLookup { get; set; }
[System.Text.Json.Serialization.JsonIgnore]
public Category? Category => IsAnniversary ? CategoryLookup?.Invoke(CategoryId ?? Category.BirthdayId) : null;
/// Accent color: category for anniversaries, otherwise folder.
[System.Text.Json.Serialization.JsonIgnore]
public string AccentColor => Category?.Color ?? FolderColor;
[System.Text.Json.Serialization.JsonIgnore]
public string? CategoryGlyph => Category?.Glyph;
/// Call after categories were edited.
public void RefreshCategory()
{
OnPropertyChanged(nameof(Category));
RaiseDisplay();
}
public const int BirthdayHour = 9;
/// Set once by the app: culture for month names, relative-date formatter.
public static Func? DisplayCulture { get; set; }
public static Func? RelativeFormatter { get; set; }
/// "19. März" for birthdays, otherwise "19.03.2027 09:00".
[System.Text.Json.Serialization.JsonIgnore]
public string DisplayDate => IsAnniversary
? ReminderDate.ToString(DayMonthPattern(DisplayCulture?.Invoke() ?? CultureInfo.CurrentCulture), DisplayCulture?.Invoke() ?? CultureInfo.CurrentCulture)
: ReminderDate.ToString("dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture);
/// "heute", "morgen", "in 5 Tagen", "vor 2 Tagen".
[System.Text.Json.Serialization.JsonIgnore]
public string RelativeDate => IsNotified ? "" : RelativeFormatter?.Invoke(ReminderDate, DateTime.Now) ?? "";
[System.Text.Json.Serialization.JsonIgnore]
public int DaysUntil => (ReminderDate.Date - DateTime.Today).Days;
/// Day number + short month for the card view.
[System.Text.Json.Serialization.JsonIgnore]
public string DayText => ReminderDate.Day.ToString(CultureInfo.InvariantCulture);
[System.Text.Json.Serialization.JsonIgnore]
public string MonthText => ReminderDate.ToString("MMM", DisplayCulture?.Invoke() ?? CultureInfo.CurrentCulture).TrimEnd('.').ToUpperInvariant();
public static string DayMonthPattern(CultureInfo c) => c.TwoLetterISOLanguageName == "de" ? "d. MMMM" : "MMMM d";
private void RaiseDisplay()
{
OnPropertyChanged(nameof(DisplayDate));
OnPropertyChanged(nameof(RelativeDate));
OnPropertyChanged(nameof(DaysUntil));
OnPropertyChanged(nameof(DayText));
OnPropertyChanged(nameof(MonthText));
OnPropertyChanged(nameof(AgeText));
OnPropertyChanged(nameof(AccentColor));
OnPropertyChanged(nameof(CategoryGlyph));
}
/// Next birthday on/after at BirthdayHour (29 Feb → 28 Feb in other years).
public static DateTime NextBirthday(int day, int month, DateTime today)
{
DateTime For(int year) => new(year, month, Math.Min(day, DateTime.DaysInMonth(year, month)), BirthdayHour, 0, 0);
var d = For(today.Year);
return d.Date >= today.Date ? d : For(today.Year + 1);
}
private string _folderName = "";
/// Runtime only: name of the folder holding this task (for the "Upcoming" view).
[System.Text.Json.Serialization.JsonIgnore]
public string FolderName { get => _folderName; set => SetProperty(ref _folderName, value); }
private string _folderColor = Folder.DefaultColor;
[System.Text.Json.Serialization.JsonIgnore]
public string FolderColor { get => _folderColor; set { if (SetProperty(ref _folderColor, value)) OnPropertyChanged(nameof(AccentColor)); } }
public DateTime GetNextOccurrence() => GetNextOccurrence(DateTime.Now);
///
/// First occurrence after . Steps are counted from the original
/// date, so "31 Jan monthly" gives 29 Feb, 31 Mar, 30 Apr … instead of drifting to the 28th.
///
public DateTime GetNextOccurrence(DateTime now)
{
var start = ReminderDate;
if (start > now) return start;
// Jump close to "now" first so long gaps (app off for years) don't loop thousands of times.
int k = RecurrenceType switch
{
RecurrenceType.Daily => Math.Max(0, (int)((now - start).TotalDays) - 1),
RecurrenceType.Weekly => Math.Max(0, (int)((now - start).TotalDays / 7) - 1),
RecurrenceType.Monthly => Math.Max(0, (now.Year - start.Year) * 12 + now.Month - start.Month - 1),
_ => Math.Max(0, now.Year - start.Year - 1)
};
DateTime next;
do
{
k++;
next = RecurrenceType switch
{
RecurrenceType.Daily => start.AddDays(k),
RecurrenceType.Weekly => start.AddDays(7 * k),
RecurrenceType.Monthly => start.AddMonths(k),
_ => start.AddYears(k)
};
} while (next <= now);
return next;
}
///
/// Marks every due task: one-off tasks become notified, recurring ones move to their next date.
/// Also emits lead reminders (LeadDays before the date) once per occurrence.
/// Returns what should be shown now; for lead reminders is the upcoming date.
///
public static System.Collections.Generic.List ProcessDue(System.Collections.Generic.IEnumerable folders, DateTime now)
{
var due = new System.Collections.Generic.List();
foreach (var folder in folders)
{
foreach (var task in folder.Tasks)
{
if (task.IsNotified) continue;
if (task.ReminderDate <= now)
{
due.Add(new DueReminder(task, task.ReminderDate, false, task.UpcomingAge));
task.IsPreNotified = false;
if (task.IsRecurring)
task.ReminderDate = task.GetNextOccurrence(now);
else
task.IsNotified = true;
}
else if (task.LeadDays > 0 && !task.IsPreNotified && now >= task.ReminderDate.AddDays(-task.LeadDays))
{
due.Add(new DueReminder(task, task.ReminderDate, true, task.UpcomingAge));
task.IsPreNotified = true;
}
}
}
return due;
}
public ReminderTask() { }
public ReminderTask(string name, string description, DateTime reminderDate)
{
Name = name;
Description = description;
ReminderDate = reminderDate;
}
}
/// One notification to show. Age is the age reached on (birthdays).
public record DueReminder(ReminderTask Task, DateTime Date, bool IsLead, int? Age);
public enum RecurrenceType
{
Daily,
Weekly,
Monthly,
Yearly
}
public class Folder : ViewModelBase
{
private string _id = Guid.NewGuid().ToString();
private string _name = "New Folder";
private ObservableCollection _tasks = new();
public string Id
{
get => _id;
set => SetProperty(ref _id, value);
}
public string Name
{
get => _name;
set => SetProperty(ref _name, value);
}
public ObservableCollection Tasks
{
get => _tasks;
set => SetProperty(ref _tasks, value);
}
public const string DefaultColor = "#6366F1";
public static readonly string[] Palette =
{ "#6366F1", "#3B82F6", "#14B8A6", "#22C55E", "#F59E0B", "#F97316", "#EF4444", "#EC4899", "#64748B" };
private string _color = DefaultColor;
public string Color
{
get => _color;
set => SetProperty(ref _color, string.IsNullOrWhiteSpace(value) ? DefaultColor : value);
}
public Folder() { }
public Folder(string name)
{
Name = name;
}
}
public class AppData
{
public ObservableCollection Folders { get; set; } = new();
/// "de"/"en"; empty = not chosen yet → Windows display language.
public string Language { get; set; } = "";
/// null = not chosen yet → Windows app theme.
public bool? IsDarkMode { get; set; }
public bool PlaySound { get; set; } = true;
public bool HideDone { get; set; }
/// "list", "compact" or "cards".
public string ViewMode { get; set; } = "list";
/// Group the list by month (collapsible).
public bool GroupByMonth { get; set; }
/// Anniversary categories (birthday, wedding, … and own ones). Filled with defaults on first start.
public ObservableCollection Categories { get; set; } = new();
/// Adds missing default categories (keeps user edits of existing ones).
public bool EnsureDefaultCategories(Func L)
{
bool changed = false;
int insertAt = 0;
foreach (var def in Category.Defaults(L))
{
if (Categories.Any(c => c.Id == def.Id)) { insertAt++; continue; }
Categories.Insert(Math.Min(insertAt++, Categories.Count), def);
changed = true;
}
return changed;
}
/// Folders and yearly tasks were checked for birthdays once.
public bool BirthdaysMigrated { get; set; }
public static bool LooksLikeBirthdayFolder(string? name) =>
name != null && (name.Contains("geburtstag", StringComparison.OrdinalIgnoreCase) ||
name.Contains("birthday", StringComparison.OrdinalIgnoreCase));
/// Yearly tasks in a "Geburtstage"/"Birthdays" folder become birthday entries (one-time).
public int MigrateBirthdays()
{
int n = 0;
if (!BirthdaysMigrated)
{
BirthdaysMigrated = true;
n += MarkYearly(LooksLikeBirthdayFolder, Category.BirthdayId);
}
if (!WeddingsMigrated)
{
WeddingsMigrated = true;
n += MarkYearly(LooksLikeWeddingFolder, Category.WeddingId);
}
return n;
}
public bool WeddingsMigrated { get; set; }
public static bool LooksLikeWeddingFolder(string? name) =>
name != null && (name.Contains("hochzeit", StringComparison.OrdinalIgnoreCase) ||
name.Contains("wedding", StringComparison.OrdinalIgnoreCase) ||
name.Contains("jahrestag", StringComparison.OrdinalIgnoreCase) ||
name.Contains("anniversar", StringComparison.OrdinalIgnoreCase));
/// Category a new anniversary in this folder should get (null = no hint).
public static string? CategoryHintForFolder(string? name) =>
LooksLikeBirthdayFolder(name) ? Category.BirthdayId : LooksLikeWeddingFolder(name) ? Category.WeddingId : null;
private int MarkYearly(Func folderMatches, string categoryId)
{
int n = 0;
foreach (var f in Folders)
{
if (!folderMatches(f.Name)) continue;
foreach (var t in f.Tasks)
{
if (t.IsAnniversary || !t.IsRecurring || t.RecurrenceType != RecurrenceType.Yearly) continue;
t.IsAnniversary = true;
t.CategoryId = categoryId;
n++;
}
}
return n;
}
}
}