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

import com.atlassian.bitbucket.mesh.git.builder.diff.GitDiffRenames;
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 StreamPullRequestChangesParameters extends AbstractPullRequestParameters {

    private final int limit;
    private final Set<String> paths;
    private final GitDiffRenames renames;
    private final Integer renameThreshold;

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

        limit = builder.limit;
        paths = builder.paths.build();
        renames = builder.renames;
        renameThreshold = builder.renameThreshold;
    }

    public int getLimit() {
        return limit;
    }

    @Nonnull
    public Set<String> getPaths() {
        return paths;
    }

    @Nonnull
    public GitDiffRenames getRenames() {
        return renames;
    }

    public Integer getRenameThreshold() {
        return renameThreshold;
    }

    public static class Builder extends AbstractPullRequestBuilder<Builder, StreamPullRequestChangesParameters> {

        private final ImmutableSet.Builder<String> paths;

        private int limit;
        private GitDiffRenames renames;
        private Integer renameThreshold;

        public Builder(@Nonnull PullRequest pullRequest) {
            super(pullRequest);

            paths = ImmutableSet.builder();
            renames = GitDiffRenames.FIND_RENAMES;
        }

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

        @Nonnull
        public Builder limit(int limit) {
            this.limit = limit;

            return self();
        }

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

            return self();
        }

        @Nonnull
        public Builder renames(@Nonnull GitDiffRenames value) {
            renames = requireNonNull(value, "renames");

            return self();
        }

        @Nonnull
        public Builder renameThreshold(@Nullable Integer value) {
            renameThreshold = value;

            return self();
        }

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