package com.atlassian.stash.internal.web.fragments;

import com.atlassian.bitbucket.util.MoreCollectors;
import com.atlassian.plugin.PluginParseException;
import com.atlassian.plugin.web.ContextProvider;
import com.atlassian.stash.internal.integrity.IntegrityCheckReporter;
import com.atlassian.stash.internal.integrity.IntegrityEventKey;
import com.google.common.collect.ImmutableMap;

import java.util.List;
import java.util.Map;

public class IntegrityChecksContextProvider implements ContextProvider {

    private final IntegrityCheckReporter integrityCheckReporter;

    IntegrityChecksContextProvider(IntegrityCheckReporter integrityCheckReporter) {
        this.integrityCheckReporter = integrityCheckReporter;
    }

    @Override
    public Map<String, Object> getContextMap(Map<String, Object> context) {
        List<String> eventIds = integrityCheckReporter.getLatestEvents(2)
                .stream()
                .map(IntegrityEventKey::getId)
                .collect(MoreCollectors.toImmutableList());

        return ImmutableMap.of("integrityCheckState", eventIds);
    }

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

    }
}
