package com.wgb.system.service.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.data.model.config.RedisConfig;
import com.data.model.config.SwaggerConfig;
import com.tool.data.external.client.LbsAmapClient;
import com.tool.data.external.client.WechatClient;
import com.tool.data.external.domain.model.WechatProperties;
import com.wgb.system.service.domain.request.WechatModelProperties;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Configuration
@Import(value ={SwaggerConfig.class, RedisConfig.class,GlobalDefultExceptionHandler.class})
@Order(1)
public class appConfig {

    @Bean
    public RestTemplate restTemplate(HttpComponentsClientHttpRequestFactory componentsClientHttpRequestFactory,
                                     List<HttpMessageConverter<?>> messageConverters) {
        RestTemplate restTemplate = new RestTemplate(componentsClientHttpRequestFactory);
        restTemplate.setMessageConverters(messageConverters);
        return restTemplate;
    }

    @Bean
    public HttpComponentsClientHttpRequestFactory componentsClientHttpRequestFactory(
            @Value("${rest.call.connect.timeout}") int connectTimeout,
            @Value("${rest.call.read.timeout}") int readTimeout){
        HttpClient httpClient;
        httpClient = HttpClients.custom().build();
        HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        httpComponentsClientHttpRequestFactory.setConnectTimeout(connectTimeout);
        httpComponentsClientHttpRequestFactory.setReadTimeout(readTimeout);
        return httpComponentsClientHttpRequestFactory;
    }

    @Bean
    @ConfigurationProperties(prefix = "wechat")
    public WechatProperties wechatProperties(){
        return new WechatProperties();
    }

    @Bean
    public WechatClient WechatClient(RestTemplate restTemplate, WechatProperties wechatProperties, RedisTemplate<String,Object> redisTemplate){
        return new WechatClient(restTemplate,wechatProperties,redisTemplate);
    }

    @Bean
    public LbsAmapClient lbsAmapClient(RestTemplate restTemplate, @Value("${lbs.amap.key}") String key){
        return new LbsAmapClient(restTemplate,key);
    }

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    @Bean
    @ConfigurationProperties(prefix = "wechat.pay")
    public WechatModelProperties wechatModelProperties(){
        return new WechatModelProperties();
    }
}
