package com.atlassian.bitbucket.mesh.client;

import com.atlassian.bitbucket.mesh.git.AbstractParameters;
import com.atlassian.bitbucket.mesh.network.MeshNode;
import com.atlassian.bitbucket.mesh.transaction.ReplicaTransactionCallback;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

public class ReplicateParameters extends AbstractParameters {

    private final ReplicaTransactionCallback callback;
    private final MeshNode leader;
    private final String transactionId;

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

        leader = builder.leader;
        transactionId = builder.transactionId;
        callback = requireNonNull(builder.callback, "callback");
    }

    /**
     * @return the callback that should be used to register the follower's write token for the subject repository
     */
    @Nonnull
    public ReplicaTransactionCallback getCallback() {
        return callback;
    }

    @Nonnull
    public MeshNode getLeader() {
        return leader;
    }

    @Nonnull
    public String getTransactionId() {
        return transactionId;
    }

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

        private final MeshNode leader;
        private final String transactionId;

        private ReplicaTransactionCallback callback;

        public Builder(@Nonnull MeshNode leader, @Nonnull String transactionId) {
            this.leader = requireNonNull(leader, "leader");
            this.transactionId = requireNonBlank(transactionId, "transactionId");
        }

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

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

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