package com.sdl.selenium.extjs6.form;

import com.sdl.selenium.web.SearchType;
import com.sdl.selenium.web.WebLocator;
import lombok.extern.slf4j.Slf4j;

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

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

@Slf4j
public class TextField extends com.sdl.selenium.web.form.TextField {

    public TextField() {
        setLabelPosition("//following-sibling::*//");
        setBaseCls("x-form-text");
    }

    public TextField(WebLocator container) {
        this();
        setContainer(container);
    }

    public TextField(WebLocator container, String label, SearchType... searchTypes) {
        this(container);
        if (searchTypes.length == 0) {
            searchTypes = new SearchType[]{SearchType.DEEP_CHILD_NODE};
        } else {
            List<SearchType> types = new ArrayList<>(Arrays.asList(searchTypes));
            types.add(SearchType.DEEP_CHILD_NODE);
            searchTypes = types.toArray(new SearchType[0]);
        }
        setLabel(label, searchTypes);
    }

    // methods
    public boolean assertSetValue(String value) {
        boolean setted = setValue(value);
        assertThat(setted, is(true));
        return true;
    }

    public WebLocator getTriggerEl(String icon) {
        WebLocator ancestor = new WebLocator(this).setElPath("/ancestor::*[contains(concat(' ', @class, ' '), ' x-form-trigger-wrap ')]");
        return new WebLocator(ancestor).setRoot("/").setClasses("x-form-" + icon).setInfoMessage(this + " -> " + icon);
//        return new WebLocator(this).setRoot("/").setTag("parent::*/parent::*/*").setClasses("x-form-" + icon).setInfoMessage(this + " -> " + icon);
    }

    public boolean clickIcon(String icon) {
        if (ready()) {
            WebLocator iconLocator = getTriggerEl(icon);
            iconLocator.setRenderMillis(500);
            return iconLocator.click();
        } else {
            log.warn("clickIcon : field is not ready for use: " + this);
        }
        return false;
    }

    /**
     * @return true is the element doesn't have attribute readonly
     */
    public boolean isEditable() {
        return !"true".equals(getAttribute("readonly"));
    }

    public boolean isEnabled() {
        String aClass = getAttributeClass();
        return !aClass.contains("x-item-disabled") && !"true".equals(getAttribute("disabled"));
    }

    public String getError() {
        WebLocator error = new WebLocator(this).setRoot("/../../../../").setClasses("x-form-error-wrap");
        return error.getText();
    }
}
@@ -2354,16 +2354,35 @@
   return
+ (aClass != null &&
  !aClass
@@ -2413,27 +2413,13 @@
 ed%22)
- && !%22true%22.equals(
+) %7C%7C 
 getA
@@ -2438,17 +2438,24 @@
 sabled%22)
-)
+ == null
 ;%0A    %7D%0A

fix isEnabled() method in TextField