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

import com.atlassian.bitbucket.mesh.rpc.v1.RpcGitError;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcInvalidTagName;
import com.google.protobuf.ByteString;
import io.grpc.Status;

import javax.annotation.Nonnull;

import static com.atlassian.bitbucket.mesh.rpc.v1.RpcInvalidTagName.Reason;
import static java.util.Objects.requireNonNull;

public class InvalidTagNameException extends AbstractGitException {

    private final Reason reason;
    private final String tagName;

    public InvalidTagNameException(@Nonnull Reason reason, @Nonnull String tagName) {
        super("[" + tagName + "] is not a valid tag name: " + reason, Status.INVALID_ARGUMENT);

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

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

    @Nonnull
    public String getTagName() {
        return tagName;
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setInvalidTagName(RpcInvalidTagName.newBuilder()
                        .setReason(reason)
                        .setTagName(ByteString.copyFromUtf8(tagName)))
                .build();
    }
}
