/*
 * Copyright 2013-2020 Signal Messenger, LLC
 * SPDX-License-Identifier: AGPL-3.0-only
 */

package org.whispersystems.textsecuregcm.configuration;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.annotations.VisibleForTesting;
import io.lettuce.core.resource.ClientResources;
import java.time.Duration;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import org.whispersystems.textsecuregcm.redis.FaultTolerantRedisCluster;

@JsonTypeName("default")
public class RedisClusterConfiguration implements FaultTolerantRedisClusterFactory {

  @JsonProperty
  @NotEmpty
  private String configurationUri;

  @JsonProperty
  @NotNull
  private Duration timeout = Duration.ofSeconds(1);

  @JsonProperty
  @NotNull
  @Valid
  private CircuitBreakerConfiguration circuitBreaker = new CircuitBreakerConfiguration();

  @JsonProperty
  @NotNull
  @Valid
  private RetryConfiguration retry = new RetryConfiguration();

  @VisibleForTesting
  void setConfigurationUri(final String configurationUri) {
    this.configurationUri = configurationUri;
  }

  public String getConfigurationUri() {
    return configurationUri;
  }

  public Duration getTimeout() {
    return timeout;
  }

  public CircuitBreakerConfiguration getCircuitBreakerConfiguration() {
    return circuitBreaker;
  }

  public RetryConfiguration getRetryConfiguration() {
    return retry;
  }

  @Override
  public FaultTolerantRedisCluster build(final String name, final ClientResources.Builder clientResourcesBuilder) {
    return new FaultTolerantRedisCluster(name, this, clientResourcesBuilder);
  }
}
