package com.atlassian.bitbucket.internal.importer.rest;

import com.atlassian.bitbucket.internal.importer.ExternalSource;
import com.atlassian.bitbucket.internal.importer.SourceType;
import com.atlassian.bitbucket.rest.RestMapEntity;
import com.atlassian.bitbucket.rest.annotation.JsonSurrogate;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import javax.annotation.Nullable;
import java.util.Map;

@JsonSerialize
@JsonSurrogate(ExternalSource.class)
public class RestExternalSource extends RestMapEntity {

    public static final String URL = "url";
    public static final String TYPE = "type";

    // for Jersey
    public RestExternalSource() {
    }

    private RestExternalSource(Map<String, Object> values) {
        super(values);
    }

    public String getUrl() {
        return getStringProperty(URL);
    }

    public SourceType getType() {
        return getEnumProperty(TYPE, SourceType.class);
    }

    @Nullable
    public static RestExternalSource valueOf(@Nullable Object value) {
        if (value instanceof RestExternalSource) {
            return (RestExternalSource) value;
        } else if (value instanceof Map) {
            return new RestExternalSource((Map) value);
        }
        return null;
    }
}