package com.atlassian.bitbucket.test.rest;

import com.atlassian.bitbucket.rest.RestLinkedMapEntity;
import com.atlassian.bitbucket.rest.project.RestProject;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.util.List;
import java.util.Map;

@JsonSerialize
public class RestProjects extends RestLinkedMapEntity {

    private static final String PROJECTS = "projects";

    @SuppressWarnings("unused")
    public RestProjects() {
    }

    public RestProjects(Map<String, Object> values) {
        super(values);
    }

    @SuppressWarnings("unchecked")
    public List<RestProject> getProjects() {
        return transform((Iterable) get(PROJECTS), RestProject::valueOf);
    }

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