package cn.iocoder.byzs.framework.web.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.time.Duration;

/**
 * WebClient 配置属性
 *
 * @author fansili
 */
@ConfigurationProperties(prefix = "byzs.web.web-client")
public class WebClientProperties {

    /**
     * 连接超时时间
     */
    private Duration connectTimeout = Duration.ofSeconds(15);

    /**
     * 读取超时时间
     */
    private Duration readTimeout = Duration.ofSeconds(30);

    /**
     * 写入超时时间
     */
    private Duration writeTimeout = Duration.ofSeconds(30);

    /**
     * 连接池配置
     */
    private Pool pool = new Pool();

    /**
     * 重试配置
     */
    private Retry retry = new Retry();

    public Duration getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(Duration connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public Duration getReadTimeout() {
        return readTimeout;
    }

    public void setReadTimeout(Duration readTimeout) {
        this.readTimeout = readTimeout;
    }

    public Duration getWriteTimeout() {
        return writeTimeout;
    }

    public void setWriteTimeout(Duration writeTimeout) {
        this.writeTimeout = writeTimeout;
    }

    public Pool getPool() {
        return pool;
    }

    public void setPool(Pool pool) {
        this.pool = pool;
    }

    public Retry getRetry() {
        return retry;
    }

    public void setRetry(Retry retry) {
        this.retry = retry;
    }

    /**
     * 连接池配置
     */
    public static class Pool {

        /**
         * 最大连接数
         */
        private int maxConnections = 50;

        /**
         * 最大空闲时间
         */
        private Duration maxIdleTime = Duration.ofMinutes(1);

        /**
         * 最大生命周期
         */
        private Duration maxLifeTime = Duration.ofMinutes(5);

        public int getMaxConnections() {
            return maxConnections;
        }

        public void setMaxConnections(int maxConnections) {
            this.maxConnections = maxConnections;
        }

        public Duration getMaxIdleTime() {
            return maxIdleTime;
        }

        public void setMaxIdleTime(Duration maxIdleTime) {
            this.maxIdleTime = maxIdleTime;
        }

        public Duration getMaxLifeTime() {
            return maxLifeTime;
        }

        public void setMaxLifeTime(Duration maxLifeTime) {
            this.maxLifeTime = maxLifeTime;
        }
    }

    /**
     * 重试配置
     */
    public static class Retry {

        /**
         * 是否启用重试
         */
        private boolean enabled = true;

        /**
         * 最大重试次数
         */
        private int maxAttempts = 3;

        /**
         * 重试间隔时间
         */
        private Duration backoff = Duration.ofMillis(100);

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public int getMaxAttempts() {
            return maxAttempts;
        }

        public void setMaxAttempts(int maxAttempts) {
            this.maxAttempts = maxAttempts;
        }

        public Duration getBackoff() {
            return backoff;
        }

        public void setBackoff(Duration backoff) {
            this.backoff = backoff;
        }
    }

}