using Demo.Domain.UseCase; using Presence.Desktop.Models; using ReactiveUI; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Reactive; using System.Reactive.Linq; using Avalonia.Controls; using System.IO; using Demo.Domain.Models; namespace Presence.Desktop.ViewModels { public class GroupViewModel : ViewModelBase, IRoutableViewModel { private readonly List _groupPresentersDataSource = new List(); private ObservableCollection _groups; public ObservableCollection Groups => _groups; public GroupPresenter? SelectedGroupItem { get => _selectedGroupItem; set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value); } private GroupPresenter? _selectedGroupItem; public ObservableCollection Users { get => _users;} public ObservableCollection _users; private IGroupUseCase _groupUseCase; private IUserUseCase _userUseCase; private ObservableCollection _secondComboBoxItems = new ObservableCollection { "no sort", "name sort", "name sort rev" }; public ObservableCollection SecondComboBoxItems => _secondComboBoxItems; private string? _selectedSecondItem; public ReactiveCommand DeleteUsersByGroupCommand { get; } public ReactiveCommand ImportStudentsCommand { get; } public string? SelectedSecondItem { get => _selectedSecondItem; set => this.RaiseAndSetIfChanged(ref _selectedSecondItem, value); } private UserPresenter? _selectedUser; public UserPresenter? SelectedUser { get => _selectedUser; set => this.RaiseAndSetIfChanged(ref _selectedUser, value); } private string? _newFIO; public string? NewFIO { get => _newFIO; set => this.RaiseAndSetIfChanged(ref _newFIO, value); } public ReactiveCommand DeleteUserCommand { get; } public ReactiveCommand UpdateUserFIOCommand { get; } public GroupViewModel(IGroupUseCase groupUseCase, IUserUseCase userUseCase) { _groupUseCase = groupUseCase; _userUseCase = userUseCase; _groups = new ObservableCollection(_groupPresentersDataSource); _users = new ObservableCollection(); RefreshGroups(); this.WhenAnyValue(vm => vm.SelectedGroupItem) .Subscribe(_ => { RefreshGroups(); SetUsers(); }); this.WhenAnyValue(vm => vm.SelectedSecondItem) .Subscribe(_ => SetUsers()); SelectedSecondItem = _secondComboBoxItems.First(); DeleteUsersByGroupCommand = ReactiveCommand.Create(() => DeleteUsersByGroupID()); ImportStudentsCommand = ReactiveCommand.CreateFromTask(async () => await ImportStudents()); DeleteUserCommand = ReactiveCommand.Create(DeleteUser); UpdateUserFIOCommand = ReactiveCommand.Create(UpdateUserFIO); } private void RefreshGroups() { _groupPresentersDataSource.Clear(); foreach (var group in _groupUseCase.GetAllGroupsWithUsers()) { var groupPresenter = new GroupPresenter { Id = group.ID, Name = group.Name, users = group.Users?.Select(user => new UserPresenter { Name = user.FIO, Guid = user.Guid, Group = new GroupPresenter { Id = group.ID, Name = group.Name } }).ToList() }; _groupPresentersDataSource.Add(groupPresenter); } _groups = new ObservableCollection(_groupPresentersDataSource); } private async System.Threading.Tasks.Task ImportStudents() { if (SelectedGroupItem == null || SelectedGroupItem.Id == null) return; var openFileDialog = new OpenFileDialog { Filters = new List { new FileDialogFilter { Name = "CSV Files", Extensions = { "csv" } } }, AllowMultiple = false }; var result = await openFileDialog.ShowAsync(new Window()); if (result?.Length > 0) { var filePath = result[0]; var lines = File.ReadAllLines(filePath); foreach (var line in lines.Skip(1)) { var columns = line.Split(','); if (columns.Length < 1) continue; var fio = columns[0]; var newUser = new User { FIO = fio, Group = new Group { Name = SelectedGroupItem.Name, ID = SelectedGroupItem.Id } }; var isCreated = _userUseCase.CreateUser(newUser); if (isCreated) { RefreshGroups(); SetUsers(); } } } } private void DeleteUsersByGroupID() { if (SelectedGroupItem == null) return; var groupID = SelectedGroupItem.Id; bool res = _userUseCase.RemoveUsersByGroupID(groupID); if (res) { Users.Clear(); } } private void SetUsers() { if (SelectedGroupItem == null) return; Users.Clear(); var group = _groups.FirstOrDefault(it => it.Id == SelectedGroupItem.Id); if (group?.users == null) return; var sortedUsers = SelectedSecondItem switch { "name sort" => group.users.OrderBy(user => user.Name), "name sort rev" => group.users.OrderByDescending(user => user.Name), _ => group.users }; foreach (var user in sortedUsers) { Users.Add(user); } } private void DeleteUser(UserPresenter user) { if (user == null) return; var isDeleted = _userUseCase.RemoveUserByGuid(user.Guid); if (isDeleted) { Users.Remove(user); } } private void UpdateUserFIO(UserPresenter user) { if (user == null || string.IsNullOrWhiteSpace(NewFIO)) return; user.Name = NewFIO; var updatedUser = new User { Guid = user.Guid, FIO = NewFIO, Group = new Group { ID = SelectedGroupItem.Id, Name = SelectedGroupItem.Name } }; var isUpdated = _userUseCase.UpdateUser(updatedUser); if (isUpdated != null) { NewFIO = string.Empty; RefreshGroups(); SetUsers(); } } public string? UrlPathSegment { get; } public IScreen HostScreen { get; } } }