using System.Diagnostics;
using System.Net;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;

namespace Berufsschule_HAM.E2ETests.Helper;

public static class GroupsHelper
{
    public static void CreateGroup(ChromeDriver driver, string cn, string displayName, bool canInventorize, bool canManageAssets, bool canManageUsers, bool canManageGroups, bool canManageLocations, bool canManageSettings)
    {
        NavigateToGroupsPage(driver);
        IWebElement createGroupButton = driver.FindElement(By.CssSelector("button[data-bs-target=\"#createGroupModal\"]"));
        createGroupButton.Click();
        AppHelper.AwaitVisible(driver, By.Id("cn"));
        driver.FindElement(By.Id("cn")).SendKeys(cn);
        driver.FindElement(By.Id("displayname")).SendKeys(displayName);
        if (canInventorize) driver.FindElement(By.Id("canInventorize")).Click();
        if (canManageAssets) driver.FindElement(By.Id("canManageAssets")).Click();
        if (canManageUsers) driver.FindElement(By.Id("canManageUsers")).Click();
        if (canManageGroups) driver.FindElement(By.Id("canManageGroups")).Click();
        if (canManageLocations) driver.FindElement(By.Id("canManageLocations")).Click();
        if (canManageSettings) driver.FindElement(By.Id("canManageSettings")).Click();
        IWebElement createButton = driver.FindElement(By.CssSelector("#createGroupForm .btn-primary"));
        createButton.Click();
        Assert.True(AppHelper.TryRetryFindSuccessToast(driver));
    }

    public static void UpdateGroup(ChromeDriver driver, string cn, string newCn, string displayName, bool canInventorize, bool canManageAssets, bool canManageUsers, bool canManageGroups, bool canManageLocations, bool canManageSettings)
    {
        NavigateToGroupsPage(driver);
        IWebElement updateGroupButton = driver.FindElement(By.CssSelector($"button[data-group-id=\"{cn}\"].btn-update"));
        AppHelper.ScrollIntoViewAndClick(driver, updateGroupButton);
        AppHelper.AwaitVisible(driver, By.CssSelector("input#groupId"));
        var cnInput = driver.FindElement(By.CssSelector("input#groupId"));
        cnInput.Click();
        cnInput.Clear();
        cnInput.SendKeys(newCn);

        IWebElement displayNameInput = driver.FindElement(By.Id("dn"));
        displayNameInput.Click();
        displayNameInput.Clear();
        displayNameInput.SendKeys(displayName);
        Dictionary<string, bool> selectors = new()
        {
            ["#updateGroupForm #canInventorize"] = canInventorize,
            ["#updateGroupForm #canManageAssets"] = canManageAssets,
            ["#updateGroupForm #canManageUsers"] = canManageUsers,
            ["#updateGroupForm #canManageGroups"] = canManageGroups,
            ["#updateGroupForm #canManageLocations"] = canManageLocations,
            ["#updateGroupForm #canManageSettings"] = canManageSettings
        };
        selectors.ToList().ForEach(kvp =>
        {
            string name = kvp.Key;
            bool value = kvp.Value;
            IWebElement input = driver.FindElement(By.CssSelector(name));
            if (value != AppHelper.CheckboxGetState(input)) input.Click();
        });
        IWebElement updateButton = driver.FindElement(By.CssSelector("#updateGroupForm .btn-warning"));
        updateButton.Click();
        Assert.True(AppHelper.TryRetryFindSuccessToast(driver));
    }


    public static void DeleteGroup(ChromeDriver driver, string cn)
    {
        NavigateToGroupsPage(driver);
        IWebElement deleteButton = driver.FindElement(By.CssSelector($"button[data-group-id=\"{cn}\"].btn-delete"));
        AppHelper.ScrollIntoViewAndClick(driver, deleteButton);
        AppHelper.AwaitVisible(driver, By.Id("deleteModal"));
        driver.FindElement(By.Id("deleteModal"));
        IWebElement deleteConfirmButton = driver.FindElement(By.CssSelector("#deleteModal .btn-danger"));
        deleteConfirmButton.Click();
        Assert.True(AppHelper.TryRetryFindSuccessToast(driver));
    }
    
    public static void NavigateToGroupsPage(ChromeDriver driver)
    {
        driver.Navigate().GoToUrl(new Uri(AppHelper.ServerUri, "/Home/Groups"));
    }
}