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

namespace Berufsschule_HAM.E2ETests;

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

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

    [RetryFact(5)]
    public void GroupsPage_ShouldShowButton()
    {
        AppHelper.Login(_driver);
        GroupsHelper.NavigateToGroupsPage(_driver);
        _driver.FindElement(By.CssSelector("button[data-bs-target=\"#createGroupModal\"]"));
    }

    [RetryFact(5)]
    public void GroupsPage_ShouldShowCreateViewModal()
    {
        AppHelper.Login(_driver);
        GroupsHelper.NavigateToGroupsPage(_driver);
        List<string> ids = ["cn", "displayname", "canInventorize", "canManageAssets", "canManageUsers", "canManageGroups", "canManageLocations", "canManageSettings"];
        List<string> css = ["#createGroupForm .btn-primary", "#createGroupForm .btn-secondary"];
        ids.ForEach(id => _driver.FindElement(By.Id(id)));
        css.ForEach(selector => _driver.FindElement(By.CssSelector(selector)));
    }

    [RetryFact(5)]
    public void GroupsPage_ShouldCreateAndUpdateAndDeleteGroup()
    {
        string randomName = $"RESERVED_TEST_{AppHelper.GetRandomName()}";
        string randomName2 = $"RESERVED_TEST_{AppHelper.GetRandomName()}";
        AppHelper.Login(_driver);
        GroupsHelper.NavigateToGroupsPage(_driver);
        GroupsHelper.CreateGroup(_driver, randomName, "TEST GROUP", true, true, true, true, true, true);
        GroupsHelper.UpdateGroup(_driver, randomName, randomName2, "TEST GROUP", false, false, false, false, false, false);
        GroupsHelper.UpdateGroup(_driver, randomName2, randomName, "TEST GROUP", true, true, true, true, true, true);
        GroupsHelper.DeleteGroup(_driver, randomName);
    }
    public void Dispose()
    {
        _driver.Quit();
        serverProcess.Kill();
    }
}
