package it.com.atlassian.stash.defaultreviewers.page;

import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.webdriver.bitbucket.element.AbstractElementPageObject;
import org.openqa.selenium.By;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.stream.Collectors;

public class DefaultReviewerRuleRow extends AbstractElementPageObject {

    private final DefaultReviewersPage parent;

    public DefaultReviewerRuleRow(@Nonnull PageElement element, DefaultReviewersPage parent) {
        super(element);
        this.parent = parent;
    }

    public DefaultReviewersPage bindParentPage() {
        return pageBinder.bind(DefaultReviewersPage.class, parent.getProjectKey(), parent.getSlug());
    }

    public AddRuleDialog bindRuleDialog() {
        return pageBinder.bind(AddRuleDialog.class, parent);
    }

    public DefaultReviewersPage delete() {
        container.find(By.className("delete-button")).select();
        return bindParentPage();
    }

    public AddRuleDialog edit() {
        container.find(By.className("edit-button")).select();
        return bindRuleDialog();
    }

    public int getRequiredApprovals() {
        return Integer.valueOf(container.getAttribute("data-required-approvals"));
    }

    public List<String> getReviewers() {
        return container.findAll(By.cssSelector("span[data-username]"))
                .stream()
                .map(s -> s.getAttribute("data-username"))
                .collect(Collectors.toList());
    }

    public String getSourceBranchType() {
        return container.getAttribute("data-from-branch-type");
    }

    public String getSourceBranchValue() {
        return container.getAttribute("data-from-branch-value");
    }

    public String getTargetBranchType() {
        return container.getAttribute("data-to-branch-type");
    }

    public String getTargetBranchValue() {
        return container.getAttribute("data-to-branch-value");
    }
}
