package gateway

import "time"

const defaultWalletBalanceCacheTTL = 120 * time.Second
const defaultRPCRequestTimeout = 15 * time.Second

// CacheSettings holds tunable gateway behaviour.
type CacheSettings struct {
	WalletBalanceCacheTTLSeconds int `yaml:"wallet_balance_ttl_seconds"`
	RPCRequestTimeoutSeconds     int `yaml:"rpc_request_timeout_seconds"`
}

func defaultSettings() CacheSettings {
	return CacheSettings{
		WalletBalanceCacheTTLSeconds: int(defaultWalletBalanceCacheTTL.Seconds()),
		RPCRequestTimeoutSeconds:     int(defaultRPCRequestTimeout.Seconds()),
	}
}

func (s CacheSettings) withDefaults() CacheSettings {
	if s.WalletBalanceCacheTTLSeconds <= 0 {
		s.WalletBalanceCacheTTLSeconds = int(defaultWalletBalanceCacheTTL.Seconds())
	}
	if s.RPCRequestTimeoutSeconds <= 0 {
		s.RPCRequestTimeoutSeconds = int(defaultRPCRequestTimeout.Seconds())
	}
	return s
}

func (s CacheSettings) walletBalanceCacheTTL() time.Duration {
	if s.WalletBalanceCacheTTLSeconds <= 0 {
		return defaultWalletBalanceCacheTTL
	}
	return time.Duration(s.WalletBalanceCacheTTLSeconds) * time.Second
}

func (s CacheSettings) rpcTimeout() time.Duration {
	if s.RPCRequestTimeoutSeconds <= 0 {
		return defaultRPCRequestTimeout
	}
	return time.Duration(s.RPCRequestTimeoutSeconds) * time.Second
}
