package com.atlassian.bitbucket.rest.user;

import com.atlassian.bitbucket.rest.annotation.JsonSurrogate;
import com.atlassian.bitbucket.rest.RestMapEntity;
import com.atlassian.bitbucket.rest.util.RestPage;
import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.bitbucket.permission.PermittedGroup;
import com.google.common.collect.ImmutableMap;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.util.function.Function;

import static com.atlassian.bitbucket.rest.util.RestDocUtils.pageOf;

@JsonSerialize
@JsonSurrogate(PermittedGroup.class)
public class RestPermittedGroup extends RestMapEntity {

    public static final Function<PermittedGroup, RestPermittedGroup> REST_TRANSFORM = RestPermittedGroup::fromPermittedGroup;

    public RestPermittedGroup(PermittedGroup permittedGroup) {
        this(permittedGroup.getGroup(), permittedGroup.getPermission());
    }

    // Required for REST documentation
    private RestPermittedGroup(String name, Permission permission) {
        put("group", ImmutableMap.of("name", name));
        put("permission", permission.name());
    }

    public static RestPermittedGroup fromPermittedGroup(PermittedGroup permittedGroup) {
        return permittedGroup != null ? new RestPermittedGroup(permittedGroup) : null;
    }

    // REST Documentation Examples

    public static final RestPermittedGroup RESPONSE_EXAMPLE = new RestPermittedGroup("group_a", Permission.ADMIN);

    public static final RestPage<RestPermittedGroup> PAGE_EXAMPLE = pageOf(RESPONSE_EXAMPLE);

}
