using Tests.Pages;
using Tests.Shared;

namespace Tests.Helpers;

public class Website
{
    public Website(IPage page)
    {
        Page = page;
        RunAgainstProduction = Environment.GetEnvironmentVariable("RUN_AGAINST_PRODUCTION") == "true";

        BaseUrl = RunAgainstProduction ? "https://igpfanreference.ca" : LocalServer.BaseUrl ?? "http://localhost:5234";

        NavigationBar = new NavigationBar(this);
        SearchDialog = new SearchDialog(this);
        BuildCalculatorPage = new BuildCalculatorPage(this);
        HarassCalculatorPage = new HarassCalculatorPage(this);
        DatabasePage = new DatabasePage(this);
        DatabaseSinglePage = new DatabaseSinglePage(this);
    }

    public IPage Page { get; }
    public bool RunAgainstProduction { get; }
    public string BaseUrl { get; }

    public NavigationBar NavigationBar { get; }
    public SearchDialog SearchDialog { get; }
    public BuildCalculatorPage BuildCalculatorPage { get; }
    public HarassCalculatorPage HarassCalculatorPage { get; }
    public DatabasePage DatabasePage { get; }
    public DatabaseSinglePage DatabaseSinglePage { get; }

    public ILocator Locator(string selector)
    {
        return Page.Locator(selector);
    }

    public ILocator FindById(string id)
    {
        return Page.Locator($"#{id}");
    }

    public async Task GotoAsync(string? path = null)
    {
        var url = path is null ? BaseUrl : $"{BaseUrl}/{path}";
        await Page.GotoAsync(url);
    }

    public async Task ClickElementAsync(ILocator locator)
    {
        await locator.ClickAsync();
    }

    public async Task EnterInputAsync(ILocator locator, string value)
    {
        await locator.FillAsync(value);
        await locator.PressAsync("Enter");
    }
}