using NexusReader.Application.Queries.Quiz;

namespace NexusReader.UI.Shared.Services;

public sealed class QuizStateService : IQuizStateService
{
    public string? CurrentQuizBlockId { get; private set; }
    public QuizDto? CurrentQuiz { get; private set; }
    public bool IsHydrating { get; private set; }
    public bool HasNewQuiz { get; private set; }

    public event Func<string, Task>? OnQuizRequested;
    public event Func<Task>? OnQuizUpdated;

    public async Task RequestQuiz(string blockId)
    {
        CurrentQuizBlockId = blockId;
        if (OnQuizRequested != null) await OnQuizRequested(blockId);
    }

    public async Task SetQuiz(string? blockId, QuizDto? quiz)
    {
        CurrentQuizBlockId = blockId;
        CurrentQuiz = quiz;
        IsHydrating = false;
        HasNewQuiz = quiz != null;
        if (OnQuizUpdated != null) await OnQuizUpdated();
    }

    public async Task SetHydrating(bool hydrating)
    {
        IsHydrating = hydrating;
        if (OnQuizUpdated != null) await OnQuizUpdated();
    }

    public async Task MarkQuizAsSeen()
    {
        if (!HasNewQuiz) return;
        HasNewQuiz = false;
        if (OnQuizUpdated != null) await OnQuizUpdated();
    }
}
