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;

public class GetMergeBaseParameters extends AbstractParameters {

    private final Set<Repository> alternates;
    private final Set<String> commitIds;
    private final Repository repository;
    private final boolean required;

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

        alternates = builder.alternates.build();
        commitIds = builder.commitIds.build();
        repository = builder.repository;
        required = builder.required;
    }

    @Nonnull
    public Set<Repository> getAlternates() {
        return alternates;
    }

    @Nonnull
    public Set<String> getCommitIds() {
        return commitIds;
    }

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

    public boolean isRequired() {
        return required;
    }

    public static class Builder extends AbstractBuilder<Builder, GetMergeBaseParameters> {

        private final ImmutableSet.Builder<Repository> alternates;
        private final ImmutableSet.Builder<String> commitIds;
        private final Repository repository;

        private boolean required;

        public Builder(Repository repository) {
            this.repository = repository;

            alternates = ImmutableSet.builder();
            commitIds = ImmutableSet.builder();
        }

        @Nonnull
        public Builder alternate(@Nullable Repository value) {
            addIf(Objects::nonNull, alternates, value);

            return this;
        }

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

        @Nonnull
        public Builder commitId(@Nullable String value) {
            addIf(NOT_BLANK, commitIds, value);

            return self();
        }

        @Nonnull
        public Builder commitIds(@Nullable Iterable<String> values) {
            addIf(NOT_BLANK, commitIds, values);

            return self();
        }

        @Nonnull
        public Builder required(boolean value) {
            required = value;

            return self();
        }

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