package com.atlassian.bitbucket.mesh.git.builder.archive;

import com.atlassian.bitbucket.mesh.rpc.v1.RpcArchiveFormat;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

public enum GitArchiveFormat {

    TAR("tar", "application/x-tar"),
    TAR_GZ("tar.gz"),
    TGZ("tgz"),
    ZIP("zip");

    private final String contentType;
    private final String extension;

    GitArchiveFormat(String extension) {
        this(extension, "application/octet-stream");
    }

    GitArchiveFormat(String extension, String contentType) {
        this.contentType = contentType;
        this.extension = extension;
    }

    @Nonnull
    public static GitArchiveFormat fromRpc(@Nonnull RpcArchiveFormat rpc) {
        switch(requireNonNull(rpc, "rpc")) {
            case ARCHIVE_FORMAT_TAR_GZ:
                return TAR_GZ;
            case ARCHIVE_FORMAT_TGZ:
                return TGZ;
            case ARCHIVE_FORMAT_ZIP:
                return ZIP;
            default: // tar, unspecified
                return TAR;
        }
    }

    @Nonnull
    public String getContentType() {
        return contentType;
    }

    @Nonnull
    public String getExtension() {
        return extension;
    }
}
