package com.atlassian.bitbucket.mesh.git;

import com.atlassian.bitbucket.mesh.git.exception.UnsupportedArgumentException;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcNodeType;

import javax.annotation.Nonnull;

import static com.atlassian.bitbucket.mesh.rpc.v1.RpcNodeType.*;
import static com.atlassian.bitbucket.mesh.rpc.v1.RpcUnsupportedArgument.Reason.UNSUPPORTED_NODE_TYPE;

public enum GitNodeType {

    DIRECTORY(NODE_DIRECTORY),
    FILE(NODE_FILE),
    /**
     * Represents a remote link to an external repository.
     */
    SUBMODULE(NODE_SUBMODULE);
    
    private final RpcNodeType rpc;

    GitNodeType(@Nonnull RpcNodeType rpc) {
        this.rpc = rpc;
    }

    @Nonnull
    public static GitNodeType fromRpc(@Nonnull RpcNodeType rpc) {
        for (GitNodeType type : values()) {
            if (type.rpc == rpc) {
                return type;
            }
        }
        
        throw new UnsupportedArgumentException(UNSUPPORTED_NODE_TYPE, rpc);
    }

    @Nonnull
    public static GitNodeType fromType(@Nonnull String type) {
        switch (type) {
            case "blob":
                return FILE;
            case "commit":
                return SUBMODULE;
            case "tree":
                return DIRECTORY;
        }

        throw new UnsupportedArgumentException(UNSUPPORTED_NODE_TYPE, type);
    }

    @Nonnull
    public RpcNodeType toRpc() {
        return rpc;
    }
}
