package com.atlassian.bitbucket.mesh.git.exception;

import com.atlassian.bitbucket.mesh.rpc.v1.RpcGitError;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcInvalidRefName;
import com.google.protobuf.ByteString;

import javax.annotation.Nonnull;

import static com.atlassian.bitbucket.mesh.rpc.v1.RpcInvalidRefName.Reason;
import static io.grpc.Status.INVALID_ARGUMENT;
import static java.util.Objects.requireNonNull;

public class InvalidRefNameException extends AbstractGitException {

    private final Reason reason;
    private final String refName;

    public InvalidRefNameException(@Nonnull Reason reason, @Nonnull String refName) {
        super("[" + refName + "] is not a valid ref name: " + reason, INVALID_ARGUMENT);

        this.reason = requireNonNull(reason, "reason");
        this.refName = requireNonNull(refName, "refName");
    }

    @Nonnull
    public Reason getReason() {
        return reason;
    }

    @Nonnull
    public String getRefName() {
        return refName;
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setInvalidRefName(getError())
                .build();
    }

    protected RpcInvalidRefName.Builder getError() {
        return RpcInvalidRefName.newBuilder()
                .setReason(reason)
                .setRefName(ByteString.copyFromUtf8(refName));
    }
}
