package com.e2open.falcon.framework.elements;

import com.e2open.falcon.framework.waiters.Waiter;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

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

public enum ElementType {
    TEXT {
        @Override
        public void setValue(WebElement element, Object value) {
            element.clear();
            element.sendKeys(value.toString());
        }

        @Override
        public String getValue(WebElement element) {
            return element.getAttribute("value");
        }
    }, CHECKBOX {
        @Override
        public void setValue(WebElement element, Object value) {
            if (value instanceof Boolean) {
                if ((Boolean) value) {
                    if (!element.isSelected()) element.click();
                } else {
                    if (element.isSelected()) element.click();
                }
            } else if (value instanceof String) {
                if (((String) value).toLowerCase().equals("true")) {
                    if (!element.isSelected()) element.click();
                } else if (((String) value).toLowerCase().equals("false")) {
                    if (element.isSelected()) element.click();
                } else {
                    throw new RuntimeException("Not sure how to handle setting radio using: " + value.toString());

                }
            } else {
                throw new RuntimeException("Not sure how to handle setting radio using: " + value.toString());
            }
        }

        @Override
        public String getValue(WebElement element) {
            return element.isSelected() ? "true" : "false";
        }
    }, RADIO {
        @Override
        public void setValue(WebElement element, Object value) {
            element.click();
        }

        @Override
        public String getValue(WebElement element) {
            return element.isSelected() ? "true" : "false";
        }
    }, SUBMIT {
        @Override
        public void setValue(WebElement element, Object value) {
            element.click();
        }

        @Override
        public String getValue(WebElement element) {
            return null;
        }
    }, PASSWORD {
        @Override
        public void setValue(WebElement element, Object value) {
            element.clear();
            element.sendKeys(value.toString());
        }

        @Override
        public String getValue(WebElement element) {
            return element.getAttribute("value");
        }
    }, LINK {
        @Override
        public void setValue(WebElement element, Object value) {
            element.click();
        }

        @Override
        public String getValue(WebElement element) {
            return null;
        }
    }, DIV {
        @Override
        public void setValue(WebElement element, Object value) {
            element.click();
        }

        @Override
        public String getValue(WebElement element) {
            return null;
        }
    }, SELECT {
        @Override
        public void setValue(WebElement element, Object value) {
            Select selectList = new Select(element);
            Waiter waiter = new Waiter();
            waiter.waitUntilSelectOptionPresent(selectList, (String) value);
            selectList.selectByVisibleText(value.toString());
        }

        @Override
        public String getValue(WebElement element) {
            List<String> options = getValues(element);
            return options.get(0);
        }

        public List<String> getValues(WebElement element) {
            ArrayList<String> values = new ArrayList<String>();
            List<WebElement> options = element.findElements(By.tagName("option"));
            for (WebElement option : options) {
                if (option.isSelected()) {
                    values.add(option.getText());
                }
            }
            return values;
        }
    };

    public abstract void setValue(WebElement element, Object value);

    public abstract String getValue(WebElement element);
}
@@ -3597,24 +3597,21 @@
 lic 
-List%3CString%3E
+Object
  get
+All
 Valu
@@ -3651,57 +3651,46 @@
     
-ArrayList%3CString%3E values = new ArrayList%3CString%3E(
+Select selectList = new Select(element
 );%0A 
@@ -3700,20 +3700,24 @@
         
-List
+Function
 %3CWebElem
@@ -3723,151 +3723,150 @@
 ment
-%3E options = element.findElements(By.tagName(%22option%22));%0A            for (WebElement option : options) %7B%0A                if (option.isSelected()
+, String%3E getTextFunctor = new Function%3CWebElement, String%3E() %7B%0A                @Override%0A                public String apply(WebElement input
 ) %7B%0A
@@ -3889,25 +3889,20 @@
     
-values.add(option
+return input
 .get
@@ -3907,17 +3907,16 @@
 etText()
-)
 ;%0A      
@@ -3932,32 +3932,33 @@
  %7D%0A            %7D
+;
 %0A            ret
@@ -3965,14 +3965,75 @@
 urn 
-values
+Lists.transform(selectList.getAllSelectedOptions(), getTextFunctor)
 ;%0A  

return all list values