package com.face.global.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
@EnableConfigurationProperties(HttpClientConfig.ProxyConfigProperties.class)
public class HttpClientConfig {

    @Autowired
    private ProxyConfigProperties properties;

    @Bean
    public RestTemplate proxyRestTemplate() {
        return new RestTemplate(
                new BufferingClientHttpRequestFactory(new HttpComponentsClientHttpRequestFactory(
                        HttpClientFactory.getHttpClient(properties.getProxy(),
                                properties.getClient()))));
    }

    @Bean
    public RestTemplate noProxyRestTemplate() {
        return new RestTemplate(
                new BufferingClientHttpRequestFactory(new HttpComponentsClientHttpRequestFactory(
                        HttpClientFactory.getHttpClient(null,
                                properties.getClient()))));
    }

    @ConfigurationProperties(value = "app.http.default")
    @Data
    public static class ProxyConfigProperties {

        private ProxyProperties proxy;

        private HttpClientProperties client;
    }
}
