package com.atlassian.webdriver.bitbucket.element;

import com.atlassian.pageobjects.elements.PageElement;
import org.openqa.selenium.By;

public class LocaleSelector {

    private static final String ID = "locale";

    private final PageElement body;

    public LocaleSelector(PageElement body) {
        this.body = body;
    }

    public String getLocale() {
        return body.find(By.id(ID)).getValue();
    }

    public void searchAndSelect(String locale) {
        body.find(By.cssSelector("#s2id_" + ID + " a")).click();
        for (PageElement element : body.findAll(By.className("select2-result-selectable"))) {
            if (element.getText().contains(locale)) {
                element.click();
                break;
            }
        }
    }
}
