package gaf3.core.gateway.bean

import org.slf4j.LoggerFactory
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.annotation.Order
import org.springframework.http.HttpStatus
import org.springframework.web.server.WebFilter

@Configuration
class WebFilterBean {
//    @Bean
//    @Order(-1)
    fun apiAccessFilter(): WebFilter {
        return WebFilter { exchange, chain ->
            log.debug("[ApiAccess] start...")
            val path = exchange.request.path.value()
            log.debug("[ApiAccess] path : {}", path)
            if(path.startsWith("/api")) {
                chain.filter(exchange.mutate().build())
            } else {
                ServerWebExchangeUtils.setResponseStatus(exchange, HttpStatus.FORBIDDEN)
                exchange.response.setComplete()
            }
        }
    }

//    @Bean
//    @Order(-1)
//    fun hostToAppFilter(): WebFilter {
//        return WebFilter { exchange, chain ->
//            log.debug("[HostToApp] start...")
//            // val host = exchange.request.headers.getFirst("Host")
//            var host = exchange.request.headers.getFirst("X-Forwarded-Host")
//            if (host == null || StringUtils.isEmpty(host))
//                host = exchange.request.headers.getFirst("Host")
//            host = host!!.toLowerCase()
//            val p = Pattern.compile("^([a-z0-9]+)\\.smart.*")
//            val m = p.matcher(host)
//            if (!m.matches()) {
//                HostToAppFilter.log.debug("Not matches: {}", host)
//                return@WebFilter chain.filter(exchange.mutate().build())
//            }
//            var app: String = m.group(1)
//            if (app !in arrayOf("hr", "wx", "plat")) {
//                app = "www" // 默认应用
//            }
//            val request = exchange.request.mutate()
//                    .headers { httpHeaders ->
//                        httpHeaders[HEADER_APP] = app
//                    }.build()
//            log.debug("[HostToApp] Set X-App : {}", app)
//            chain.filter(exchange.mutate().request(request).build())
//        }
//    }

    @Bean
    @Order(-1000)
    fun cleanHeaderFilter(): WebFilter {
        return WebFilter { exchange, chain ->
            val request = exchange.request.mutate()
                    .headers { httpHeaders ->
                        httpHeaders.remove(HEADER_APP)
                        httpHeaders.remove(HEADER_ROLE)
                        httpHeaders.remove(HEADER_TAGS)
                        httpHeaders.remove(HEADER_USERID)
                        httpHeaders.remove(HEADER_USERNAME)
//                        httpHeaders.remove(HEADER_CORPID)
//                        httpHeaders.remove(HEADER_CORPNAME)
//                        httpHeaders.remove(HEADER_SCHID)
//                        httpHeaders.remove(HEADER_SCHNAME)
                    }.build()
            log.debug("[CleanHeader] ok")
            chain.filter(exchange.mutate().request(request).build())
        }
    }
    companion object {
        const val HEADER_APP = "X-App"
        const val HEADER_ROLE = "X-Role"
        const val HEADER_TAGS = "X-Tags"
        const val HEADER_USERID = "X-UserID"
        const val HEADER_USERNAME = "X-UserName"
//        const val HEADER_CORPID = "X-CorpID"
//        const val HEADER_CORPNAME = "X-CorpName"
//        const val HEADER_SCHID = "X-SchID"
//        const val HEADER_SCHNAME = "X-SchName"
        internal val log = LoggerFactory.getLogger("GatewayWebFilter")
    }
}
