package redis

import (
	
	
	
	
)

// UniversalOptions information is required by UniversalClient to establish
// connections.
type UniversalOptions struct {
	// Either a single address or a seed list of host:port addresses
	// of cluster/sentinel nodes.
	Addrs []string

	// ClientName will execute the `CLIENT SETNAME ClientName` command for each conn.
	ClientName string

	// Database to be selected after connecting to the server.
	// Only single-node and failover clients.
	DB int

	// Common options.

	Dialer    func(ctx context.Context, network, addr string) (net.Conn, error)
	OnConnect func(ctx context.Context, cn *Conn) error

	Username         string
	Password         string
	SentinelUsername string
	SentinelPassword string

	MaxRetries      int
	MinRetryBackoff time.Duration
	MaxRetryBackoff time.Duration

	DialTimeout           time.Duration
	ReadTimeout           time.Duration
	WriteTimeout          time.Duration
	ContextTimeoutEnabled bool

	// PoolFIFO uses FIFO mode for each node connection pool GET/PUT (default LIFO).
	PoolFIFO bool

	PoolSize        int
	PoolTimeout     time.Duration
	MinIdleConns    int
	MaxIdleConns    int
	ConnMaxIdleTime time.Duration
	ConnMaxLifetime time.Duration

	TLSConfig *tls.Config

	// Only cluster clients.

	MaxRedirects   int
	ReadOnly       bool
	RouteByLatency bool
	RouteRandomly  bool

	// The sentinel master name.
	// Only failover clients.

	MasterName string
}

// Cluster returns cluster options created from the universal options.
func ( *UniversalOptions) () *ClusterOptions {
	if len(.Addrs) == 0 {
		.Addrs = []string{"127.0.0.1:6379"}
	}

	return &ClusterOptions{
		Addrs:      .Addrs,
		ClientName: .ClientName,
		Dialer:     .Dialer,
		OnConnect:  .OnConnect,

		Username: .Username,
		Password: .Password,

		MaxRedirects:   .MaxRedirects,
		ReadOnly:       .ReadOnly,
		RouteByLatency: .RouteByLatency,
		RouteRandomly:  .RouteRandomly,

		MaxRetries:      .MaxRetries,
		MinRetryBackoff: .MinRetryBackoff,
		MaxRetryBackoff: .MaxRetryBackoff,

		DialTimeout:           .DialTimeout,
		ReadTimeout:           .ReadTimeout,
		WriteTimeout:          .WriteTimeout,
		ContextTimeoutEnabled: .ContextTimeoutEnabled,

		PoolFIFO: .PoolFIFO,

		PoolSize:        .PoolSize,
		PoolTimeout:     .PoolTimeout,
		MinIdleConns:    .MinIdleConns,
		MaxIdleConns:    .MaxIdleConns,
		ConnMaxIdleTime: .ConnMaxIdleTime,
		ConnMaxLifetime: .ConnMaxLifetime,

		TLSConfig: .TLSConfig,
	}
}

// Failover returns failover options created from the universal options.
func ( *UniversalOptions) () *FailoverOptions {
	if len(.Addrs) == 0 {
		.Addrs = []string{"127.0.0.1:26379"}
	}

	return &FailoverOptions{
		SentinelAddrs: .Addrs,
		MasterName:    .MasterName,
		ClientName:    .ClientName,

		Dialer:    .Dialer,
		OnConnect: .OnConnect,

		DB:               .DB,
		Username:         .Username,
		Password:         .Password,
		SentinelUsername: .SentinelUsername,
		SentinelPassword: .SentinelPassword,

		MaxRetries:      .MaxRetries,
		MinRetryBackoff: .MinRetryBackoff,
		MaxRetryBackoff: .MaxRetryBackoff,

		DialTimeout:           .DialTimeout,
		ReadTimeout:           .ReadTimeout,
		WriteTimeout:          .WriteTimeout,
		ContextTimeoutEnabled: .ContextTimeoutEnabled,

		PoolFIFO:        .PoolFIFO,
		PoolSize:        .PoolSize,
		PoolTimeout:     .PoolTimeout,
		MinIdleConns:    .MinIdleConns,
		MaxIdleConns:    .MaxIdleConns,
		ConnMaxIdleTime: .ConnMaxIdleTime,
		ConnMaxLifetime: .ConnMaxLifetime,

		TLSConfig: .TLSConfig,
	}
}

// Simple returns basic options created from the universal options.
func ( *UniversalOptions) () *Options {
	 := "127.0.0.1:6379"
	if len(.Addrs) > 0 {
		 = .Addrs[0]
	}

	return &Options{
		Addr:       ,
		ClientName: .ClientName,
		Dialer:     .Dialer,
		OnConnect:  .OnConnect,

		DB:       .DB,
		Username: .Username,
		Password: .Password,

		MaxRetries:      .MaxRetries,
		MinRetryBackoff: .MinRetryBackoff,
		MaxRetryBackoff: .MaxRetryBackoff,

		DialTimeout:           .DialTimeout,
		ReadTimeout:           .ReadTimeout,
		WriteTimeout:          .WriteTimeout,
		ContextTimeoutEnabled: .ContextTimeoutEnabled,

		PoolFIFO:        .PoolFIFO,
		PoolSize:        .PoolSize,
		PoolTimeout:     .PoolTimeout,
		MinIdleConns:    .MinIdleConns,
		MaxIdleConns:    .MaxIdleConns,
		ConnMaxIdleTime: .ConnMaxIdleTime,
		ConnMaxLifetime: .ConnMaxLifetime,

		TLSConfig: .TLSConfig,
	}
}

// --------------------------------------------------------------------

// UniversalClient is an abstract client which - based on the provided options -
// represents either a ClusterClient, a FailoverClient, or a single-node Client.
// This can be useful for testing cluster-specific applications locally or having different
// clients in different environments.
type UniversalClient interface {
	Cmdable
	AddHook(Hook)
	Watch(ctx context.Context, fn func(*Tx) error, keys ...string) error
	Do(ctx context.Context, args ...interface{}) *Cmd
	Process(ctx context.Context, cmd Cmder) error
	Subscribe(ctx context.Context, channels ...string) *PubSub
	PSubscribe(ctx context.Context, channels ...string) *PubSub
	SSubscribe(ctx context.Context, channels ...string) *PubSub
	Close() error
	PoolStats() *PoolStats
}

var (
	_ UniversalClient = (*Client)(nil)
	_ UniversalClient = (*ClusterClient)(nil)
	_ UniversalClient = (*Ring)(nil)
)

// NewUniversalClient returns a new multi client. The type of the returned client depends
// on the following conditions:
//
// 1. If the MasterName option is specified, a sentinel-backed FailoverClient is returned.
// 2. if the number of Addrs is two or more, a ClusterClient is returned.
// 3. Otherwise, a single-node Client is returned.
func ( *UniversalOptions) UniversalClient {
	if .MasterName != "" {
		return NewFailoverClient(.Failover())
	} else if len(.Addrs) > 1 {
		return NewClusterClient(.Cluster())
	}
	return NewClient(.Simple())
}