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

import com.atlassian.bitbucket.mesh.git.ref.RefUpdate;
import com.atlassian.bitbucket.mesh.util.BuilderSupport;
import com.google.common.collect.ImmutableList;

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

import static java.util.Objects.requireNonNull;

public class PullRequestEffectiveDiff {

    private final String fromCommit;
    private final List<RefUpdate> refUpdates;
    private final String toCommit;

    private PullRequestEffectiveDiff(Builder builder) {
        fromCommit = builder.fromCommit;
        refUpdates = builder.refUpdates.build();
        toCommit = builder.toCommit;
    }

    @Nonnull
    public String getFromCommit() {
        return fromCommit;
    }

    @Nonnull
    public List<RefUpdate> getRefUpdates() {
        return refUpdates;
    }

    @Nonnull
    public String getToCommit() {
        return toCommit;
    }

    public static class Builder extends BuilderSupport {

        private final String fromCommit;
        private final ImmutableList.Builder<RefUpdate> refUpdates;
        private final String toCommit;

        public Builder(@Nonnull String fromCommit, @Nonnull String toCommit) {
            this.fromCommit = requireNonNull(fromCommit, "fromCommit");
            this.toCommit = requireNonNull(toCommit, "toCommit");

            refUpdates = ImmutableList.builder();
        }

        @Nonnull
        public PullRequestEffectiveDiff build() {
            return new PullRequestEffectiveDiff(this);
        }

        @Nonnull
        public Builder refUpdate(@Nonnull RefUpdate value) {
            refUpdates.add(requireNonNull(value, "refUpdate"));

            return this;
        }

        @Nonnull
        public Builder refUpdates(@Nonnull Iterable<RefUpdate> values) {
            addIf(Objects::nonNull, refUpdates, values);

            return this;
        }
    }
}
