package cn.sh.stc.sict.cloud.common.gateway.config;

import cn.sh.stc.sict.cloud.common.gateway.handler.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;

/**
 * @Description 路由配置信息
 * @Author
 * @Date
 */
@Slf4j
@Configuration
@AllArgsConstructor
public class RouterFunctionConfiguration {
    private final HystrixFallbackHandler hystrixFallbackHandler;
    private final SwaggerResourceHandler swaggerResourceHandler;
    private final SwaggerSecurityHandler swaggerSecurityHandler;
    private final SwaggerUiHandler swaggerUiHandler;
    private final ImageCodeHandler imageCodeHandler;
    private final SmsCodeHandler smsCodeHandler;
    private final CurrentDateHandler currentDateHandler;

    @Bean
    public RouterFunction routerFunction() {
        // 当前系统时间
        // 图形验证码
        // 短信验证码
        // swagger
        return RouterFunctions.route(
                RequestPredicates.path("/fallback")
                        .and(RequestPredicates.accept(MediaType.APPLICATION_JSON_UTF8)), hystrixFallbackHandler)
                .andRoute(RequestPredicates.GET("/currentDate")
                        .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), currentDateHandler)
                .andRoute(RequestPredicates.GET("/img/code")
                        .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), imageCodeHandler)
                .andRoute(RequestPredicates.GET("/sms/code")
                        .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), smsCodeHandler)
                .andRoute(RequestPredicates.GET("/swagger-resources")
                        .and(RequestPredicates.accept(MediaType.ALL)), swaggerResourceHandler)
                .andRoute(RequestPredicates.GET("/swagger-resources/configuration/ui")
                        .and(RequestPredicates.accept(MediaType.ALL)), swaggerUiHandler)
                .andRoute(RequestPredicates.GET("/swagger-resources/configuration/security")
                        .and(RequestPredicates.accept(MediaType.ALL)), swaggerSecurityHandler);

    }

}