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.repository.RepositoryService;

import java.util.Map;

/**
 * A condition which checks if a repository is empty
 */
public class RepositoryNotEmptyCondition implements Condition {

    static final String REPOSITORY = "repository";

    private final RepositoryService repositoryService;

    public RepositoryNotEmptyCondition(RepositoryService repositoryService) {
        this.repositoryService = repositoryService;
    }

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

    @Override
    public boolean shouldDisplay(Map<String, Object> context) {
        Object repository = context.get(REPOSITORY);
        return !(repository instanceof Repository) || !repositoryService.isEmpty((Repository) repository);
    }
}
