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

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

import javax.annotation.Nonnull;

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

public class InvalidBranchNameException extends AbstractGitException {

    private final String branchName;
    private final Reason reason;

    public InvalidBranchNameException(@Nonnull Reason reason, @Nonnull String branchName) {
        super("[" + branchName + "] is not a valid branch name", Status.INVALID_ARGUMENT);

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

    @Nonnull
    public String getBranchName() {
        return branchName;
    }

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

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setInvalidBranchName(RpcInvalidBranchName.newBuilder()
                        .setBranchName(ByteString.copyFromUtf8(branchName))
                        .setReason(reason))
                .build();
    }
}
