package com.atlassian.webdriver.bitbucket.element;

import com.atlassian.pageobjects.binder.Init;
import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.elements.PageElementFinder;
import com.atlassian.pageobjects.elements.query.TimedCondition;
import com.google.inject.Inject;
import org.openqa.selenium.By;

import java.util.List;

public class LikesInfoDialog {

    @Inject
    private PageElementFinder elementFinder;

    private PageElement dialog;
    private By locator;

    public LikesInfoDialog(By locator) {
        this.locator = locator;
    }

    // This cannot be done in the constructor because the elementFinder wont be injected until after construction
    @Init
    public void init() {
        dialog = elementFinder.find(locator);
    }

    public int getNumberOfUsers() {
        return getUsers().size();
    }

    /**
     *
     * @param index 0 based index into the list of users in the dialog
     * @return the users display name
     */
    public String getUserName(int index) {
        return getUsers().get(index).getText();
    }

    private List<PageElement> getUsers() {
        return dialog.find(By.className("user-list")).findAll(By.tagName("li"));
    }

    public TimedCondition isOpen() {
        return dialog.timed().isVisible();
    }
}
