using System.Text.RegularExpressions;

namespace Tests.Pages.BuildCalculator;

public class EntityClickViewComponent
{
    private readonly Website _website;

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

    public ILocator EntityClickView => _website.Locator(".entityClickView");

    public async Task<string?> GetEntityNameAsync()
    {
        var el = EntityClickView.Locator("#entityName");
        if (await el.CountAsync() == 0) return null;
        return (await el.TextContentAsync())?.Trim();
    }

    public async Task<string?> GetEntityHealthAsync()
    {
        var healthText = EntityClickView.Locator("div").Filter(new LocatorFilterOptions
            { HasTextRegex = new Regex("Health", RegexOptions.IgnoreCase) }).First;
        if (await healthText.CountAsync() == 0) return null;
        var text = await healthText.TextContentAsync() ?? "";
        var match = Regex.Match(text, @"(\d+)");
        return match.Success ? match.Groups[1].Value : null;
    }

    public async Task ClickDetailedViewAsync()
    {
        await EntityClickView.Locator("button").Filter(new LocatorFilterOptions { HasText = "Detailed" }).ClickAsync();
    }

    public async Task ClickPlainViewAsync()
    {
        await EntityClickView.Locator("button").Filter(new LocatorFilterOptions { HasText = "Plain" }).ClickAsync();
    }
}