package com.ym.mec.web;

import javax.servlet.Filter;

import com.huifu.adapay.model.MerConfig;
import com.ym.mec.biz.service.NotifyCallback;
import com.ym.mec.thirdparty.adapay.ConfigInit;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.client.RestTemplate;

import com.spring4all.swagger.EnableSwagger2Doc;
import com.ym.mec.common.filters.EmojiEncodingFilter;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients("com.ym.mec")
@MapperScan("com.ym.mec.biz.dal.dao")
@ComponentScan(basePackages = "com.ym.mec")
@Configuration
@EnableSwagger2Doc
@EnableAsync
public class WebApplication {

	public static void main(String[] args) {
		SpringApplication.run(WebApplication.class, args);
	}

	@Bean
	@LoadBalanced
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}

	/**
	 * 注册filter
	 * @return
	 */
	@Bean
	public FilterRegistrationBean<Filter> filterRegistrationBean() {
		FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<Filter>();
		// 注入过滤器
		registration.setFilter(new EmojiEncodingFilter());
		// 过滤器名称
		registration.setName("emojiEncodingFilter");
		// 拦截规则
		registration.addUrlPatterns("/*");
		// 过滤器顺序(值越小，优先级越高)
		registration.setOrder(1);
		return registration;
	}

	@Bean
	public static void startMqtt() {
		MerConfig merConfig = ConfigInit.merConfig;
		new NotifyCallback();
	}
}
