using domain.UseCase; using Presence.Desktop.Models; using Presence.Desktop.Helpers; using System; using System.Reactive.Linq; using System.Reactive; using System.Linq; using ReactiveUI; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.IO; using System.Windows.Input; using System.Threading.Tasks; using Avalonia.Controls.Selection; using domain.Request; using System.Text; namespace Presence.Desktop.ViewModels { public class MainWindowViewModel : ViewModelBase { private readonly IGroupUseCase _groupService; private List groupPresenters = new List(); private ObservableCollection _groups; public ObservableCollection Groups { get => _groups; set => this.RaiseAndSetIfChanged(ref _groups, value); } private GroupPresenter? _selectedGroupItem; public GroupPresenter? SelectedGroupItem { get => _selectedGroupItem; set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value); } private ObservableCollection _students; public ObservableCollection Students { get => _students; set => this.RaiseAndSetIfChanged(ref _students, value); } public Interaction ShowOpenFileDialog; private string? _selectedFile; public string? SelectedFile { get => _selectedFile; set => this.RaiseAndSetIfChanged(ref _selectedFile, value); } private bool _MultipleSelected = false; public bool MultipleSelected { get => _MultipleSelected; set => this.RaiseAndSetIfChanged(ref _MultipleSelected, value); } public SelectionModel Selection { get; } private string _selectedSort = "Релевантность"; public string SelectedSort { get => _selectedSort; set => this.RaiseAndSetIfChanged(ref _selectedSort, value); } public List Sorting { get; set; } = [ "Релевантность", "А-Я", "Я-А" ]; public ICommand OpenFileDialog { get; } public ICommand EditCommand { get; } public ICommand DeleteCommand { get; } public ICommand DeleteSelectedCommand { get; } public ICommand DeleteAllCommand { get; } public MainWindowViewModel() { _groupService = null; _groups = new(); _students = new(); OpenFileDialog = ReactiveCommand.CreateFromTask(SelectFile); } public MainWindowViewModel(IGroupUseCase gService) { _groupService = gService; ShowOpenFileDialog = new Interaction(); OpenFileDialog = ReactiveCommand.CreateFromTask(SelectFile); _students = new(); this.WhenAnyValue(vm => vm.SelectedGroupItem, x => x.SelectedSort) .Subscribe(_ => { RefreshGroups(); SetStudents(SelectedSort); }); Selection = new SelectionModel(); Selection.SingleSelect = false; Selection.SelectionChanged += (sender, args) => { MultipleSelected = Selection.SelectedItems.Count > 1; }; DeleteCommand = ReactiveCommand.Create(DeleteStudent); DeleteSelectedCommand = ReactiveCommand.Create(DeleteStudent); DeleteAllCommand = ReactiveCommand.Create(DeleteAllStudents); } private void RefreshGroups() { groupPresenters.Clear(); foreach (var item in _groupService.GetGroupsWithStudents()) { GroupPresenter groupPresenter = new GroupPresenter { Id = item.Id, Name = item.Name, students = item.Users?.Select(u => new StudentPresenter { Id = u.Id, FirstName = u.FirstName, LastName = u.LastName, Patronymic = u.Patronymic, Group = new GroupPresenter { Id = item.Id, Name = item.Name } }).ToList() }; groupPresenters.Add(groupPresenter); } _groups = new(groupPresenters); } private void SetStudents(string selectedSort) { if (SelectedGroupItem == null) return; if (SelectedGroupItem.students == null) return; Students.Clear(); List students = new(_groupService.GetGroupsWithStudents() .Single(g => g.Id == SelectedGroupItem.Id) .Users .Select(u => new StudentPresenter { Id = u.Id, FirstName = u.FirstName, LastName = u.LastName, Patronymic = u.Patronymic, Group = new GroupPresenter() { Id = SelectedGroupItem.Id, Name = SelectedGroupItem.Name } }) ); students = selectedSort switch { "Релевантность" => students.OrderBy(s => s.Id).ToList(), "А-Я" => students.OrderBy(s => s.LastName).ToList(), "Я-А" => students.OrderByDescending(s => s.LastName).ToList(), _ => students }; foreach (var student in students) Students.Add(student); } private async Task SelectFile() { try { SelectedFile = await ShowOpenFileDialog.Handle(Unit.Default); } catch (Exception) { return; } if (SelectedGroupItem == null) return; try { var studentsToAdd = CsvHelper.ReadCsvStudents(SelectedFile); if (studentsToAdd.Any()) { _groupService.AddStudentsToGroup(SelectedGroupItem.Id, studentsToAdd); foreach (var studentRequest in studentsToAdd) { var studentPresenter = new StudentPresenter { LastName = studentRequest.LastName, FirstName = studentRequest.FirstName, Patronymic = studentRequest.Patronymic, Group = SelectedGroupItem }; SelectedGroupItem.students.Add(studentPresenter); Students.Add(studentPresenter); } RefreshGroups(); SetStudents(SelectedSort); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } } private void DeleteStudent() { if (Selection.SelectedItems == null || Selection.SelectedItems.Count == 0) return; var studentIds = Selection.SelectedItems.Select(s => s.Id).ToList(); _groupService.RemoveStudentsFromGroupByIds(SelectedGroupItem.Id, studentIds); foreach (var student in Selection.SelectedItems.ToList()) { SelectedGroupItem.students.Remove(student); Students.Remove(student); } SetStudents(SelectedSort); RefreshGroups(); } private void DeleteAllStudents() { if (Students.Count == 0 && SelectedGroupItem == null) return; _groupService.RemoveStudentsFromGroup(SelectedGroupItem.Id); SelectedGroupItem.students.Clear(); Students.Clear(); RefreshGroups(); SetStudents(SelectedSort); } } }