package com.gyee.generation.util.dataadapter;

import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SslProvider;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;

import java.time.Duration;

@Component
public class MyWebClient {

    public final WebClient webClient;


    public MyWebClient(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder
                .clientConnector(new ReactorClientHttpConnector(
                        HttpClient.create()
                                .secure(sslContextSpec -> {
                                    sslContextSpec.sslContext(sslContext());
                                })
                                .responseTimeout(Duration.ofMillis(30000))  // 设置 Netty 的连接超时
                ))
                .baseUrl("https://10.220.1.5:5300")
                //                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) // 示例：添加默认头部
                .codecs(configurer -> {
                    // 根据您的 Spring WebFlux 版本，调整缓冲区大小的方式可能有所不同
                    // 这里假设您想要设置响应的缓冲区大小
                    configurer.defaultCodecs().maxInMemorySize(16 * 1024 * 1024); // 示例：调整最大内存中的缓冲区大小
                })
                .build();
    }

    @Bean("Latest")
    public WebClient getLatest() {
        WebClient webClient = WebClient.builder()
                .clientConnector(new ReactorClientHttpConnector(
                        HttpClient.create()
                                .secure(sslContextSpec -> {
                                    sslContextSpec.sslContext(sslContext());
                                })
                                .responseTimeout(Duration.ofMillis(30000))  // 设置 Netty 的连接超时
                ))
                .baseUrl("https://10.220.1.5:9011")
                //                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) // 示例：添加默认头部
                .codecs(configurer -> {
                    // 根据您的 Spring WebFlux 版本，调整缓冲区大小的方式可能有所不同
                    // 这里假设您想要设置响应的缓冲区大小
                    configurer.defaultCodecs().maxInMemorySize(16 * 1024 * 1024); // 示例：调整最大内存中的缓冲区大小
                })
                .build();
        return webClient;
    }


    private SslContext sslContext() {
        try {
            return SslContextBuilder.forClient()
                    .sslProvider(SslProvider.JDK)
                    .trustManager(InsecureTrustManagerFactory.INSTANCE)
                    .build();
        } catch (Exception e) {
            // 处理异常，这里简单抛出运行时异常
            throw new RuntimeException("Failed to create SSL context", e);
        }
    }


}
