using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using LehrerApp.Core.Interfaces;
using LehrerApp.Core.Models;
using LehrerApp.Desktop.Services;
namespace LehrerApp.Desktop.ViewModels.UntisHub;
/// Eine Zeile im Untis-Hub-Fenster - reine Anzeige-Projektion von
/// , neu aufgebaut bei jedem .
public sealed class UntisHubRowViewModel
{
public UntisHubJobKind Kind { get; }
public Guid? GroupId { get; }
public string GroupName { get; }
public string JobLabel { get; }
public string DueLabel { get; }
public string? LastResultSummary { get; }
public bool IsWarning { get; }
public bool IsDanger { get; }
public UntisHubRowViewModel(UntisHubJobRow row)
{
Kind = row.Kind; GroupId = row.GroupId; GroupName = row.GroupName;
JobLabel = Label(row.Kind); DueLabel = row.DueLabel; LastResultSummary = row.LastResultSummary;
IsWarning = row.DueState == UntisHubDueState.Due;
IsDanger = row.DueState == UntisHubDueState.Overdue;
}
private static string Label(UntisHubJobKind kind) => kind switch
{
UntisHubJobKind.FehlzeitenKurz => "Fehlzeiten (kurzfristig)",
UntisHubJobKind.FehlzeitenLang => "Fehlzeiten (seit Schuljahresbeginn)",
UntisHubJobKind.OffenePeriods => "Offene Stunden",
UntisHubJobKind.Klassenbuchabgleich => "Klassenbuchabgleich",
UntisHubJobKind.Hausaufgabenabgleich => "Hausaufgabenabgleich",
_ => kind.ToString(),
};
}
/// ViewModel des Untis-Hub-Fensters (siehe TODO.md) - zeigt nur den gespeicherten
/// Fälligkeitsstand an (, rein lesend aus LiteDB). Das
/// tatsächliche Ausführen eines Jobs (inkl. WebUntis-Anfrage) übernimmt die Code-Behind-Klasse über
/// , weil dafür ein Fenster-Owner für ShowDialog gebraucht wird.
public partial class UntisHubViewModel : ObservableObject
{
private readonly UntisHubService _hub;
private readonly IGroupRepository _groups;
public ObservableCollection Rows { get; } = [];
[ObservableProperty] private bool _isAvailable;
[ObservableProperty] private string _status = "";
public UntisHubViewModel(UntisHubService hub, IGroupRepository groups, WebUntisIntegrationService untis)
{
_hub = hub; _groups = groups;
IsAvailable = untis.IsAvailable;
Load();
}
public void Load()
{
Rows.Clear();
if (!IsAvailable)
{
Status = "WebUntis ist nicht konfiguriert (siehe Einstellungen).";
return;
}
foreach (var row in _hub.GetRows()) Rows.Add(new UntisHubRowViewModel(row));
var overdue = Rows.Count(r => r.IsDanger);
var due = Rows.Count(r => r.IsWarning);
Status = overdue > 0 || due > 0
? $"{overdue + due} von {Rows.Count} Prüfungen fällig ({overdue} überfällig)."
: $"Alle {Rows.Count} Prüfungen aktuell.";
}
public LearningGroup? FindGroup(Guid id) => _groups.GetById(id);
}