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.user.ApplicationUser;
import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.bitbucket.permission.PermittedUser;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.util.function.Function;

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

@JsonSerialize
@JsonSurrogate(PermittedUser.class)
public class RestPermittedUser extends RestMapEntity {

    public static final Function<PermittedUser, RestPermittedUser> REST_TRANSFORM = RestPermittedUser::fromPermittedUser;

    public RestPermittedUser(PermittedUser permittedUser) {
        this(permittedUser.getUser(), permittedUser.getPermission());
    }

    // Required for REST documentation
    private RestPermittedUser(ApplicationUser stashUser, Permission permission) {
        put("user", new RestApplicationUser(stashUser));
        put("permission", permission.name());
    }

    public static RestPermittedUser fromPermittedUser(PermittedUser permittedUser) {
        return permittedUser == null ? null : new RestPermittedUser(permittedUser);
    }

    // REST Documentation Examples

    public static final RestPermittedUser RESPONSE_EXAMPLE = new RestPermittedUser(RestApplicationUser.RESPONSE_EXAMPLE,
            Permission.ADMIN);

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

}
