using Microsoft.Playwright.NUnit;
using NUnit.Framework;

namespace Tests.Specs;

[Parallelizable(ParallelScope.Self)]
[FixtureLifeCycle(LifeCycle.SingleInstance)]
public class SearchFeaturesTests : PageTest
{
    private Website _website = null!;

    [SetUp]
    public void CreateWebsite()
    {
        _website = new Website(Page);
    }

    [Test]
    public async Task DesktopOpenCloseSearchDialog()
    {
        await _website.GotoAsync();
        await _website.NavigationBar.ClickSearchButtonAsync();
        await _website.SearchDialog.CloseDialogAsync();
        await _website.NavigationBar.ClickHomeLinkAsync();
    }

    [Test]
    public async Task DesktopSearchForThrone()
    {
        await _website.GotoAsync();
        await _website.NavigationBar.ClickSearchButtonAsync();
        await _website.SearchDialog.SearchAsync("Throne");
        await _website.SearchDialog.SelectSearchEntityAsync("Throne");

        var name = await _website.DatabaseSinglePage.GetEntityNameAsync();
        var health = await _website.DatabaseSinglePage.GetEntityHealthAsync();

        Assert.That(name, Is.EqualTo("Throne"));
        Assert.That(health.Trim(), Is.Not.Empty);
    }

    [Test]
    public async Task DesktopFilterForThrone()
    {
        var page = _website.DatabasePage;
        await page.GotoAsync();
        await page.FilterNameAsync("Throne");
        var name = await page.GetEntityNameByIndexAsync(0);
        Assert.That(name, Is.EqualTo("Throne"));
    }

    [Test]
    public async Task SeeThroneByDefault()
    {
        var page = _website.DatabasePage;
        await page.GotoAsync();
        var name = await page.GetEntityNameAsync("army", "throne");
        Assert.That(name, Is.EqualTo("Throne"));
    }

    [Test]
    public async Task DirectLinkNotThroneFailure()
    {
        var page = _website.DatabaseSinglePage;
        await page.GotoWithSearchAsync("not throne");
        var invalidSearch = await page.GetInvalidSearchAsync();
        var validSearch = await page.GetValidSearchAsync();
        Assert.That(invalidSearch, Is.EqualTo("not throne"));
        Assert.That(validSearch, Is.EqualTo("Throne"));
    }
}