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

import com.atlassian.bitbucket.mesh.git.merge.GitMergeConflict;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcMergeConflicts;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.protobuf.ByteString;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.Set;

import static java.util.Objects.requireNonNull;

public class MergeConflictedException extends MergeException {

    private final Set<String> binaryPaths;
    private final List<GitMergeConflict> conflicts;

    public MergeConflictedException(@Nonnull String message, @Nonnull List<GitMergeConflict> conflicts,
                                    @Nonnull Set<String> binaryPaths) {
        super(message);

        this.binaryPaths = ImmutableSet.copyOf(requireNonNull(binaryPaths, "binaryPaths"));
        this.conflicts = ImmutableList.copyOf(requireNonNull(conflicts, "conflicts"));
    }

    @Nonnull
    public RpcMergeConflicts toMergeConflicts() {
        return RpcMergeConflicts.newBuilder()
                .addAllBinaryPaths(Iterables.transform(binaryPaths, ByteString::copyFromUtf8))
                .addAllConflicts(Iterables.transform(conflicts, GitMergeConflict::toMergeConflict))
                .setFromCommitId(fromCommitId)
                .build();
    }
}
