package com.atlassian.stash.internal.pull;

import com.atlassian.bitbucket.pull.PullRequestParticipantSearchRequest;
import com.atlassian.bitbucket.pull.PullRequestRole;
import com.atlassian.stash.internal.InternalConverter;
import com.atlassian.stash.internal.repository.InternalRepository;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Optional;

import static java.util.Objects.requireNonNull;
import static java.util.Optional.ofNullable;

/**
 * @since 4.2
 */
public class PullRequestParticipantSearchCriteria {

    private final InternalRepository fromRepository;
    private final PullRequestRole role;
    private final InternalRepository toRepository;

    private PullRequestParticipantSearchCriteria(Builder builder) {
        fromRepository = builder.fromRepository;
        role = builder.role;
        toRepository = builder.toRepository;
    }

    @Nonnull
    public Optional<InternalRepository> getFromRepository() {
        return ofNullable(fromRepository);
    }

    @Nonnull
    public Optional<PullRequestRole> getRole() {
        return ofNullable(role);
    }

    @Nonnull
    public Optional<InternalRepository> getToRepository() {
        return ofNullable(toRepository);
    }

    public static class Builder {

        private InternalRepository fromRepository;
        private PullRequestRole role;
        private InternalRepository toRepository;

        public Builder() {
        }

        public Builder(@Nonnull PullRequestParticipantSearchRequest searchRequest) {
            fromRepository = requireNonNull(searchRequest, "searchRequest").getFromRepository()
                    .map(InternalConverter::convertToInternalRepository)
                    .orElse(null);
            role = searchRequest.getRole()
                    .orElse(null);
            toRepository = searchRequest.getToRepository()
                    .map(InternalConverter::convertToInternalRepository)
                    .orElse(null);
        }

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

        @Nonnull
        public Builder fromRepository(@Nullable InternalRepository value) {
            fromRepository = value;
            return this;
        }

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

        @Nonnull
        public Builder toRepository(@Nullable InternalRepository value) {
            toRepository = value;
            return this;
        }
    }
}
