using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using cfs_tracker.Data; using cfs_tracker.Helpers; using cfs_tracker.Models; using cfs_tracker.Services; using Microsoft.Maui.Storage; using System.Collections.ObjectModel; namespace cfs_tracker.ViewModels; public partial class WeekViewModel : ObservableObject { private readonly HealthEntryRepository _healthEntryRepository; private readonly ActivityRepository _activityRepository; private readonly SymptomRepository _symptomRepository; private readonly AppearanceService _appearanceService; private readonly AggregationService _aggregationService; private readonly CalendarStateService _calendarStateService; private readonly IPdfExportService _pdfExportService; [ObservableProperty] private DateTime selectedDate; [ObservableProperty] private DateTime weekStart; [ObservableProperty] private string headerText = string.Empty; public ObservableCollection Days { get; } = new(); public IAsyncRelayCommand PreviousWeekCommand { get; } public IAsyncRelayCommand NextWeekCommand { get; } public IAsyncRelayCommand TodayCommand { get; } public IAsyncRelayCommand ExportPdfCommand { get; } public IAsyncRelayCommand OpenDayCommand { get; } public WeekViewModel( HealthEntryRepository healthEntryRepository, ActivityRepository activityRepository, SymptomRepository symptomRepository, AppearanceService appearanceService, AggregationService aggregationService, CalendarStateService calendarStateService, IPdfExportService pdfExportService) { _healthEntryRepository = healthEntryRepository; _activityRepository = activityRepository; _symptomRepository = symptomRepository; _appearanceService = appearanceService; _aggregationService = aggregationService; _calendarStateService = calendarStateService; _pdfExportService = pdfExportService; PreviousWeekCommand = new AsyncRelayCommand(PreviousWeekAsync); NextWeekCommand = new AsyncRelayCommand(NextWeekAsync); TodayCommand = new AsyncRelayCommand(TodayAsync); ExportPdfCommand = new AsyncRelayCommand(ExportAsync); OpenDayCommand = new AsyncRelayCommand(OpenDayAsync); _calendarStateService.SelectedDateChanged += HandleSharedDateChanged; SelectedDate = _calendarStateService.SelectedDate; } public async Task RefreshAsync() { SelectedDate = _calendarStateService.SelectedDate; WeekStart = DateHelper.StartOfWeekMonday(SelectedDate); HeaderText = $"Woche {WeekStart:dd.MM.yyyy} - {WeekStart.AddDays(6):dd.MM.yyyy}"; var entries = await _healthEntryRepository.GetEntriesBetweenAsync(WeekStart, WeekStart.AddDays(7)); var activityMap = await _activityRepository.GetNameMapAsync(); var symptomsMap = await _symptomRepository.GetSymptomsByEntryIdsAsync(entries.Select(x => x.Id)); Days.Clear(); for (var i = 0; i < 7; i++) { var date = WeekStart.AddDays(i); var dayEntries = entries.Where(x => x.EntryDate.Date == date.Date).ToList(); var morningAverage = _aggregationService.CalculateAverage(_aggregationService.FilterForDayPart(dayEntries, DayPart.Morning)); var afternoonAverage = _aggregationService.CalculateAverage(_aggregationService.FilterForDayPart(dayEntries, DayPart.Afternoon)); Days.Add(new WeekDaySummaryViewModel { Date = date, DateText = DateHelper.ToGermanShortDay(date), MorningText = _aggregationService.ToDisplayText(morningAverage, _appearanceService), AfternoonText = _aggregationService.ToDisplayText(afternoonAverage, _appearanceService), MorningColor = _appearanceService.GetColor(morningAverage.State.ToAppearanceColorKey(), "#1F242B"), AfternoonColor = _appearanceService.GetColor(afternoonAverage.State.ToAppearanceColorKey(), "#1F242B"), ActivitiesText = _aggregationService.BuildDistinctActivityText(dayEntries, activityMap), SymptomsText = _aggregationService.BuildDistinctSymptomText(dayEntries, symptomsMap) }); } } private async Task OpenDayAsync(WeekDaySummaryViewModel? item) { if (item is null) return; _calendarStateService.SelectedDate = item.Date; await Shell.Current.GoToAsync("//day"); } private async Task ExportAsync() { var result = await _pdfExportService.ExportWeekAsync(SelectedDate); await ExportUiHelper.ShowExportSavedActionsAsync(result); } private async Task PreviousWeekAsync() { _calendarStateService.SelectedDate = DateHelper.StartOfWeekMonday(SelectedDate).AddDays(-7); await RefreshAsync(); } private async Task NextWeekAsync() { _calendarStateService.SelectedDate = DateHelper.StartOfWeekMonday(SelectedDate).AddDays(7); await RefreshAsync(); } private async Task TodayAsync() { _calendarStateService.ResetToToday(); await RefreshAsync(); } private async void HandleSharedDateChanged(DateTime date) { SelectedDate = date; await RefreshAsync(); } }