package com.sdl.bootstrap.form;

import com.sdl.selenium.web.SearchType;
import com.sdl.selenium.web.WebDriverConfig;
import com.sdl.selenium.web.WebLocator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

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

public class MultiSelect extends WebLocator {

    public MultiSelect() {
        setClassName("MultiSelect");
        setBaseCls("multiselect dropdown-toggle btn");
        setTag("button");
    }

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

    public MultiSelect(WebLocator container, String label) {
        this(container);
        setLabel(label);
    }

    public boolean select(String... values) {
        boolean selected = false;
        if (click()) {
            WebLocator select = new WebLocator(this, "//following-sibling::*[contains(@class, 'dropdown-menu')]");
            select.ready();
            for (String val : values) {
                WebLocator el = new WebLocator(select).setTag("label").setText(val, SearchType.HTML_NODE);
                CheckBox checkBox = new CheckBox(el).setInfoMessage("check: '" + val + "'");
                selected = checkBox.click();
                if (!selected) {
                    return false;
                }
            }
            click();
        }
        return selected;
    }

    public List<String> getValueSelected() {
        List<String> list = null;
        if (click()) {
            list = new ArrayList<String>();
            WebLocator select = new WebLocator(this, "//following-sibling::*[contains(@class, 'dropdown-menu')]");
            WebLocator li = new WebLocator(select).setTag("li").setCls("active");
            WebLocator el = new WebLocator(li).setTag("label");
            el.ready();
            List<WebElement> elements = WebDriverConfig.getDriver().findElements(By.xpath(el.getPath()));
            for (WebElement element : elements) {
                String text = element.getText();
                list.add(text);
            }
            click();
        }
        return list;
    }
}
@@ -60,232 +60,60 @@
 web.
-SearchType;%0D%0Aimport com.sdl.selenium.web.WebDriverConfig;%0D%0Aimport com.sdl.selenium.web.WebLocator;%0D%0Aimport org.openqa.selenium.By;%0D%0Aimport org.openqa.selenium.WebElement;%0D%0A%0D%0Aimport java.util.ArrayList;%0D%0Aimport java.util.List
+WebLocator;%0D%0Aimport com.sdl.selenium.web.form.ICombo
 ;%0D%0A%0D
@@ -157,16 +157,34 @@
 Locator 
+implements ICombo 
 %7B%0D%0A%0D%0A   
@@ -230,27 +230,28 @@
 ssName(%22
-Multi
 Select
+Picker
 %22);%0D%0A   
@@ -271,19 +271,11 @@
 ls(%22
-multiselect
+btn
  dro
@@ -290,12 +290,8 @@
 ggle
- btn
 %22);%0D
@@ -550,32 +550,47 @@
 bel);%0D%0A    %7D%0D%0A%0D%0A
+    @Override%0D%0A
     public boole
@@ -609,58 +609,19 @@
 ring
-...
  value
-s
 ) %7B%0D%0A
-        boolean selected = false;%0D%0A
     
@@ -755,243 +755,62 @@
 u')%5D
-%22);%0D%0A            select.ready();%0D%0A            for (String val : values) %7B%0D%0A                WebLocator el = new WebLocator(select).setTag(%22label%22).setText(val, SearchType.HTML_NODE);%0D%0A                CheckBox checkBox = new CheckBox(el)
+//span%5Btext()='%22 + value + %22'%5D%22)%0D%0A                    
 .set
@@ -822,21 +822,22 @@
 essage(%22
-check
+select
 : '%22 + v
@@ -838,16 +838,18 @@
 '%22 + val
+ue
  + %22'%22);
@@ -866,31 +866,21 @@
     
-   
+return
  select
-ed = checkBox
 .cli
@@ -898,169 +898,56 @@
     
-        if (!selected) %7B%0D%0A                    return false;%0D%0A                %7D%0D%0A            %7D%0D%0A            click();%0D%0A        %7D%0D%0A        return selected;%0D%0A    %7D%0D%0A
+%7D%0D%0A        return false;%0D%0A    %7D%0D%0A%0D%0A    @Override
 %0D%0A  
@@ -955,28 +955,22 @@
  public 
-List%3C
 String
-%3E
  getValu
@@ -974,16 +974,8 @@
 alue
-Selected
 () %7B
@@ -988,701 +988,135 @@
     
-List%3CString%3E list = null;%0D%0A        if (click()) %7B%0D%0A            list = new ArrayList%3CString%3E();%0D%0A            WebLocator select = new WebLocator(this, %22//following-sibling::*%5Bcontains(@class, 'dropdown-menu')%5D%22);%0D%0A            WebLocator li = new WebLocator(select).setTag(%22li%22).setCls(%22active%22);%0D%0A            WebLocator el = new WebLocator(li).setTag(%22label%22);%0D%0A            el.ready();%0D%0A            List%3CWebElement%3E elements = WebDriverConfig.getDriver().findElements(By.xpath(el.getPath()));%0D%0A            for (WebElement element : elements) %7B%0D%0A                String text = element.getText();%0D%0A                list.add(text);%0D%0A            %7D%0D%0A            click();%0D%0A        %7D%0D%0A        return list
+return getHtmlText().trim();%0D%0A    %7D%0D%0A%0D%0A    @Override%0D%0A    public boolean setValue(String value) %7B%0D%0A        return select(value)
 ;%0D%0A 

add MultiSelects