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 Object getAllValues(WebElement element) {
            Select selectList = new Select(element);
            Function<WebElement, String> getTextFunctor = new Function<WebElement, String>() {
                @Override
                public String apply(WebElement input) {
                    return input.getText();
                }
            };
            return Lists.transform(selectList.getAllSelectedOptions(), getTextFunctor);
        }
    };

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

    public abstract String getValue(WebElement element);
}
@@ -102,30 +102,79 @@
 ort 
-org.openqa.selenium.By
+com.google.common.base.Function;%0Aimport com.google.common.collect.Lists
 ;%0Aim
@@ -261,60 +261,8 @@
 t;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 publ
@@ -3490,40 +3490,38 @@
     
-List%3CString%3E options = getValues
+Select selectList = new Select
 (ele
@@ -3550,21 +3550,52 @@
 urn 
-o
+selectList.getFirstSelectedO
 ption
-s
+()
 .get
+Text
 (
-0
 );%0A 

fix getValue for select lists