package com.atlassian.bitbucket.web.conditions;

import com.atlassian.plugin.PluginParseException;
import com.atlassian.plugin.web.Condition;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.atlassian.bitbucket.auth.AuthenticationContext;

import java.util.Map;

/**
 * A condition which checks if a repository is forkable
 */
public class IsRepositoryForkableCondition implements Condition {

    public static final String REPOSITORY = "repository";

    private final AuthenticationContext authenticationContext;

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

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

    @Override
    public boolean shouldDisplay(Map<String, Object> context) {
        Repository repository = (Repository) context.get(REPOSITORY);
        ApplicationUser user = authenticationContext.getCurrentUser();

        return user != null && repository != null && repository.isForkable();


    }
}
