using NexusReader.UI.Shared.Models;

namespace NexusReader.UI.Shared.Services;

/// <summary>
/// Thread-safe implementation of IReaderStateService.
/// Thread safety is ensured via lock-guarded property getters/setters.
/// UI updates originating from the JS event loop (via JSInvokable) are synchronized at Blazor's InvokeAsync(StateHasChanged) render boundary.
/// </summary>
public sealed class ReaderStateService : IReaderStateService
{
    private readonly object _lock = new();
    private int _scrollPercent;
    private List<string> _checkpoints = new();
    private string _blockId = string.Empty;
    private MobileReaderTab _activeTab = MobileReaderTab.Reader;

    public int CurrentScrollPercentage
    {
        get { lock (_lock) return _scrollPercent; }
        set { lock (_lock) _scrollPercent = value; }
    }

    public List<string> CurrentCheckpoints
    {
        get { lock (_lock) return _checkpoints; }
        set { lock (_lock) _checkpoints = value ?? new(); }
    }

    public string CurrentBlockId
    {
        get { lock (_lock) return _blockId; }
        set { lock (_lock) _blockId = value ?? string.Empty; }
    }

    public MobileReaderTab ActiveTab
    {
        get { lock (_lock) return _activeTab; }
        set { lock (_lock) _activeTab = value; }
    }
}
