using ESuite.UI.E2E.Helpers;
using ESuite.UI.E2E.Pages;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;

namespace ESuite.UI.E2E.StepDefinitions
{
    [Binding]
    public class PasswordStepDefinitions
    {
        private readonly IWebDriver driver;
        private readonly ScenarioContext _scenarioContext;
        private readonly AutomationTestManagerHelper automationTestManagerHelper;
        private readonly ConfigHelper configHelper;
        private readonly LoginPage loginPage;

        public PasswordStepDefinitions(ScenarioContext scenarioContext)
        {
            driver = WebDriverHelper.GetWebDriver();
            _scenarioContext = scenarioContext;
            automationTestManagerHelper = new(_scenarioContext);
            configHelper = new();
            loginPage = new(driver);
        }

        [When(@"I attempt to change the Password on the My Account page with: (.*)")]
        [When(@"I attempt to create the Password on registration page with: (.*)")]
        public void IChangeThePasswordOnTheMyAccountPage(string password)
        {
            BasicPage.ClickOnDropdownUserMenuItem("Account");
            I.WaitForPageToLoad();
            I.FillField(loginPage.PasswordInput, password);
        }

        [When(@"The password validation should check the following criteria")]
        [Then(@"The password validation should check the following criteria")]
        public void ValidationPassChecking(Table dataTable)
        {
            var rowData = new Dictionary<string, bool>();
            foreach (var row in dataTable.Rows)
            {
                rowData.Add(row["validationText"], bool.Parse(row["passed"]));
            }

            foreach (var row in rowData)
            {
                ValidationFieldPassed(row.Key, row.Value);
            }
        }

        public void ValidationFieldPassed(string validationText, bool validationPassed)
        {
            //IWebElement isVis =driver.FindElement(EditUsersPage.PasswordValidationSuccessString(validationText));
            switch (validationPassed)
            {
                case true:
                    Assert.IsTrue(driver.FindElement(EditUsersPage.PasswordValidationSuccessString(validationText)).Displayed,
                        $"Validation '{validationText}' is not visible.");
                    break;

                case false:
                    Assert.IsTrue(I.WaitTillInvisible(EditUsersPage.PasswordValidationSuccessString(validationText), 1),
                        $"Validation '{validationText}' is visible.");
                    break;

                default:
                    throw new Exception($"Invalid argument for validation field:'{validationPassed}'!");
            }
        }
    }
}
