package com.atlassian.bitbucket.test.rest.server.stub.mirror;

import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

public class DescriptorUtils {
    public static String generateDescriptor(String key, String name, String selfUrl, String installedUrl,
                                            String uninstalledUrl, String enabledUrl, String disabledUrl,
                                            String webhookEventsUrl, String baseUrl) {
        Map<String, Object> descriptor = new LinkedHashMap<>();
        descriptor.put("key", key);
        descriptor.put("name", name);
        descriptor.put("vendor", ImmutableMap.of("name", "Atlassian", "url", "http://www.atlassian.com"));
        descriptor.put("links", ImmutableMap.of("self", selfUrl));
        descriptor.put("lifecycle", ImmutableMap.builder()
                .put("installed", installedUrl)
                .put("uninstalled", uninstalledUrl)
                .put("enabled", enabledUrl)
                .put("disabled", disabledUrl)
                .build());
        descriptor.put("baseUrl", baseUrl);
        descriptor.put("authentication", ImmutableMap.of("type", "jwt"));
        descriptor.put("scopes", ImmutableList.of("read"));
        descriptor.put("modules", ImmutableMap.of(
                "webhooks", ImmutableList.of(
                        ImmutableMap.of("event", "project:*", "url", webhookEventsUrl),
                        ImmutableMap.of("event", "repo:*", "url", webhookEventsUrl))));

        try {
            return new ObjectMapper().writeValueAsString(descriptor);
        } catch (IOException e) {
            throw Throwables.propagate(e);
        }
    }
}
