using System.Text.RegularExpressions;

namespace Tests.Pages.BuildCalculator;

public class HighlightsComponent
{
    private readonly Website _website;

    public HighlightsComponent(Website website)
    {
        _website = website;
    }

    public ILocator HighlightsContainer => _website.Locator(".highlightsContainer");

    public ILocator RequestedColumn => HighlightsContainer.Locator("div")
        .Filter(new LocatorFilterOptions { HasText = "Requested" }).Locator("+ div");

    public ILocator FinishedColumn => HighlightsContainer.Locator("div")
        .Filter(new LocatorFilterOptions { HasText = "Finished" }).Locator("+ div");

    public async Task<IReadOnlyList<string>> GetRequestedItemsAsync()
    {
        return await GetHighlightItemsAsync();
    }

    public async Task<IReadOnlyList<string>> GetFinishedItemsAsync()
    {
        return await GetHighlightItemsAsync();
    }

    private async Task<IReadOnlyList<string>> GetHighlightItemsAsync()
    {
        var items = await _website.Locator(".highlightsContainer").Locator("div")
            .Filter(new LocatorFilterOptions { HasTextRegex = new Regex(@"^\d+\s*\|") }).AllAsync();
        var result = new List<string>();
        foreach (var item in items)
        {
            var text = (await item.TextContentAsync())?.Trim();
            if (text is not null) result.Add(text);
        }

        return result;
    }
}