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

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

        // Constructor to initialize the driver
        public AddFormTemplatePage(IWebDriver driver)
        {
            this.driver = driver;
        }

        // Page elements
        public static By InsertFieldButton => By.XPath("//button/span[text()='Insert Field']");
        public static By CustomFieldDropdownItem(string? name) => By.XPath($"//span[contains(@class,'ck-button__label')][text()='{name}']");
        public static By DefinitionTextBox => By.XPath("//div[contains(@class,'ck-editor__editable')][@role='textbox']");
        public static By InsertTableButton => By.XPath("//button[@data-cke-tooltip-text='Insert table']");
        public static By InsertTableDropdownGrid => By.ClassName("ck-insert-table-dropdown__grid");
        public static By CellTextbox(int row, int column) => By.XPath($"//div[@role='textbox']//figure/table/tbody/tr[{row}]/td[{column}]/span");
        public static By FormTemplateVersionByName(string name) => By.XPath($"//td[text()='{name}']/following-sibling::td");

        // Page actions
        public static void CreateTableViaGrid(int row, int column)
        {
            By coordinates = By.XPath($"//button[@data-row='{row}'][@data-column='{column}']");

            I.Click(InsertTableButton);
            I.WaitForVisible(InsertTableDropdownGrid);
            I.Click(coordinates);

        }

        public static void ChooseCustomField(string? customField, int row, int column)
        {
            I.Click(CellTextbox(row, column));
            I.Click(InsertFieldButton);
            I.Click(CustomFieldDropdownItem(customField));
        }

        public static void FillFieldTextInForm(string text, int row, int column)
        {
            I.Click(CellTextbox(row, column));
            I.FillField(CellTextbox(row, column), text);
        }

        public static int GetFormVersionTemplateByName(string name)
        {
            var version = I.GetTextFromElement(FormTemplateVersionByName(name));
            return int.Parse(version);
        }
    }
}