#nullable enable
using System;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using Microsoft.Toolkit.Uwp.Notifications;
namespace ReminderOrganizer.Services
{
///
/// Windows toast notifications (Action Center) with snooze buttons. Falls back to the tray balloon if toasts
/// are not available (e.g. notifications turned off or an old Windows build).
///
public class NotificationService : INotificationService
{
private readonly ILocalizationService _loc;
private string? _logoPath;
public static NotifyIcon? SharedTrayIcon { get; set; }
public NotificationService(ILocalizationService localizationService)
{
_loc = localizationService;
}
public void ShowNotification(string title, string message)
{
try
{
Builder(title, message).AddArgument("action", "open").Show();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[Toast] {ex.Message}");
ShowBalloon(title, message);
}
}
public void ShowReminder(string title, string message, string taskId, bool isLead)
{
try
{
string lead = isLead ? "1" : "0";
ToastButton Snooze(string label, string minutes) => new ToastButton()
.SetContent(label)
.AddArgument("action", "snooze").AddArgument("min", minutes)
.AddArgument("id", taskId).AddArgument("lead", lead)
.SetBackgroundActivation();
Builder(title, message)
.AddArgument("action", "open").AddArgument("id", taskId)
.AddButton(Snooze(_loc.GetString("Snooze10"), "10"))
.AddButton(Snooze(_loc.GetString("Snooze60"), "60"))
.AddButton(Snooze(_loc.GetString("SnoozeTomorrow"), "tomorrow"))
.AddButton(new ToastButton()
.SetContent(_loc.GetString("ToastDone"))
.AddArgument("action", "done").AddArgument("id", taskId).AddArgument("lead", lead)
.SetBackgroundActivation())
.Show();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[Toast] {ex.Message}");
ShowBalloon(title, message);
}
}
private ToastContentBuilder Builder(string title, string message)
{
var b = new ToastContentBuilder()
.AddText(string.IsNullOrWhiteSpace(title) ? "Reminder Organizer" : title);
if (!string.IsNullOrWhiteSpace(message)) b.AddText(message);
if (LogoPath() is { } logo) b.AddAppLogoOverride(new Uri(logo), ToastGenericAppLogoCrop.Default);
return b;
}
/// Toasts need the logo as a file – extract it once to the app folder.
private string? LogoPath()
{
if (_logoPath != null) return _logoPath;
try
{
var path = Path.Combine(DataService.AppDirectory, "toast-logo.png");
if (!File.Exists(path))
{
var res = System.Windows.Application.GetResourceStream(new Uri("pack://application:,,,/Assets/app.png"));
if (res == null) return null;
Directory.CreateDirectory(DataService.AppDirectory);
using var fs = File.Create(path);
res.Stream.CopyTo(fs);
}
return _logoPath = path;
}
catch
{
return null;
}
}
private static void ShowBalloon(string title, string message)
{
try
{
if (SharedTrayIcon == null) return;
// ShowBalloonTip throws on empty text – tasks without description would never notify.
SharedTrayIcon.BalloonTipTitle = string.IsNullOrWhiteSpace(title) ? "Reminder Organizer" : title;
SharedTrayIcon.BalloonTipText = string.IsNullOrWhiteSpace(message) ? " " : message;
SharedTrayIcon.BalloonTipIcon = ToolTipIcon.Info;
SharedTrayIcon.ShowBalloonTip(5000);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[Balloon] {ex.Message}");
}
}
}
}