using Core.Entities;
using WebApp.Models;
namespace WebApp.Services;
internal record ExcludedStudent(int TeamId, int TimeSlotIndex, int StudentId);
///
/// Service for managing meeting schedule state persistence in localStorage.
///
public class MeetingScheduleStateService : IMeetingScheduleStateService
{
private readonly LocalStorageService _localStorage;
private const string ScheduledTeamsKey = "MeetingSchedule_ScheduledTeams";
private const string AbsentStudentsKey = "MeetingSchedule_AbsentStudents";
private const string TimeSlotCountKey = "MeetingSchedule_TimeSlotCount";
private const string ExtendedTeamsKey = "MeetingSchedule_ExtendedTeams";
private const string ExcludedStudentsKey = "MeetingSchedule_ExcludedStudents";
public MeetingScheduleStateService(LocalStorageService localStorage)
{
_localStorage = localStorage;
}
public async Task> LoadScheduledTeamsAsync(Team[] allTeams)
{
var teamIds = await _localStorage.GetIntArrayAsync(ScheduledTeamsKey);
if (teamIds.Length > 0)
{
return allTeams.Where(t => teamIds.Contains(t.Id)).ToArray();
}
return [];
}
public async Task SaveScheduledTeamsAsync(IEnumerable scheduledTeams)
{
var teamIds = scheduledTeams.Select(t => t.Id).ToArray();
await _localStorage.SetIntArrayAsync(ScheduledTeamsKey, teamIds);
}
public async Task> LoadAbsentStudentsAsync(Student[] allStudents)
{
var studentIds = await _localStorage.GetIntArrayAsync(AbsentStudentsKey);
if (studentIds.Length > 0)
{
return allStudents.Where(s => studentIds.Contains(s.Id)).ToArray();
}
return [];
}
public async Task SaveAbsentStudentsAsync(IEnumerable absentStudents)
{
var studentIds = absentStudents.Select(s => s.Id).ToArray();
await _localStorage.SetIntArrayAsync(AbsentStudentsKey, studentIds);
}
public async Task LoadTimeSlotCountAsync(int defaultValue = 2)
{
var timeSlots = await _localStorage.GetIntAsync(TimeSlotCountKey, defaultValue);
return timeSlots > 0 ? timeSlots : defaultValue;
}
public async Task SaveTimeSlotCountAsync(int timeSlotCount)
{
await _localStorage.SetIntAsync(TimeSlotCountKey, timeSlotCount);
}
public async Task> LoadExtendedTeamsAsync(Team[] allTeams)
{
var teamIds = await _localStorage.GetIntArrayAsync(ExtendedTeamsKey);
if (teamIds.Length > 0)
{
return allTeams.Where(t => teamIds.Contains(t.Id)).ToArray();
}
return [];
}
public async Task SaveExtendedTeamsAsync(IEnumerable extendedTeams)
{
var teamIds = extendedTeams.Select(t => t.Id).ToArray();
await _localStorage.SetIntArrayAsync(ExtendedTeamsKey, teamIds);
}
public async Task> LoadExcludedStudentsAsync()
{
var exclusions = await _localStorage.GetJsonAsync(ExcludedStudentsKey);
if (exclusions != null && exclusions.Length > 0)
{
return exclusions.ToDictionary(
e => (e.TeamId, e.TimeSlotIndex, e.StudentId),
_ => true);
}
return new Dictionary<(int teamId, int timeSlotIndex, int studentId), bool>();
}
public async Task SaveExcludedStudentsAsync(Dictionary<(int teamId, int timeSlotIndex, int studentId), bool> excludedStudents)
{
var exclusions = excludedStudents.Keys
.Where(k => excludedStudents[k])
.Select(k => new ExcludedStudent(k.teamId, k.timeSlotIndex, k.studentId))
.ToArray();
await _localStorage.SetJsonAsync(ExcludedStudentsKey, exclusions);
}
}