using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.Extensions;
using SeleniumExtras.WaitHelpers;

namespace Berufsschule_HAM.E2ETests.Helper;

public static class UsersHelper
{
    public static void CreateUser(ChromeDriver driver)
    {
        IWebElement createUserButton = driver.FindElement(By.CssSelector("button[data-bs-target=\"#createModal\"]"));
        createUserButton.Click();
        AppHelper.AwaitVisible(driver, By.Id("createTitle"));
        driver.FindElement(By.Id("createTitle")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("createName")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("createSurname")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("createBirthDate")).SendKeys("01.01.1900");
        driver.FindElement(By.Id("createCity")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("createStreet")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("createStreetNr")).SendKeys("42");

        driver.FindElement(By.Id("createWorkplace")).Click();
        AppHelper.AwaitVisible(driver, By.Id("createWorkplace"));
        AppHelper.Timeout(driver, 30).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("#createWorkplace > :nth-child(2)")));
        driver.ExecuteJavaScript("document.getElementById(\"createWorkplace\").children[1].selected = true");
        driver.FindElement(By.Id("createGroups")).FindElement(By.CssSelector("*")).Click();

        driver.FindElement(By.Id("createPassword")).SendKeys(AppHelper.GetRandomName() + "+42A");
        IWebElement createButton = driver.FindElement(By.CssSelector("#createModal .modal-footer .btn-primary"));
        createButton.Click();
        Assert.True(AppHelper.TryRetryFindSuccessToast(driver));
    }

    public static void UpdateUser(ChromeDriver driver)
    {
        driver.ExecuteJavaScript($"$('tr.user-row:last-of-type .btn-warning').click()");
        
        AppHelper.AwaitVisible(driver, By.Id("updateTitle"));
        driver.FindElement(By.Id("updateTitle")).Clear();
        driver.FindElement(By.Id("updateTitle")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("updateName")).Clear();
        driver.FindElement(By.Id("updateName")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("updateSurname")).Clear();
        driver.FindElement(By.Id("updateSurname")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("updateBirthdate")).Clear();
        driver.FindElement(By.Id("updateBirthdate")).SendKeys("01.01.1900");
        driver.FindElement(By.Id("updateAddressCity")).Clear();
        driver.FindElement(By.Id("updateAddressCity")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("updateAddressStreet")).Clear();
        driver.FindElement(By.Id("updateAddressStreet")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("updateAddressStreetNr")).Clear();
        driver.FindElement(By.Id("updateAddressStreetNr")).SendKeys("42");

        driver.FindElement(By.Id("updateWorkplace")).Click();
        driver.ExecuteJavaScript("document.getElementById(\"updateWorkplace\").children[1].selected = true");
        driver.FindElement(By.Id("updateGroups")).FindElement(By.CssSelector("*")).Click();

        driver.FindElement(By.Id("updatePassword")).SendKeys(AppHelper.GetRandomName() + "+42A");

        IWebElement updateButton = driver.FindElement(By.CssSelector("#updateModal .btn-warning"));
        updateButton.Click();
        Assert.True(AppHelper.TryRetryFindSuccessToast(driver));
    }


    public static void DeleteUser(ChromeDriver driver)
    {
        driver.ExecuteJavaScript($"$('tr.user-row:last-of-type .btn-danger').click()");
        AppHelper.AwaitVisible(driver, By.Id("deleteModal"));
        driver.FindElement(By.Id("deleteModal"));
        driver.ExecuteJavaScript($"$('#deleteModal .btn-danger').click()");
        Assert.True(AppHelper.TryRetryFindSuccessToast(driver));
    }
    
    public static void NavigateToUsersPage(ChromeDriver driver)
    {
        driver.Navigate().GoToUrl(new Uri(AppHelper.ServerUri, "/Home/Users"));
    }
}