package com.sdl.selenium.extjs6.button;

import com.sdl.selenium.web.SearchType;
import com.sdl.selenium.web.WebLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.List;

public class SplitButton extends Button {
    private static final Logger LOGGER = LoggerFactory.getLogger(SplitButton.class);

    public SplitButton() {
        withClassName("SplitButton");
    }

    public SplitButton(WebLocator container) {
        this();
        withContainer(container);
    }

    public SplitButton(WebLocator container, String text) {
        this(container);
        withText(text);
    }

    public boolean clickOnMenu(String option) {
        return clickOnMenu(new String[]{option});
    }

    public boolean clickOnMenu(String[] menuOptions) {
        int n = menuOptions.length;
        LOGGER.debug("clickOnMenu : " + menuOptions[n - 1]);
        assertReady();
        boolean selected = true;
        if (showMenu()) {
            for (String val : menuOptions) {
                WebLocator option = getComboEl(val, false, 300);
                selected = selected && option.click();
            }
        } else {
            LOGGER.debug("(" + toString() + ") The element arrow could not be located.");
            selected = false;
        }
        return selected;
    }

    public List<String> getAllSplitButtonValues() {
        click();
        WebLocator comboList = new WebLocator(new WebLocator("x-menu")).withClasses("x-menu-body").withVisibility(true);
        String[] comboValues = comboList.getText().split("\\n");
        click();
        return Arrays.asList(comboValues);
    }

    private WebLocator getComboEl(String value, boolean startWith, long optionRenderMillis) {
        WebLocator comboListElement = new WebLocator("x-menu").withAttribute("aria-hidden", "false").withInfoMessage(this + " -> x-menu");
        return new WebLocator(comboListElement).withText(value, startWith ? SearchType.STARTS_WITH : SearchType.EQUALS).withRenderMillis(optionRenderMillis).withInfoMessage(value);
    }
}

@@ -1368,27 +1368,20 @@
 %3E getAll
-SplitButton
+Menu
 Values()
@@ -1419,25 +1419,29 @@
 Locator 
-comboList
+menuContainer
  = new W
@@ -1450,16 +1450,95 @@
 Locator(
+%22x-menu%22).withAttribute(%22aria-hidden%22, %22false%22);%0A        WebLocator menuList = 
 new WebL
@@ -1544,25 +1544,29 @@
 Locator(
-%22x-
 menu
-%22)
+Container
 ).withCl
@@ -1594,23 +1594,76 @@
 with
-Visibility(true
+InfoMessage(this + %22 -%3E x-menu-body%22);%0A        menuList.assertReady(
 );%0A 
@@ -1678,21 +1678,20 @@
 tring%5B%5D 
-combo
+menu
 Values =
@@ -1691,21 +1691,20 @@
 alues = 
-combo
+menu
 List.get
@@ -1770,21 +1770,20 @@
 .asList(
-combo
+menu
 Values);

Rename getAllSplitButtonValues() to getAllMenuValue() Change the menu locators to take into consideration the "aria-hidden" attribute when getting the elements