package com.atlassian.bitbucket.rest.content;

import com.atlassian.bitbucket.content.Path;
import com.atlassian.bitbucket.content.SimplePath;
import com.atlassian.bitbucket.rest.annotation.JsonSurrogate;
import com.atlassian.bitbucket.rest.RestMapEntity;
import org.codehaus.jackson.map.annotate.JsonSerialize;

@JsonSerialize
@JsonSurrogate(Path.class)
public class RestPath extends RestMapEntity {

    public RestPath(Path path) {
        this(path.getParent(), path.getName(), path.getExtension(), path.getComponents(), path.toString());
    }

    public RestPath(String path) {
        this(new SimplePath(path));
    }

    private RestPath(String parent,
                     String name,
                     String extension,
                     String[] components,
                     String toString) {
        putIfNotNull("components", components);
        putIfNotNull("parent", parent);
        putIfNotNull("name", name);
        putIfNotNull("extension", extension);
        putIfNotNull("toString", toString);
    }

    @Override
    public String toString() {
        return getStringProperty("toString");
    }

    public static RestPath fromPath(Path path) {
        return path == null ? null : new RestPath(path);
    }

    public static RestPath fromPath(String path) {
        return path == null ? null : new RestPath(path);
    }

    // REST Documentation Examples

    public static final RestPath EXAMPLE = RestPath.fromPath("path/to/file.txt");

}
