package com.hotent.assembly.mockmvc.conf;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import com.hotent.assembly.mockmvc.MockMvcBeanPostProcessor;
import com.hotent.assembly.mockmvc.MockMvcContextBeanPostProcessor;
import com.hotent.assembly.mockmvc.MockMvcFeignBuilder;
import com.hotent.assembly.mockmvc.MockMvcFeignObjectWrapper;

import feign.Client;
import feign.Feign;

/**
 * 自定义一套 FeignClient 来实现多个微服务合并为一个微服务
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Client.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class MockMvcFeignClienConf {
	@Bean
	MockMvcBeanPostProcessor mockMvcBeanPostProcessor(MockMvcFeignObjectWrapper mockMvcFeignObjectWrapper) {
		return new MockMvcBeanPostProcessor(mockMvcFeignObjectWrapper);
	}

	@Bean
	MockMvcContextBeanPostProcessor mockMvcContextBeanPostProcessor(BeanFactory beanFactory) {
		return new MockMvcContextBeanPostProcessor(beanFactory);
	}

	@Bean
	MockMvcFeignObjectWrapper mockMvcFeignObjectWrapper(ApplicationContext applicationContext) {
		return new MockMvcFeignObjectWrapper(applicationContext);
	}

	@Bean
	@ConditionalOnMissingBean
	@Scope("prototype")
	Feign.Builder feignBuilder(ApplicationContext applicationContext) {
		return MockMvcFeignBuilder.builder(applicationContext);
	}
}
