package com.atlassian.bitbucket.internal.mirroring.mirror.rest.server;

import com.atlassian.bitbucket.internal.mirroring.mirror.ExternalProject;
import com.atlassian.bitbucket.rest.RestMapEntity;
import org.hibernate.validator.constraints.NotBlank;

import java.util.Map;

import static com.atlassian.bitbucket.internal.mirroring.mirror.rest.server.RestServerEntityProperties.*;

public class RestServerExternalProject extends RestMapEntity implements ExternalProject {

    // for Jackson
    public RestServerExternalProject() {
    }

    public RestServerExternalProject(ExternalProject externalProject) {
        put(ID, externalProject.getId());
        put(KEY, externalProject.getKey());
        put(NAME, externalProject.getName());
        put(PUBLIC, externalProject.isPublic());
    }

    private RestServerExternalProject(Map<String, Object> map) {
        super(map);
    }

    @NotBlank
    @Override
    public String getId() {
        return String.valueOf(get(ID));
    }

    @NotBlank
    @Override
    public String getKey() {
        return getStringProperty(KEY);
    }

    @NotBlank
    @Override
    public String getName() {
        return getStringProperty(NAME);
    }

    @Override
    public boolean isPublic() {
        return getBoolProperty(PUBLIC, false);
    }

    @SuppressWarnings("unchecked")
    public static RestServerExternalProject valueOf(Object value) {
        if (value instanceof RestServerExternalProject) {
            return (RestServerExternalProject) value;
        }
        if (value instanceof Map) {
            return new RestServerExternalProject((Map) value);
        }
        if (value instanceof ExternalProject) {
            return new RestServerExternalProject((ExternalProject) value);
        }
        return null;
    }
}
