package com.kxs.gateway.api.config;

import cn.hutool.crypto.symmetric.AES;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kxs.gateway.api.filter.RequestGlobalFilter;
import com.kxs.gateway.api.filter.RequestDecoderFilter;
import com.kxs.gateway.api.filter.ValidateCodeGatewayFilter;
import com.kxs.gateway.api.handler.GlobalExceptionHandler;
import com.kxs.gateway.api.handler.ImageCodeHandler;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 网关配置
 *
 * @author Lxq
 * @date 2023-11-11
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(GatewayConfigProperties.class)
public class GatewayConfiguration {

	@Bean
	public RequestGlobalFilter requestGlobalFilter() {
		return new RequestGlobalFilter();
	}

	@Bean
	public RequestDecoderFilter requestDecoderFilter(AES cryptoAes) {
		return new RequestDecoderFilter(cryptoAes);
	}

	@Bean
	public ValidateCodeGatewayFilter validateCodeGatewayFilter(GatewayConfigProperties configProperties,
			ObjectMapper objectMapper, RedisTemplate<String, Object> redisTemplate) {
		return new ValidateCodeGatewayFilter(configProperties, objectMapper, redisTemplate);
	}

	@Bean
	public GlobalExceptionHandler globalExceptionHandler(ObjectMapper objectMapper) {
		return new GlobalExceptionHandler(objectMapper);
	}

	@Bean
	public ImageCodeHandler imageCodeHandler(RedisTemplate<String, Object> redisTemplate) {
		return new ImageCodeHandler(redisTemplate);
	}

}
