using ESuite.UI.E2E.Helpers;
using OpenQA.Selenium;

namespace ESuite.UI.E2E.Pages
{
    public class BasicPage
    {
        private readonly IWebDriver driver;

        public BasicPage(IWebDriver driver)
        {
            this.driver = driver;
        }

        public IWebDriver Driver => driver;

        // Page elements
        public static By NameField => By.CssSelector("input#name");
        public static By SSOProvider => By.Id("ssoProviderId");
        public static By AddressField => By.CssSelector("input#address");
        public static By AddButton => By.XPath("//*[contains(@class,'btn-primary')][text()='Add']");
        public static By SaveAndCloseButton => By.CssSelector("button.btn-primary[name='Save and close']:not([disabled])");
        public static By SaveButton => By.CssSelector("button.btn-primary[name='Submit']:not([disabled])");
        public static By ObjectNameInTable(string? name) => By.XPath($"//table[@class='table table-sm table-striped table-bordered']//*[contains(text(),'{name}')]");
        public static By SearchInput => By.CssSelector("th input#name");
        public static By SearchUsersInput => By.CssSelector("th input#displayName");
        public static By DropdownTab(string title) => By.XPath($"//div[@class='leftMenuItemLabel'][text()='{title}']");
        public static By DropdownOpened(string title) => By.XPath($"//div[@class='leftMenuItemLabel'][text()='{title}']/parent::div[contains(@class,'leftMenuSubMenuOpen')]/ancestor::div[@class='frame-leftMenu']//div[@class='subbar']");
        public static By OpenedDropdown => By.XPath("//div[@class='dropdown-menu show']");
        public static By SelectorByLabel(string? label) => By.XPath($"//label[text()='{label}']/following-sibling::select");
        public static By AlertMessageEmptyFieldByName(string name) => By.XPath($"//div[@class='alert alert-danger'][text()='{name} is not allowed to be empty']");
        public static By LoadingMessage => By.XPath("//div[text()='Loading']");
        public static By AuditObjectButton(string objName) => By.XPath($"//td[text()='{objName}']/following-sibling::td//*[contains(@class, 'fa-book')]");
        public static By EditObjectButton(string objName) => By.XPath($"//td[text()='{objName}']/following-sibling::td//*[contains(@class, 'fa-pen-to-square')]");
        public static By DeleteObjectButton(string objName) => By.XPath($"//td[text()='{objName}']/following-sibling::td//*[contains(@class, 'fa-trash')]");
        public static By DeleteOrganisationButton(string objName) => By.XPath($"//td/a[text()='{objName}']//ancestor::tr/td//*[contains(@class, 'fa-trash')]");
        public static By EditOrganisationButton(string objName) => By.XPath($"//td/a[text()='{objName}']//ancestor::tr/td//*[contains(@class, 'fa-pen-to-square')]");
        public static By EPrintInstanceButton(string objName) => By.XPath($"//td/a[text()='{objName}']");
        public static By OrganisationButton(string objName) => By.XPath($"//td/a[text()='{objName}']");
        public static By ConfirmDeletionButton => By.XPath("//button[text()='Press again to delete']");
        public static By AuditMenuButton => By.XPath("//a[text()='Audit']");
        public static By UserDropdownMenu => By.XPath("//div[contains(@class,'dropdown')]/a[text()='User']");
        public static By UserDropdownMenuItem(string item) => By.XPath($"//div[@class='dropdown-menu show dropdown-menu-end']/a[contains(text(),'{item}')]");
        public static By ToastWithMessage(string text) => By.XPath($"//div[@class='Toastify']//div[text()='{text}']");
        public static By LoadingDropdownMessage => By.XPath("//option[@value='loading...']");

        // Page actions

        public static void ClickOnDropdownMenuItem(string menuItemName, string menuTitle = "Admin")
        {
            I.Click(DropdownTab(menuTitle));
            I.WaitForElementVisibleAndClickable(DropdownOpened(menuTitle));
            I.Click(By.XPath($"//div[@class='subbar']/a[text()='{menuItemName}']"));
            I.WaitTillInvisible(DropdownOpened(menuTitle));
        }

        public static void SelectTab(string tabName)
        {
            By locator = By.XPath($"//ul[@class='tab-list']/li[contains(@class, 'tab-list-item')][text()='{tabName}']");
            I.WaitForElementVisibleAndClickable(locator);
            I.Click(locator);
        }

        // CK-Editor 
        public static By CKEditorTextBox => By.XPath("//div[contains(@class,'ck-editor__editable')][@role='textbox']");
        public void FillCKEditorTextbox(string text)
        {
            IWebElement textbox = driver.FindElement(CKEditorTextBox);
            textbox.SendKeys(Keys.Control + "a");
            textbox.SendKeys(Keys.Delete);
            I.FillField(CKEditorTextBox, text);
        }
        public static void SearchDeletedObjectNameInTableViaSearchInputField(string name)
        {
            I.FillField(SearchInput, name);
            I.WaitTillInvisible(ObjectNameInTable(name));
        }

        public static void ClickOnDropdownUserMenuItem(string item)
        {
            I.Click(UserDropdownMenu);
            I.Click(UserDropdownMenuItem(item));
            I.WaitTillInvisible(UserDropdownMenuItem(item));
        }
        public static void SearchObjectNameInTableViaSearchInputField(string text, By? locator = null)
        {
            locator ??= SearchInput; // Use SearchInput if no locator is provided
            I.Click(locator);
            I.FillField(locator, text);
            I.WaitForVisible(ObjectNameInTable(text));
        }
    }
}