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

import com.atlassian.bitbucket.mesh.git.AbstractParameters;
import com.atlassian.bitbucket.mesh.repository.Repository;
import com.google.common.collect.ImmutableSet;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Objects;
import java.util.Set;

import static java.util.Objects.requireNonNull;

public class ResolveCommitsParameters extends AbstractParameters {

    private final Set<String> commitishes;
    private final Repository repository;

    private ResolveCommitsParameters(Builder builder) {
        super(builder);

        commitishes = builder.commitishes.build();
        repository = builder.repository;
    }

    @Nonnull
    public Set<String> getCommitishes() {
        return commitishes;
    }

    @Nonnull
    public Repository getRepository() {
        return repository;
    }

    public static class Builder extends AbstractParameters.AbstractBuilder<Builder, ResolveCommitsParameters> {

        private final ImmutableSet.Builder<String> commitishes;
        private final Repository repository;

        public Builder(@Nonnull Repository repository) {
            this.repository = requireNonNull(repository, "repository");
            commitishes = ImmutableSet.builder();
        }

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

        @Nonnull
        public Builder commitishes(@Nullable String value, @Nullable String... moreValues) {
            addIf(Objects::nonNull, commitishes, value, moreValues);
            return self();
        }

        @Nonnull
        public Builder commitishes(@Nullable Iterable<String> values) {
            addIf(Objects::nonNull, commitishes, values);
            return self();
        }

        @Nonnull
        @Override
        protected Builder self() {
            return this;
        }
    }
}
