package org.getopentest.selenium;

import org.getopentest.selenium.core.SeleniumTestAction;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.Select;

public class DeselectListOption extends SeleniumTestAction {

    @Override
    public void run() {
        super.run();

        By locator = this.readLocatorArgument("locator");
        String optionValue = this.readStringArgument("optionValue", null);
        String optionText = this.readStringArgument("optionText", null);
        Integer optionNumber = this.readIntArgument("optionNumber", null);

        this.waitForAsyncCallsToFinish();

        Select dropdownElement = new Select(this.getElement(locator));

        if (optionValue != null) {
            dropdownElement.deselectByValue(optionValue);
        } else if (optionText != null) {
            dropdownElement.deselectByVisibleText(optionText);
        } else if (optionNumber != null) {
            dropdownElement.deselectByIndex(optionNumber - 1);
        } else {
            throw new RuntimeException(
                    "You must identify the option you want to deselect from the "
                    + "list by providing one of the following arguments: "
                    + "optionValue, optionText or optionIndex.");
        }

    }

}