using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LehrerApp.Core.Interfaces;
using LehrerApp.Core.Models;
using LehrerApp.Desktop.ViewModels.Groups;
using System.Collections.ObjectModel;
namespace LehrerApp.Desktop.ViewModels.Students;
/// Einfacher "einen Schüler wählen"-Dialog für Einstiegspunkte ohne bereits geöffneten
/// Schüler (z.B. das Formulare-Menü) - anders als
/// ohne Gruppenbezug/Mitgliedschaftszeitraum, einfach alle Schüler durchsuchbar.
public partial class StudentPickerDialogViewModel : ObservableObject
{
private readonly IStudentRepository _students;
[ObservableProperty] private string _searchText = "";
[ObservableProperty] private StudentPickerItem? _selectedStudent;
[ObservableProperty] private string _validationMessage = "";
public ObservableCollection Students { get; } = [];
public Student? Result { get; private set; }
public StudentPickerDialogViewModel(IStudentRepository students)
{
_students = students;
LoadStudents();
}
partial void OnSearchTextChanged(string value) => LoadStudents();
private void LoadStudents()
{
var matches = _students.GetAll()
.Where(s => string.IsNullOrWhiteSpace(SearchText) ||
s.FullName.Contains(SearchText, StringComparison.OrdinalIgnoreCase))
.OrderBy(s => s.FullName, StringComparer.CurrentCultureIgnoreCase);
Students.Clear();
foreach (var student in matches) Students.Add(new StudentPickerItem(student));
}
[RelayCommand]
private void Select()
{
if (SelectedStudent is null) { ValidationMessage = "Bitte einen Schüler auswählen."; return; }
Result = _students.GetById(SelectedStudent.Id);
}
}