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

namespace Berufsschule_HAM.E2ETests.Helper;

public static class AssetsHelper
{
    public static void CreateAsset(ChromeDriver driver)
    {
        IWebElement createAssetButton = driver.FindElement(By.CssSelector("button[data-bs-target=\"#createAssetModal\"]"));
        createAssetButton.Click();
        AppHelper.AwaitVisible(driver, By.Id("createName"));
        driver.FindElement(By.Id("createName")).SendKeys(AppHelper.GetRandomName());
        AppHelper.AwaitVisible(driver, By.Id("createLocationSelect-ts-control"));
        driver.FindElement(By.Id("createLocationSelect-ts-control")).Click();
        Thread.Sleep(1000); //AppHelper.Timeout(driver, 30).Until(ExpectedConditions.ElementExists(By.Id("createLocationSelect-opt-1")));
        AppHelper.ScrollIntoViewAndClick(driver, driver.FindElement(By.Id("createLocationSelect-opt-1")));
        //driver.ExecuteJavaScript("document.getElementById(\"createLocationSelect-ts-control\").value=\"somewhere\"");
        driver.ExecuteJavaScript("document.getElementById(\"createLocationSelect-opt-1\").click()");
        driver.FindElement(By.Id("createUsersSelect-ts-control")).Click();
        AppHelper.AwaitVisible(driver, By.Id("createUsersSelect-opt-1"));
        driver.ExecuteJavaScript("document.getElementById(\"createUsersSelect-opt-1\").click()");
        driver.FindElement(By.Id("createSerialNumber")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("createType")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("createMake")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("createModel")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("addAttributeBtn")).Click();
        driver.FindElement(By.CssSelector("#attributesContainer input[data-attr-name]")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.CssSelector("#attributesContainer input[data-attr-value]")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("createPurchaseDate")).Click();
        driver.FindElement(By.Id("createPurchaseDate")).SendKeys("01011990");
        driver.FindElement(By.Id("createPurchaseValue")).SendKeys("12345.67");
        driver.FindElement(By.Id("createPurchaseAt")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("createPurchaseBy")).SendKeys("admin");
        IWebElement createButton = driver.FindElement(By.CssSelector("#createAssetModal .modal-footer .btn-primary"));
        createButton.Click();
        Assert.True(AppHelper.TryRetryFindSuccessToast(driver));
    }

    public static void UpdateAsset(ChromeDriver driver, IWebElement row)
    {
        int id = Convert.ToInt32(row.GetAttribute("data-asset-id"));
        IWebElement updateAssetButton = row.FindElement(By.CssSelector($"button[data-bs-target=\"#updateAssetModal\"]"));
        driver.ExecuteJavaScript($"$('button[data-asset-id=\"{id}\"][data-bs-target=\"#updateAssetModal\"]').click()");
        
        AppHelper.AwaitVisible(driver, By.Id("updateName"));
        driver.FindElement(By.Id("updateName")).SendKeys(AppHelper.GetRandomName());
        driver.ExecuteJavaScript("$(\"#updateLocationSelect-ts-control\").click()");
        AppHelper.AwaitVisible(driver, By.Id("updateLocationSelect-opt-1"));
        driver.ExecuteJavaScript("document.getElementById(\"updateLocationSelect-opt-1\").click()");
        driver.ExecuteJavaScript("$(\"#updateUsersSelect-ts-control\").click()");
        AppHelper.AwaitVisible(driver, By.Id("updateUsersSelect-opt-1"));
        driver.ExecuteJavaScript("document.getElementById(\"updateUsersSelect-opt-1\").click()");
        driver.FindElement(By.Id("updateSerialNumber")).SendKeys(AppHelper.GetRandomName());

        driver.FindElement(By.Id("updateType")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("updateMake")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("updateModel")).SendKeys(AppHelper.GetRandomName());
        driver.ExecuteJavaScript("document.getElementById(\"addAttributeBtn\").click()");

        AppHelper.AwaitVisible(driver, By.CssSelector("#updateAttributesContainer input[data-attr-name]"));
        driver.FindElement(By.CssSelector("#updateAttributesContainer input[data-attr-name]")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.CssSelector("#updateAttributesContainer input[data-attr-value]")).SendKeys(AppHelper.GetRandomName());

        driver.FindElement(By.Id("updatePurchaseDate")).Click();
        driver.FindElement(By.Id("updatePurchaseDate")).SendKeys("01011990");

        driver.FindElement(By.Id("updatePurchaseValue")).SendKeys("12345.67");
        driver.FindElement(By.Id("updatePurchaseAt")).SendKeys(AppHelper.GetRandomName());
        driver.FindElement(By.Id("updatePurchaseBy")).SendKeys("admin");


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


    public static void DeleteAsset(ChromeDriver driver, IWebElement row)
    {
        int id = Convert.ToInt32(row.GetAttribute("data-asset-id"));
        driver.ExecuteJavaScript($"$('button[data-asset-id=\"{id}\"][data-bs-target=\"#deleteModal\"]').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 NavigateToAssetsPage(ChromeDriver driver)
    {
        driver.Navigate().GoToUrl(new Uri(AppHelper.ServerUri, "/Home/Assets"));
    }
}