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

namespace Berufsschule_HAM.E2ETests;

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

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

    [RetryFact(5)]
    public void InventoryPage_ShouldShowButtons()
    {
        AppHelper.Login(_driver);
        _driver.Navigate().GoToUrl(new Uri(serverUri, "/Home/Inventory"));
        _driver.FindElement(By.CssSelector("a[href=\"/Home/Assets?CreateModal=true\"]"));
        _driver.FindElement(By.Id("barcodeInput"));
        _driver.FindElement(By.Id("enterAssetIdManuallyButton"));
        _driver.FindElement(By.Id("scanBarcodeButton"));
    }

    [RetryFact(5)]
    public void InventoryPage_ShouldShowCompleteViewModal()
    {
        AppHelper.Login(_driver);
        _driver.Navigate().GoToUrl(new Uri(serverUri, "/Home/Inventory"));
        IWebElement assetIdInputField = _driver.FindElement(By.Id("barcodeInput"));
        assetIdInputField.SendKeys("1");
        IWebElement scanBarcode = _driver.FindElement(By.Id("enterAssetIdManuallyButton"));
        scanBarcode.Click();
        AppHelper.AwaitVisible(_driver, By.Id("viewAssetModal"));
        IWebElement viewModal = _driver.FindElement(By.Id("viewAssetModal"));
        Assert.True(viewModal.Displayed);
        Assert.True(_driver.FindElement(By.CssSelector("svg[class=\"form-control\"]")).Displayed);

        Assert.NotEmpty(_driver.FindElement(By.Id("detailName")).GetAttribute("value") ?? "");
        Assert.NotEmpty(_driver.FindElement(By.Id("detailLocation")).GetAttribute("value") ?? "");
        Assert.NotEmpty(_driver.FindElement(By.Id("detailOwner")).GetAttribute("value") ?? "");
        Assert.NotEmpty(_driver.FindElement(By.Id("detailSerialNumber")).GetAttribute("value") ?? "");

        Assert.NotEmpty(_driver.FindElement(By.Id("detailType")).GetAttribute("value") ?? "");
        Assert.NotEmpty(_driver.FindElement(By.Id("detailMake")).GetAttribute("value") ?? "");
        Assert.NotEmpty(_driver.FindElement(By.Id("detailModel")).GetAttribute("value") ?? "");

        Assert.NotEmpty(_driver.FindElement(By.Id("detailPurchaseDate")).GetAttribute("value") ?? "");
        Assert.NotEmpty(_driver.FindElement(By.Id("detailPurchaseValue")).GetAttribute("value") ?? "");
        Assert.NotEmpty(_driver.FindElement(By.Id("detailPurchaseAt")).GetAttribute("value") ?? "");
        Assert.NotEmpty(_driver.FindElement(By.Id("detailPurchaseBy")).GetAttribute("value") ?? "");
    }

    [RetryFact(5)]
    public void InventoryPage_ShouldShowSuccessToastOnInformationCorrect()
    {
        AppHelper.Login(_driver);
        _driver.Navigate().GoToUrl(new Uri(serverUri, "/Home/Inventory"));
        IWebElement assetIdInputField = _driver.FindElement(By.Id("barcodeInput"));
        assetIdInputField.SendKeys("1");
        IWebElement scanBarcode = _driver.FindElement(By.Id("enterAssetIdManuallyButton"));
        scanBarcode.Click();
        AppHelper.AwaitVisible(_driver, By.Id("viewAssetModal"));
        IWebElement viewModal = _driver.FindElement(By.Id("viewAssetModal"));
        Assert.True(viewModal.Displayed);
        IWebElement okButton = _driver.FindElement(By.CssSelector("#viewAssetModal button.btn.btn-primary[data-bs-dismiss=\"modal\"]"));
        okButton.Click();
        Assert.True(AppHelper.TryRetryFindSuccessToast(_driver));
    }

    [RetryFact(5)]
    public void InventoryPage_ShouldShowSuccessToastOnInformationUpdate()
    {
        AppHelper.Login(_driver);
        _driver.Navigate().GoToUrl(new Uri(serverUri, "/Home/Inventory"));
        IWebElement assetIdInputField = _driver.FindElement(By.Id("barcodeInput"));
        assetIdInputField.SendKeys("1");
        IWebElement scanBarcode = _driver.FindElement(By.Id("enterAssetIdManuallyButton"));
        scanBarcode.Click();
        AppHelper.AwaitVisible(_driver, By.Id("viewAssetModal"));
        IWebElement viewModal = _driver.FindElement(By.Id("viewAssetModal"));
        Assert.True(viewModal.Displayed);
        IWebElement updateButton = _driver.FindElement(By.CssSelector("#viewAssetModal button.btn.btn-warning[data-bs-dismiss=\"modal\"]"));
        updateButton.Click();
        AppHelper.AwaitVisible(_driver, By.CssSelector("#updateAssetModal button.btn.btn-warning[type=\"submit\"]"));
        IWebElement okButton = _driver.FindElement(By.CssSelector("#updateAssetModal button.btn.btn-warning[type=\"submit\"]"));
        okButton.Click();
        Assert.True(AppHelper.TryRetryFindSuccessToast(_driver));
    }

    [RetryFact(5)]
    public void InventoryPage_ShouldLeadToAssetPage()
    {
        AppHelper.Login(_driver);
        _driver.Navigate().GoToUrl(new Uri(serverUri, "/Home/Inventory"));
        IWebElement link = _driver.FindElement(By.CssSelector("a[href=\"/Home/Assets?CreateModal=true\"]"));
        link.Click();
        AppHelper.Timeout(_driver).Until(ExpectedConditions.UrlContains("/Home/Assets"));
        Assert.Contains("/Home/Assets", _driver.Url);
    }

    [RetryFact(5)]
    public void InventoryPage_ShouldShowPrintModal()
    {
        AppHelper.Login(_driver);
        _driver.Navigate().GoToUrl(new Uri(serverUri, "/Home/Inventory"));
        IWebElement openPrintModalButton = _driver.FindElement(By.Id("openPrintModal"));
        openPrintModalButton.Click();
        AppHelper.AwaitVisible(_driver, By.Id("printModal"));
        IWebElement printModal = _driver.FindElement(By.Id("printModal"));
        Assert.True(printModal.Displayed);
    }

    public void Dispose()
    {
        _driver.Quit();
        serverProcess.Kill();
    }
}
