package com.atlassian.stash.internal.plugin.hooks.task;

import com.atlassian.plugin.PluginParseException;
import com.atlassian.plugin.web.ContextProvider;
import com.atlassian.bitbucket.repository.Repository;
import com.google.common.collect.ImmutableMap;

import java.util.Map;

public class IncompleteTasksMergeCheckContextProvider implements ContextProvider {
    private static final String REPOSITORY = "repository";
    private final IncompleteTasksMergeCheckConfig mergeCheckConfig;

    public IncompleteTasksMergeCheckContextProvider(IncompleteTasksMergeCheckConfig mergeCheckConfig) {
        this.mergeCheckConfig = mergeCheckConfig;
    }

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

    @Override
    public Map<String, Object> getContextMap(Map<String, Object> context) {
        boolean mergeCheckEnabled = false;

        final Object repository = context.get(REPOSITORY);
        if (repository instanceof Repository) {
            mergeCheckEnabled = mergeCheckConfig.isResolutionRequired((Repository) repository);
        }
        return ImmutableMap.<String, Object>of(
                "incompleteTasksMergeCheck", ImmutableMap.of("enabled", mergeCheckEnabled)
        );
    }
}
