using Berufsschule_HAM.E2ETests.Helper;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Diagnostics;
using xRetry;

namespace Berufsschule_HAM.E2ETests;

public class LocationsPageTests : IDisposable
{
    private readonly ChromeDriver _driver;
    public Uri serverUri;
    public string serverUrl;
    public Process serverProcess;

    public LocationsPageTests()
    {
        serverUrl = AppHelper.ServerUrl;
        serverUri = AppHelper.ServerUri;
        Task<Process> app = AppHelper.StartApp(serverUrl);
        _driver = AppHelper.GetChromeDriver();
        serverProcess = app.Result;
    }

    [RetryFact(5)]
    public void LocationsPage_ShouldShowButton()
    {
        AppHelper.Login(_driver);
        LocationsHelper.NavigateToLocationsPage(_driver);
        _driver.FindElement(By.CssSelector("button[data-bs-target=\"#createModal\"]"));
    }

    [RetryFact(5)]
    public void LocationsPage_ShouldShowCreateViewModal()
    {
        AppHelper.Login(_driver);
        LocationsHelper.NavigateToLocationsPage(_driver);
        List<string> ids = ["createLocationName", "createRoomNumber", "createSeat"];
        List<string> css = ["#createModal .btn-primary", "#createModal .btn-secondary"];
        ids.ForEach(id => _driver.FindElement(By.Id(id)));
        css.ForEach(selector => _driver.FindElement(By.CssSelector(selector)));
    }

    [RetryFact(5)]
    public void LocationsPage_ShouldCreateAndUpdateAndDeleteGroup()
    {
        string randomName = $"RESERVED_LOCATION_{AppHelper.GetRandomName()}";
        string randomName2 = $"RESERVED_LOCATION_{AppHelper.GetRandomName()}";
        AppHelper.Login(_driver);
        LocationsHelper.NavigateToLocationsPage(_driver);
        LocationsHelper.CreateLocation(_driver, randomName, "RESERVED_ROOM", "RESERVED_ROOMNUMBER");
        LocationsHelper.UpdateLocation(_driver, randomName, "RESERVED_ROOM", "RESERVED_ROOMNUMBER", randomName2, "RESERVED_ROOM2", "RESERVED_ROOMNUMBER2");
        LocationsHelper.UpdateLocation(_driver, randomName2, "RESERVED_ROOM2", "RESERVED_ROOMNUMBER2", randomName, "RESERVED_ROOM", "RESERVED_ROOMNUMBER");
        LocationsHelper.DeleteLocation(_driver, $"{randomName.ToLower().Replace("_", "")}-reservedroom-reservedroomnumber");
    }
    public void Dispose()
    {
        _driver.Quit();
        serverProcess.Kill();
    }
}
