package org.ssssssss.magicapi.redis;



import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.time.Duration;
/**
 *  MagicRedisProperties
 * @since 3.0.0
 * @author 冰点
 */

@Slf4j
@Data
@Accessors(chain = true)
public class MagicRedisProperties {

    /**
     * 连接类型
     */
    private String type;

    /**
     * redis主机地址，ip：port，多个用逗号(,)分隔
     */
    private String address = "localhost:6379";

    /**
     * 用户名
     */
    private String username;

    /**
     * 登录密码
     */
    private String password;

    /**
     * 数据库(默认0)
     */
    private int database = 0;

    /**
     * 连接超时时间，默认3S
     */
    private Duration timeout = Duration.ofMillis(3000L);

    /**
     * 哨兵时需要指定master Name
     */
    private Sentinel sentinel = new Sentinel();

    /**
     * 默认使用lettuce
     */
    private Lettuce lettuce = new Lettuce();

    /**
     * 重试次数
     */
    private int maxRedirects = 5;

    /**
     * 哨兵模式
     */
    @Data
    public static class Sentinel {
        private String master;
    }

    /**
     * Lettuce client配置
     */
    @Data
    public static class Lettuce {

        /**
         * Shutdown timeout.
         */
        private Duration shutdownTimeout = Duration.ofMillis(100);

        /**
         * Lettuce pool configuration.
         */
        private MagicRedisProperties.Pool pool = new Pool();

        /**
         * setShutdownTimeout
         */
        public void setShutdownTimeout(long shutdownTimeout) {
            this.shutdownTimeout = Duration.ofMillis(shutdownTimeout);
        }
    }

    /**
     * 连接池属性
     */
    @Data
    public static class Pool {
        /**
         * 最大空闲
         */
        private int maxIdle = 8;

        /**
         * 最小空闲
         */
        private int minIdle = 0;

        /**
         * 最大存活
         */
        private int maxActive = 8;

        /**
         * 最长等待
         */
        private Duration maxWait = Duration.ofMillis(-1);

        /**
         * long转Duration
         */
        public void setMaxWait(long maxWait) {
            this.maxWait = Duration.ofMillis(maxWait);
        }
    }
}
