package com.atlassian.bitbucket.web.conditions;

import com.atlassian.plugin.PluginParseException;
import com.atlassian.plugin.web.Condition;
import com.atlassian.bitbucket.pull.PullRequest;
import com.atlassian.bitbucket.pull.PullRequestState;
import com.atlassian.bitbucket.auth.AuthenticationContext;

import java.util.Map;

/**
 * Verifies that the current user is allowed to approve the current pull request
 */
public class CanApprovePullRequestCondition implements Condition {

    private final AuthenticationContext authenticationContext;

    public CanApprovePullRequestCondition(AuthenticationContext authenticationContext) {
        this.authenticationContext = authenticationContext;
    }

    @Override
    public void init(Map<String, String> params) throws PluginParseException {
    }

    @Override
    public boolean shouldDisplay(Map<String, Object> context) {
        PullRequest pullRequest = (PullRequest) context.get("pullRequest");
        boolean isAuthor = pullRequest.getAuthor().getUser().equals(authenticationContext.getCurrentUser());
        return !isAuthor && pullRequest.getState().equals(PullRequestState.OPEN);
    }
}
