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

import com.atlassian.bitbucket.mesh.git.AbstractParameters;
import com.atlassian.bitbucket.mesh.repository.Repository;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcGitTimeouts;

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

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

public class BatchChangesParameters extends AbstractParameters {

    private final Repository repository;
    private final String sinceCommitId;
    private final String untilCommitId;

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

        repository = builder.repository;
        sinceCommitId = builder.sinceCommitId;
        untilCommitId = builder.untilCommitId;
    }

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

    /**
     * @return optional ID of the "since" commit; if specified only changes since that commit ID are streamed,
     * otherwise all changes are streamed
     */
    @Nonnull
    public Optional<String> getSinceCommitId() {
        return ofNullable(sinceCommitId);
    }

    /**
     * @return ID of the commit whose changes should be streamed
     */
    @Nonnull
    public String getUntilCommitId() {
        return untilCommitId;
    }

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

        private final Repository repository;

        private String sinceCommitId;
        private String untilCommitId;

        public Builder(@Nonnull Repository repository, @Nonnull RpcGitTimeouts timeouts) {
            super(timeouts);

            this.repository = requireNonNull(repository, "repository");
        }

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

        @Nonnull
        public Builder sinceCommitId(@Nullable String value) {
            sinceCommitId = value;
            return self();
        }

        @Nonnull
        public Builder untilCommitId(@Nonnull String value) {
            untilCommitId = requireNonNull(value, "untilCommitId");
            return self();
        }

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