using Avalonia.Controls; using Avalonia.Interactivity; using LehrerApp.Core.Interfaces; using LehrerApp.Core.Models; using LehrerApp.Desktop.Services; using LehrerApp.Desktop.ViewModels.Groups; using Microsoft.Extensions.DependencyInjection; namespace LehrerApp.Desktop.Views.Groups; public partial class LessonDialog : Window { public LessonDialog() => InitializeComponent(); protected override void OnDataContextChanged(EventArgs e) { base.OnDataContextChanged(e); if (DataContext is LessonDialogViewModel vm) vm.OnPickAlternativePath = ShowAlternativePathDialog; } private async Task ShowAlternativePathDialog(Guid? currentId) { var dialogVm = new AlternativePathDialogViewModel( App.Services.GetRequiredService(), currentId); var dialog = new AlternativePathDialog { DataContext = dialogVm }; var ok = await dialog.ShowDialog(this); return ok ? dialogVm.Result : null; } private void OnSave(object? s, RoutedEventArgs e) { if (DataContext is LessonDialogViewModel vm && vm.SaveCommand.CanExecute(null)) { vm.SaveCommand.Execute(null); if (vm.Result is not null) Close(true); } } private void OnCancel(object? s, RoutedEventArgs e) => Close(false); /// KI-Unterstützung mit Fokus auf genau diese (bereits gespeicherte) Stunde, statt den Umweg /// über die Einheiten-Übersicht nehmen zu müssen (4.5.22, Nutzer-Feedback). Die Übernahme im /// AiAssistDialog speichert direkt ins Repository — dieser Dialog schließt sich danach mit dem /// frisch geladenen Stand, statt seine eigenen (jetzt veralteten) Feldwerte zu speichern. private async void OnAiAssist(object? s, RoutedEventArgs e) { if (DataContext is not LessonDialogViewModel vm || vm.EditingLesson is null) return; var unitRepo = App.Services.GetRequiredService(); var unit = unitRepo.GetById(vm.UnitId); if (unit is null) return; var lessonRepo = App.Services.GetRequiredService(); var currentLesson = lessonRepo.GetByUnit(vm.UnitId).FirstOrDefault(l => l.Id == vm.EditingLesson.Id); if (currentLesson is null) return; var dialogVm = new AiAssistDialogViewModel( App.Services.GetRequiredService(), App.Services.GetRequiredService(), lessonRepo, unit, focusLesson: currentLesson); var dialog = new AiAssistDialog { DataContext = dialogVm }; var ok = await dialog.ShowDialog(this); if (!ok) return; var updated = lessonRepo.GetByUnit(vm.UnitId).FirstOrDefault(l => l.Id == vm.EditingLesson.Id); if (updated is not null) vm.MarkAppliedExternally(updated); Close(true); } }