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

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.cloud.RestCloudEntityProperties.*;

public class RestCloudExternalProject extends RestMapEntity implements ExternalProject {

    // for Jackson
    public RestCloudExternalProject() {
    }

    public RestCloudExternalProject(Map<String, ?> map) {
        super(map);
    }

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

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

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

    /**
     * In cloud, public projects can have private repos, but in server that is not possible (a public project grants
     * access to all its repos even if they are marked private). So for cloud we must rely on the repo's access
     * alone to determine public access. Consequently all mirrored cloud projects must always be marked private.
    */
    @Override
    public boolean isPublic() {
        return false;
    }

    @SuppressWarnings("unchecked")
    public static RestCloudExternalProject valueOf(Object value) {
        if (value instanceof RestCloudExternalProject) {
            return (RestCloudExternalProject) value;
        }
        if (value instanceof Map<?, ?>) {
            return new RestCloudExternalProject((Map) value);
        }
        return null;
    }
}
