package com.sdl.selenium.extjs3.tab;

import com.sdl.selenium.WebLocatorUtils;
import com.sdl.selenium.extjs3.ExtJsComponent;
import com.sdl.selenium.web.SearchType;
import com.sdl.selenium.web.WebLocator;
import com.sdl.selenium.web.XPathBuilder;
import com.sdl.selenium.web.utils.Utils;
import org.openqa.selenium.ElementNotVisibleException;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

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

    private TabPanel() {
        setClassName("TabPanel");
        setBaseCls("x-tab-panel");
    }

    public TabPanel(String text) {
        this();
        setText(text, SearchType.EQUALS);
    }

    public TabPanel(WebLocator container, String text) {
        this(text);
        setContainer(container);
    }

    private String getTitlePath() {
        WebLocator header = new WebLocator().setClasses("x-tab-panel-header");
        WebLocator locator = new WebLocator().setText(getPathBuilder().getText()).setContainer(header);
        locator.setSearchTextType(getPathBuilder().getSearchTextType().toArray(new SearchType[getPathBuilder().getSearchTextType().size()]));
        return locator.getXPath();
    }

    /**
     * this method return the path of the main TabPanel (that contains also this Tab/Panel)
     *
     * @return the path of the main TabPanel
     */
    private String getBaseTabPanelPath() {
        String selector = getPathBuilder().getBasePath();
        WebLocator el = new WebLocator().setText(getPathBuilder().getText(), SearchType.EQUALS);
        el.setSearchTextType(getPathBuilder().getSearchTextType().toArray(new SearchType[getPathBuilder().getSearchTextType().size()]));
        selector += (selector.length() > 0 ? " and " : "") + "not(contains(@class, 'x-masked')) and count(*[contains(@class,'x-tab-panel-header')]//*[contains(@class, 'x-tab-strip-active')]" + el.getXPath() + ") > 0";
        return "//*[" + selector + "]";
    }

    protected XPathBuilder createXPathBuilder() {
        return new XPathBuilder() {
            /**
             * this method return the path of only one visible div from the main TabPanel
             *
             * @param disabled disabled
             * @return the path of only one visible div from the main TabPanel
             */
            @Override
            public String getItemPath(boolean disabled) {
                String selector = getBaseTabPanelPath();
                selector += "/*/*[contains(@class, 'x-tab-panel-body')]" +  //TODO
                        "/*[not(contains(@class, 'x-hide-display'))]"; // "/" is because is first element after -body
                return selector;
            }
        };
    }

    /**
     * After the tab is set to active will wait 300ms to make sure tab is rendered
     *
     * @return true or false
     */
    public boolean setActive() {
        String baseTabPath = "//*[" + getPathBuilder().getBasePath() + "]";
        String titlePath = baseTabPath + getTitlePath();
        WebLocator titleElement = new WebLocator(getPathBuilder().getContainer()).setElPath(titlePath).setInfoMessage(getPathBuilder().getText() + " Tab");
        LOGGER.info("setActive : " + toString());
        boolean activated;
        try {
            activated = titleElement.assertClick();
        } catch (ElementNotVisibleException e) {
            LOGGER.error("setActive Exception: " + e.getMessage());
            WebLocator tabElement = new WebLocator(getPathBuilder().getContainer()).setElPath(baseTabPath);
            String id = tabElement.getAttributeId();
            String path = "//*[@id='" + id + "']//*[contains(@class, 'x-tab-strip-inner')]";
            int tabIndex = getTabIndex(getPathBuilder().getText(), path);
            String script = "return Ext.getCmp('" + id + "').setActiveTab(" + tabIndex + ");";
            LOGGER.warn("force TabPanel setActive with js: " + script);
            WebLocatorUtils.doExecuteScript(script);
            activated = true; // TODO verify when is not executed
        }
        if (activated) {
            Utils.sleep(300); // need to make sure this tab is rendered
        }
        return activated;
    }

    public int getTabIndex(String title, String path) {
        WebLocator titles = new WebLocator().setElPath(path);
        List<WebElement> element = titles.findElements();
        int index = 0;
        for (WebElement el : element) {
            if (title.equals(el.getText())) {
                LOGGER.debug(index + " : " + el.getText());
                return index;
            }
            index++;
        }
        return -1;
    }

    /**
     * @deprecated use {@link #getTabIndex(String, String)}
     * @param nameTab
     * @param path
     * @return
     */
    public int getTabCount(String nameTab, String path) {
        return getTabIndex(nameTab, path);
    }
}

@@ -3613,32 +3613,492 @@
 essage());%0D%0A    
+        activated = setActiveWithExtJS();%0D%0A        %7D%0D%0A        if (activated) %7B%0D%0A            Utils.sleep(300); // need to make sure this tab is rendered%0D%0A        %7D%0D%0A        return activated;%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * TO Be used in extreme cases when simple %7B@link #setActive()%7D is not working%0D%0A     *%0D%0A     * @return true or false%0D%0A     */%0D%0A    public boolean setActiveWithExtJS() %7B%0D%0A        String baseTabPath = %22//*%5B%22 + getPathBuilder().getBasePath() + %22%5D%22;%0D%0A
         WebLocat
@@ -4190,28 +4190,24 @@
 );%0D%0A        
-    
 String id = 
@@ -4232,28 +4232,24 @@
 ibuteId();%0D%0A
-    
         Stri
@@ -4330,28 +4330,24 @@
 %22;%0D%0A        
-    
 int tabIndex
@@ -4393,28 +4393,24 @@
 (), path);%0D%0A
-    
         Stri
@@ -4493,28 +4493,24 @@
 %22;%0D%0A        
-   
- 
 LOGGER.warn(
@@ -4562,28 +4562,24 @@
 );%0D%0A        
-    
 WebLocatorUt
@@ -4620,212 +4620,19 @@
     
-    activated = true; // TODO verify when is not executed%0D%0A        %7D%0D%0A        if (activated) %7B%0D%0A            Utils.sleep(300); // need to make sure this tab is rendered%0D%0A        %7D%0D%0A        return activated
+return true
 ;%0D%0A 

add setActiveWithExtJS()