package com.atlassian.bitbucket.mesh.transaction;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

public class StartTransactionParameters {

    private final String id;
    private final TransactionLeader leader;

    private StartTransactionParameters(Builder builder) {
        id = builder.id;
        leader = requireNonNull(builder.leader, "leader");
        if (!leader.getNode().isLocal()) {
            checkArgument(isNotBlank(id), "A transaction ID must be provided if another node is the transaction leader");
        }
    }

    @Nullable
    public String getId() {
        return id;
    }

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

    public static class Builder {

        private final TransactionLeader leader;

        private String id;

        public Builder(@Nonnull TransactionLeader leader) {
            this.leader = requireNonNull(leader, "leader");
        }

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

        @Nonnull
        public Builder id(@Nullable String value) {
            id = value;
            return this;
        }
    }
}
