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

import com.atlassian.bitbucket.mesh.git.AbstractParameters;
import com.google.common.collect.ImmutableList;

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

public class BatchRescopePullRequestsParameters extends AbstractParameters {

    static final int DEFAULT_MAX_COMMITS_PER_UPDATE = 25;

    private final int maxCommitsPerUpdate;
    private final Integer maxMessageLength;
    private final List<PullRequestRescope> rescopes;

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

        maxCommitsPerUpdate = builder.maxCommitsPerUpdate;
        maxMessageLength = builder.maxMessageLength;
        rescopes = builder.rescopes.build();
    }

    public int getMaxCommitsPerUpdate() {
        return maxCommitsPerUpdate;
    }

    @Nonnull
    public OptionalInt getMaxMessageLength() {
        return maxMessageLength == null ? OptionalInt.empty() : OptionalInt.of(maxMessageLength);
    }

    @Nonnull
    public List<PullRequestRescope> getRescopes() {
        return rescopes;
    }

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

        private final ImmutableList.Builder<PullRequestRescope> rescopes;

        private int maxCommitsPerUpdate;
        private Integer maxMessageLength;

        public Builder() {
            maxCommitsPerUpdate = DEFAULT_MAX_COMMITS_PER_UPDATE;
            rescopes = ImmutableList.builder();
        }

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

        @Nonnull
        public Builder maxCommitsPerUpdate(int value) {
            maxCommitsPerUpdate = value;
            return this;
        }

        @Nonnull
        public Builder maxMessageLength(int value) {
            maxMessageLength = value;
            return this;
        }

        @Nonnull
        public Builder rescopes(@Nullable Iterable<PullRequestRescope> values) {
            addIf(Objects::nonNull, rescopes, values);
            return this;
        }

        @Nonnull
        public Builder rescopes(@Nullable PullRequestRescope value, PullRequestRescope... moreValues) {
            addIf(Objects::nonNull, rescopes, value, moreValues);
            return this;
        }

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