using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using DueskWPF.Models; using DueskWPF.Services; namespace DueskWPF.Views; public partial class ProfileEditWindow : Window { private readonly Profile? _editProfile; private readonly ApiService _apiService = new(); private List _groups = new(); private List _seItems = new List(Enumerable.Repeat(2, 36)); private List _feItems = new List(Enumerable.Repeat(2, 36)); private List _seRatings = new(); private List _feRatings = new(); public ProfileEditWindow(Profile? profile = null) { InitializeComponent(); _editProfile = profile; if (profile != null) { NameBox.Text = profile.Name; _seItems = profile.GetAllSEItems(); _feItems = profile.GetAllFEItems(); } Loaded += async (s, e) => await LoadGroupsAsync(); InitializeItemsControls(); } private async Task LoadGroupsAsync() { _groups = await _apiService.GetGroupsAsync(); GroupCombo.ItemsSource = new List { "Keine Gruppe" } .Concat(_groups.Select(g => g.Name)) .Concat(new[] { "+ Neue Gruppe..." }) .ToList(); GroupCombo.SelectedIndex = 0; if (_editProfile?.Gruppename != null) { var index = GroupCombo.Items.Cast().ToList() .FindIndex(x => x == _editProfile.Gruppename); if (index >= 0) GroupCombo.SelectedIndex = index; } } private void InitializeItemsControls() { for (int i = 0; i < 36; i++) { _seRatings.Add(new ItemRating { Index = i, Name = Norms.Items[i], Value = _seItems[i] }); _feRatings.Add(new ItemRating { Index = i, Name = Norms.Items[i], Value = _feItems[i] }); } SEItemsControl.ItemsSource = _seRatings; FEItemsControl.ItemsSource = _feRatings; } private void GroupCombo_SelectionChanged(object sender, SelectionChangedEventArgs e) { var selected = GroupCombo.SelectedItem as string; NewGroupPanel.Visibility = selected == "+ Neue Gruppe..." ? Visibility.Visible : Visibility.Collapsed; } private void Rating_Checked(object sender, RoutedEventArgs e) { var radio = sender as RadioButton; var tag = radio?.Tag?.ToString(); var value = int.TryParse(tag, out var v) ? v : 2; var parent = FindParent(radio); if (parent?.DataContext is ItemRating rating) { rating.Value = value; // Find which items control this belongs to if (FindParent(radio) == SEItemsControl) _seItems[rating.Index] = value; else _feItems[rating.Index] = value; } } private T? FindParent(DependencyObject child) where T : DependencyObject { var parent = System.Windows.Media.VisualTreeHelper.GetParent(child); while (parent != null && !(parent is T)) parent = System.Windows.Media.VisualTreeHelper.GetParent(parent); return parent as T; } private async void SaveButton_Click(object sender, RoutedEventArgs e) { var name = NameBox.Text.Trim(); if (string.IsNullOrEmpty(name)) { MessageBox.Show("Bitte geben Sie einen Namen ein", "Fehler", MessageBoxButton.OK, MessageBoxImage.Warning); return; } SaveButton.IsEnabled = false; int? groupId = null; var selectedGroup = GroupCombo.SelectedItem as string; if (selectedGroup == "+ Neue Gruppe..." && !string.IsNullOrEmpty(NewGroupBox.Text)) { var success = await _apiService.CreateGroupAsync(NewGroupBox.Text.Trim()); if (success) { _groups = await _apiService.GetGroupsAsync(); var newGroup = _groups.FirstOrDefault(g => g.Name == NewGroupBox.Text.Trim()); if (newGroup != null) groupId = newGroup.GruppeID; } } else if (selectedGroup != null && selectedGroup != "Keine Gruppe" && selectedGroup != "+ Neue Gruppe...") { var group = _groups.FirstOrDefault(g => g.Name == selectedGroup); if (group != null) groupId = group.GruppeID; } var profileData = new ProfileCreate { Name = name, GruppeID = groupId, Gruppename = selectedGroup?.StartsWith("+") == true ? NewGroupBox.Text : selectedGroup }; // Set all 72 items for (int i = 0; i < 36; i++) { typeof(ProfileCreate).GetProperty($"Item{i + 1}")?.SetValue(profileData, _seItems[i]); typeof(ProfileCreate).GetProperty($"Feitem{i + 1}")?.SetValue(profileData, _feItems[i]); } bool success; if (_editProfile != null) success = await _apiService.UpdateProfileAsync(_editProfile); else success = await _apiService.CreateProfileAsync(profileData); if (success) { DialogResult = true; Close(); } else { MessageBox.Show("Fehler beim Speichern des Profils", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error); SaveButton.IsEnabled = true; } } private void CancelButton_Click(object sender, RoutedEventArgs e) { DialogResult = false; Close(); } private class ItemRating { public int Index { get; set; } public string Name { get; set; } = string.Empty; public int Value { get; set; } } }