package com.sdl.selenium.extjs6.menu;

import com.google.common.base.Strings;
import com.sdl.selenium.WebLocatorUtils;
import com.sdl.selenium.web.SearchType;
import com.sdl.selenium.web.WebLocator;
import com.sdl.selenium.web.link.WebLink;

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

public class Menu extends WebLocator {

    public Menu() {
        setClassName("Menu");
        setBaseCls("x-menu");
        setAttribute("aria-hidden", "false");
        setVisibility(true);
    }

    public Menu(String title, SearchType... searchTypes) {
        this();
        WebLocator titleEL = new WebLocator().setClasses("x-menu-item-plain").setSearchTitleType(SearchType.DEEP_CHILD_NODE_OR_SELF);
        setTemplateTitle(titleEL);
        setTitle(title, searchTypes);
    }

    public void clickOnMenu(String option, SearchType... searchTypes) {
        ready();
        WebLink link = new WebLink(this).setText(option, searchTypes).setSearchTextType(SearchType.DEEP_CHILD_NODE_OR_SELF);
        link.click();
    }

    public boolean doClickOnMenu(String option, SearchType... searchTypes) {
        ready();
        WebLink link = new WebLink(this).setText(option, searchTypes).setSearchTextType(SearchType.DEEP_CHILD_NODE_OR_SELF);
        return link.doClick();
    }

    public List<String> getMenuValues() {
        WebLocator menuList = new WebLocator(this).setClasses("x-menu-body").setVisibility(true).setInfoMessage(this + " -> x-menu-body");
        menuList.assertReady();
        String[] menuValues = menuList.getText().split("\\n");
        return Arrays.asList(menuValues);
    }

    public boolean showMenu(WebLocator parent) {
        final String id = parent.getAttributeId();
        if (!Strings.isNullOrEmpty(id)) {
            String script = "return (function(){var b = Ext.getCmp('" + id + "'); if(b.menu.isHidden()) {b.showMenu();} return !b.menu.isHidden();})()";
            Object object = WebLocatorUtils.doExecuteScript(script);
            return (Boolean) object;
        }
        return false;
    }

    public boolean hideMenu(WebLocator parent) {
        final String id = parent.getAttributeId();
        if (!Strings.isNullOrEmpty(id)) {
            String script = "return (function(){var b = Ext.getCmp('" + id + "'); if(!b.menu.isHidden()) {b.hideMenu();} return b.menu.isHidden();})()";
            Object object = WebLocatorUtils.doExecuteScript(script);
            return (Boolean) object;
        }
        return false;
    }

    public boolean close() {
        final String id = getAttributeId();
        if (!Strings.isNullOrEmpty(id)) {
            String script = "return (function(){var b = Ext.getCmp('" + id + "'); if(b && !b.isHidden()) {b.colse();} return b ? b.isHidden() : false})()";
            Object object = WebLocatorUtils.doExecuteScript(script);
            return (Boolean) object;
        }
        return false;
    }
}
@@ -2102,33 +2102,39 @@
    final String 
-i
+parentI
 d = parent.getAt
@@ -2158,412 +2158,88 @@
     
-if (!Strings.isNullOrEmpty(id)) %7B%0A            String script = %22return (function()%7Bvar b = Ext.getCmp('%22 + id + %22'); if(!b.menu.isHidden()) %7Bb.hideMenu();%7D return b.menu.isHidden();%7D)()%22;%0A            Object object = WebLocatorUtils.doExecuteScript(script);%0A            return (Boolean) object;%0A        %7D%0A        return false;%0A    %7D%0A%0A    public boolean close() %7B%0A        final String id = getAttributeId();
+return hideMenu(parentId);%0A    %7D%0A%0A    public boolean hideMenu(String parentId) %7B
 %0A   
@@ -2262,33 +2262,39 @@
 s.isNullOrEmpty(
-i
+parentI
 d)) %7B%0A          
@@ -2347,33 +2347,39 @@
 Ext.getCmp('%22 + 
-i
+parentI
 d + %22'); if(b &&
@@ -2378,15 +2378,15 @@
  if(
-b && !b
+!b.menu
 .isH
@@ -2401,13 +2401,16 @@
  %7Bb.
-colse
+hideMenu
 ();%7D
@@ -2422,12 +2422,13 @@
 rn b
- ? b
+.menu
 .isH
@@ -2438,16 +2438,9 @@
 en()
- : false
+;
 %7D)()

remove close() method on menu