package com.sdl.selenium.web.form;

import com.sdl.selenium.web.SearchType;
import com.sdl.selenium.web.WebLocator;
import com.sdl.selenium.web.utils.internationalization.InternationalizationUtils;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.hamcrest.MatcherAssert.assertThat;

public class ComboBox extends WebLocator implements ICombo {
    private static final Logger LOGGER = LoggerFactory.getLogger(ComboBox.class);

    public ComboBox() {
        setClassName("ComboBox");
        setTag("select");
    }

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

    @Override
    public boolean select(String value) {
        return select(value, InternationalizationUtils.isInternationalizedTestsSuite());
    }

    public boolean select(String value, boolean isInternationalized) {
        boolean selected = waitToRender();
        assertThat("Element was not rendered " + toString(), selected);
        selected = doSelect(value, isInternationalized);
        assertThat("Could not selected value on : " + this, selected);
        return selected;
    }

    public boolean doSelect(String value, boolean isInternationalized) {
        value = InternationalizationUtils.getInternationalizedText(value, isInternationalized);
        boolean selected;
        ready();
        if ("".equals(value)) {
            LOGGER.warn("value is empty");
            selected = false;
        } else {
            new Select(getWebElement()).selectByVisibleText(value);
            selected = true;
        }
        if (selected) {
            LOGGER.info("Set value(" + this + "): " + value);
        }
        return selected;
    }

    public boolean doSelect(String value) {
        return doSelect(value, InternationalizationUtils.isInternationalizedTestsSuite());
    }

    @Override
    public String getValue() {
        String value = this.getAttribute("value");
        WebLocator el = new WebLocator(this).setTag("option").setAttribute("value", value, SearchType.CONTAINS);
        return el.getText();
    }
}

@@ -857,32 +857,121 @@
 e());%0D%0A    %7D%0D%0A%0D%0A
+    public boolean selectByIndex(int index) %7B%0D%0A        return doSelect(index);%0D%0A    %7D%0D%0A%0D%0A
     public boole
@@ -1300,32 +1300,242 @@
 cted;%0D%0A    %7D%0D%0A%0D%0A
+    private boolean doSelect(int index) %7B%0D%0A        ready();%0D%0A        new Select(getWebElement()).selectByIndex(index);%0D%0A        LOGGER.info(%22Set value(%22 + this + %22): %22 + index);%0D%0A        return true;%0D%0A    %7D%0D%0A%0D%0A
     public boole

add ComboBox.selectByIndex