package com.atlassian.stash.internal.pull;

import com.atlassian.bitbucket.pull.PullRequestParticipantStatus;
import com.atlassian.bitbucket.pull.PullRequestRole;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.atlassian.bitbucket.util.BuilderSupport;
import com.google.common.collect.ImmutableSet;

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

import static com.google.common.base.Preconditions.checkNotNull;

public class PullRequestParticipantCriteria {

    private final PullRequestRole role;
    private final Set<PullRequestParticipantStatus> statuses;
    private final ApplicationUser user;

    public PullRequestParticipantCriteria(Builder builder) {
        statuses = ImmutableSet.copyOf(builder.statuses);
        role = builder.role;
        user = builder.user;
    }

    @Nonnull
    public Set<PullRequestParticipantStatus> getStatuses() {
        return statuses;
    }

    @Nullable
    public PullRequestRole getRole() {
        return role;
    }

    @Nonnull
    public ApplicationUser getUser() {
        return user;
    }

    public static class Builder extends BuilderSupport {

        private final EnumSet<PullRequestParticipantStatus> statuses;
        private final ApplicationUser user;

        private PullRequestRole role;

        public Builder(@Nonnull ApplicationUser user) {
            this.user = checkNotNull(user, "user");
            statuses = EnumSet.noneOf(PullRequestParticipantStatus.class);
        }

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

        @Nonnull
        public Builder role(@Nullable PullRequestRole value) {
            role = value;
            return this;
        }

        @Nonnull
        public Builder status(@Nullable PullRequestParticipantStatus value) {
            addIf(Objects::nonNull, statuses, value);
            return this;
        }

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