package com.atlassian.bitbucket.rest.project;

import com.atlassian.bitbucket.rest.user.RestApplicationUser;
import com.atlassian.bitbucket.project.PersonalProject;
import com.atlassian.bitbucket.project.ProjectType;
import com.atlassian.bitbucket.project.ProjectVisitor;
import com.atlassian.bitbucket.rest.annotation.JsonSurrogate;
import com.atlassian.bitbucket.user.ApplicationUser;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import javax.annotation.Nonnull;

@JsonSerialize
@JsonSurrogate(PersonalProject.class)
public class RestPersonalProject extends RestProject implements PersonalProject {

    private static final String OWNER = "owner";

    @SuppressWarnings("unused") //Required by Jersey
    public RestPersonalProject() {
    }

    public RestPersonalProject(PersonalProject project) {
        this(project.getId(), project.getNamespace(), project.getKey(), project.getName(), project.getDescription(), project.getOwner());
    }

    private RestPersonalProject(int id, String namespace, String key, String name, String description, ApplicationUser owner) {
        super(id, namespace, key, name, description, ProjectType.PERSONAL, null);

        if (owner != null) {
            put(OWNER, new RestApplicationUser(owner));
        }
    }

    @Override
    public <T> T accept(@Nonnull ProjectVisitor<T> visitor) {
        return visitor.visit(this);
    }

    @Nonnull
    @Override
    public ApplicationUser getOwner() {
        return RestApplicationUser.valueOf(get(OWNER));
    }
}
