namespace Tests.Pages;

public class HarassCalculatorPage : BasePage
{
    public HarassCalculatorPage(Website website) : base(website)
    {
    }

    public override string Url => "harass-calculator";

    public async Task SetWorkersLostToHarassAsync(int number)
    {
        await EnterAndPressAsync("numberOfWorkersLostToHarass", number);
    }

    public async Task SetNumberOfTownHallsExistingAsync(int number)
    {
        await EnterAndPressAsync("numberOfTownHallsExisting", number);
    }

    public async Task SetTownHallTravelTimeAsync(int index, int seconds)
    {
        await EnterInputAtIndexAsync("numberOfTownHallTravelTimes", index, seconds);
    }

    public async Task<int> GetTotalAlloyHarassmentAsync()
    {
        return await ReadIntAsync("totalAlloyHarassment");
    }

    public async Task<int> GetWorkerReplacementCostAsync()
    {
        return await ReadIntAsync("workerReplacementCost");
    }

    public async Task<int> GetDelayedMiningCostAsync()
    {
        return await ReadIntAsync("delayedMiningCost");
    }

    public async Task<int> GetAverageTravelTimeAsync()
    {
        return await ReadIntAsync("getAverageTravelTime");
    }

    public async Task<int> GetExampleTotalAlloyLossAsync()
    {
        return await ReadIntAsync("exampleTotalAlloyLoss");
    }

    public async Task<int> GetExampleWorkerCostAsync()
    {
        return await ReadIntAsync("exampleWorkerCost");
    }

    public async Task<int> GetExampleMiningTimeCostAsync()
    {
        return await ReadIntAsync("exampleMiningTimeCost");
    }

    public async Task<int> GetExampleTotalAlloyLossAccurateAsync()
    {
        return await ReadIntAsync("exampleTotalAlloyLossAccurate");
    }

    public async Task<int> GetExampleTotalAlloyLossDifferenceAsync()
    {
        return await ReadIntAsync("exampleTotalAlloyLossDifference");
    }

    public async Task<int> GetExampleTotalAlloyLossAccurateDifferenceAsync()
    {
        return await ReadIntAsync("exampleTotalAlloyLossAccurateDifference");
    }

    private async Task EnterAndPressAsync(string id, int value)
    {
        var locator = Website.FindById(id);
        await Website.EnterInputAsync(locator, value.ToString());
    }

    private async Task EnterInputAtIndexAsync(string parentId, int index, int value)
    {
        var inputs = Website.FindById(parentId).Locator("input");
        var input = inputs.Nth(index);
        await input.FillAsync(value.ToString());
        await input.PressAsync("Enter");
    }

    private async Task<int> ReadIntAsync(string id)
    {
        var text = await Website.FindById(id).TextContentAsync() ?? "";
        return int.Parse(text.Trim());
    }
}