package com.sdl.bootstrap.button;

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

/**
 * <p><b><i>Used for finding element process (to generate xpath address)</i><b></p>
 * <p>Example:</p>
 * <pre>
 * {@code
 *  <button class="btn" type="button">Save</button>
 *}
 * </pre>
 * <p>In Java write this:</p>
 * <pre>
 * {@code
 *  new Button(container, "Save") or new Button().setText("Save");
 *  }
 * </pre>
 */
public class Button extends WebLocator implements IButton {
    private static final Logger LOGGER = LoggerFactory.getLogger(Button.class);

    private String iconCls;

    public String getIconCls() {
        return iconCls;
    }

    public <T extends Button> T setIconCls(final String iconCls) {
        this.iconCls = iconCls;
        String key = "icon-cls";
        setElPathSuffix(key, applyTemplate(key, iconCls));
        return (T) this;
    }

    public Button() {
        setClassName("Button");
        setBaseCls("btn");
        setTag("button");
        setTemplate("icon-cls", "count(.//*[contains(@class, '%s')]) > 0");
    }

    /**
     * <pre>
     * {@code
     * <form class="panel">
     *          <button class="btn" type="button">Save</button>
     *        </form>
     * }
     * </pre>
     * <p>In Java appears like this:</p>
     * <pre>
     * {@code
     * Form panelForm = new Form().setClasses("panel");
     *        Button saveButton = new Button(panelForm);
     * }
     * </pre>
     * @param container is component which contains the button
     */
    public Button(WebLocator container) {
        this();
        setContainer(container);
    }

    public Button(WebLocator container, String text) {
        this(container);
        setText(text);
        setSearchTextType(SearchType.EQUALS);
    }

    /**
     * <pre>
     *     {@code
     *<button class="btn" type="button" disabled>DisableBtn</button>
     *           <button class="btn disabled" type="button">DisableBtnCls</button>
     * }
     * </pre>
     *
     * <p>Example:</p>
     * <pre>
     *     {@code
     * Button disableButton = new Button().setText("DisableBtn");
     *            disableButton.isDisabled();
     * }
     * </pre>
     * @return true if element has attribute disabled or class disabled otherwise false
     */
    public boolean isDisabled() {
        String cls = getAttributeClass();
        return (cls != null && cls.contains("disabled")) || getAttribute("disabled") != null;
    }
}
@@ -328,37 +328,32 @@
 e:%3C/p%3E%0D%0A * %3Cpre%3E
-%0D%0A * 
 %7B@code%0D%0A *  %3Cbut
@@ -346,17 +346,16 @@
 code%0D%0A *
- 
  %3Cbutton
@@ -398,22 +398,18 @@
 ton%3E%0D%0A *
-%7D%0D%0A *
  
+%7D
 %3C/pre%3E%0D%0A
@@ -447,21 +447,16 @@
  * %3Cpre%3E
-%0D%0A * 
 %7B@code%0D%0A
@@ -461,42 +461,28 @@
 %0D%0A *
-  new
  Button
-(container, %22Save%22) or
+ saveButton =
  new
@@ -516,15 +516,39 @@
 %0A * 
- %7D
+saveButton.assertClick();
 %0D%0A * 
+%7D
 %3C/pr
@@ -1230,472 +1230,8 @@
 %0D%0A%0D%0A
-    /**%0D%0A     * %3Cpre%3E%0D%0A     * %7B@code%0D%0A     * %3Cform class=%22panel%22%3E%0D%0A     *          %3Cbutton class=%22btn%22 type=%22button%22%3ESave%3C/button%3E%0D%0A     *        %3C/form%3E%0D%0A     * %7D%0D%0A     * %3C/pre%3E%0D%0A     * %3Cp%3EIn Java appears like this:%3C/p%3E%0D%0A     * %3Cpre%3E%0D%0A     * %7B@code%0D%0A     * Form panelForm = new Form().setClasses(%22panel%22);%0D%0A     *        Button saveButton = new Button(panelForm);%0D%0A     * %7D%0D%0A     * %3C/pre%3E%0D%0A     * @param container is component which contains the button%0D%0A     */%0D%0A
     
@@ -1508,37 +1508,24 @@
      * %3Cpre%3E
-%0D%0A     *     
 %7B@code%0D%0A    
@@ -1526,16 +1526,17 @@
 %0D%0A     *
+ 
 %3Cbutton 
@@ -1597,26 +1597,16 @@
 %0D%0A     *
-          
  %3Cbutton
@@ -1669,33 +1669,24 @@
 n%3E%0D%0A     * %7D
-%0D%0A     * 
 %3C/pre%3E%0D%0A    
@@ -1687,16 +1687,21 @@
 %0D%0A     *
+ %3Cp/%3E
 %0D%0A     *
@@ -1730,29 +1730,16 @@
  * %3Cpre%3E
-%0D%0A     *     
 %7B@code%0D%0A
@@ -1811,27 +1811,16 @@
 %0D%0A     *
-           
  disable
@@ -1849,24 +1849,30 @@
      * %7D
+%3C/pre%3E
 %0D%0A     *
  %3C/pre%3E%0D
@@ -1867,15 +1867,8 @@
    *
- %3C/pre%3E
 %0D%0A  

add documentation on Button