/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.common.config;

import com.crunii.micro.common.constants.CommonConstants;
import com.crunii.micro.common.spring.rest.RestBuilder;
import com.crunii.micro.common.spring.rest.RestConfig;
import com.crunii.micro.common.spring.rest.TimeoutRestTemplate;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     所有依赖注入采用名字进行@Resource(name = "xxxName")
 *     所有的http调用内部和外部的都采用这个
 *     一共10个RestTemplate 分为
 *     1 内部调用不记录日志：noLogApiRestTemplate 或者 noLogApiTimeoutRestTemplate
 *     2 内部调用记录日志：logApiRestTemplate 或者 logApiTimeoutRestTemplate
 *     3 外部调用记录日志：logRestTemplate 或者 logTimeoutRestTemplate
 *     4 外部调用不记录日志：noLogRestTemplate 或者 noLogTimeoutRestTemplate
 *     5 外部公网调用记录日志：logRestTemplate 或者 logTimeoutRestTemplate
 *
 *     http代理访问公网
 *     1.引入http-proxy.yml配置文件
 *     2.通过@Resource引用：noLogInternetRestTemplate 或者 logInternetRestTemplate
 * </pre>
 *
 * @author 田平 create 2018年5月29日下午3:04:37
 */
@Configuration
public class RestTemplateConfiguration {

	private static final String INTERNET_PROXY_KEY_NAME = "http-proxy.internet";

	@Autowired
	private Environment env;

	@Resource(name = "objectMapper")
	private ObjectMapper objectMapper;

	@Bean(name = "noLogApiRestTemplate")
	@Primary
	@LoadBalanced
	@DependsOn("springUtil")
	public RestTemplate regNoLogApiRestTemplate() {
		List<Header> headers = new ArrayList<>();
		headers.add(new BasicHeader(CommonConstants.HEADER_IS_NEED_AUTH_URL, "false"));
		RestConfig restConfig = RestConfig.builder()
				.mapper(objectMapper)
				.defaultHeaders(headers)
				.build();

		return RestBuilder.buildRestTemplate(restConfig);
	}

	@Bean(name = "logApiRestTemplate")
	@LoadBalanced
	@DependsOn("springUtil")
	public RestTemplate regLogApiRestTemplate() {
		List<Header> headers = new ArrayList<>();
		headers.add(new BasicHeader(CommonConstants.HEADER_IS_NEED_AUTH_URL, "false"));
		RestConfig restConfig = RestConfig.builder()
				.mapper(objectMapper)
				.defaultHeaders(headers)
				.enabledLog(true)
				.build();

		return RestBuilder.buildRestTemplate(restConfig);
	}

	@Bean(name = "noLogApiTimeoutRestTemplate")
	@LoadBalanced
	@DependsOn("springUtil")
	public TimeoutRestTemplate regNoLogApiTimeoutRestTemplate() {
		List<Header> headers = new ArrayList<>();
		headers.add(new BasicHeader(CommonConstants.HEADER_IS_NEED_AUTH_URL, "false"));
		RestConfig restConfig = RestConfig.builder()
				.mapper(objectMapper)
				.defaultHeaders(headers)
				.build();

		return RestBuilder.buildTimeoutRestTemplate(restConfig);
	}

	@Bean(name = "logApiTimeoutRestTemplate")
	@LoadBalanced
	@DependsOn("springUtil")
	public TimeoutRestTemplate regLogApiTimeoutRestTemplate() {
		List<Header> headers = new ArrayList<>();
		headers.add(new BasicHeader(CommonConstants.HEADER_IS_NEED_AUTH_URL, "false"));
		RestConfig restConfig = RestConfig.builder()
				.mapper(objectMapper)
				.enabledLog(true)
				.defaultHeaders(headers)
				.build();

		return RestBuilder.buildTimeoutRestTemplate(restConfig);
	}

	@Bean(name = "noLogRestTemplate")
	@DependsOn("springUtil")
	public RestTemplate regNoLogRestTemplate() {
		RestConfig restConfig = RestConfig.builder()
				.mapper(objectMapper)
				.build();
		return RestBuilder.buildRestTemplate(restConfig);
	}

	@Bean(name = "logRestTemplate")
	@DependsOn("springUtil")
	public RestTemplate regLogRestTemplate() {
		RestConfig restConfig = RestConfig.builder()
				.mapper(objectMapper)
				.enabledLog(true)
				.build();
		return RestBuilder.buildRestTemplate(restConfig);
	}

	@ConditionalOnProperty(name = INTERNET_PROXY_KEY_NAME)
	@Bean(name = "noLogInternetRestTemplate")
	@DependsOn("springUtil")
	public RestTemplate regNoLogInternetRestTemplate() {
		RestConfig restConfig = RestConfig.builder()
				.mapper(objectMapper)
				.proxyServerAddress(env.getProperty(INTERNET_PROXY_KEY_NAME))
				.build();
		return RestBuilder.buildRestTemplate(restConfig);
	}

	@ConditionalOnProperty(name = INTERNET_PROXY_KEY_NAME)
	@Bean(name = "logInternetRestTemplate")
	@DependsOn("springUtil")
	public RestTemplate regLogInternetRestTemplate() {
		RestConfig restConfig = RestConfig.builder()
				.mapper(objectMapper)
				.enabledLog(true)
				.proxyServerAddress(env.getProperty(INTERNET_PROXY_KEY_NAME))
				.build();
		return RestBuilder.buildRestTemplate(restConfig);
	}

	@Bean(name = "noLogTimeoutRestTemplate")
	@DependsOn("springUtil")
	public TimeoutRestTemplate regNoLogTimeoutRestTemplate() {
		RestConfig restConfig = RestConfig.builder()
				.mapper(objectMapper)
				.build();
		return RestBuilder.buildTimeoutRestTemplate(restConfig);
	}

	@Bean(name = "logTimeoutRestTemplate")
	@DependsOn("springUtil")
	public TimeoutRestTemplate regLogTimeoutRestTemplate() {
		RestConfig restConfig = RestConfig.builder()
				.mapper(objectMapper)
				.enabledLog(true)
				.build();
		return RestBuilder.buildTimeoutRestTemplate(restConfig);
	}

}
