package com.kxs.system.api.feign.config;

import cn.hutool.core.util.StrUtil;
import com.kxs.common.core.constant.SecurityConstants;
import com.kxs.common.core.constant.ServiceNameConstants;
import com.kxs.common.core.util.WebUtils;
import com.kxs.system.api.feign.*;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

import java.util.Optional;

/**
 * <p>
 * 当前服务的webClient配置 服务下的远程调用统一在此配置
 * </p>
 *
 * @author 没秃顶的码农
 * @date 2023/11/13
 */
@Configuration
@RequiredArgsConstructor
public class AdminFeignClientConfiguration {

	@Bean
	public WebClient oauthRequestInterceptor() {

		return WebClient.builder()
			// 给请求添加过滤器，添加自定义的认证头
			.filter((request, next) -> {
				ClientRequest.Builder filtered = ClientRequest.from(request);

				ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
					.getRequestAttributes();
				// 不是web请求不传递token
				if (requestAttributes != null) {
					HttpServletRequest httpServletRequest = requestAttributes.getRequest();
					String token = httpServletRequest.getHeader(HttpHeaders.AUTHORIZATION);
					if (!StrUtil.isBlank(token)) {
						// 传递token
						filtered.header(HttpHeaders.AUTHORIZATION, token);
					}
				}
				return next.exchange(filtered.build());
			})
			.baseUrl(ServiceNameConstants.ADMIN_SERVICE)
			.build();
	}

	/**
	 * 远程日志服务
	 * @param client 客户端
	 * @return 远程日志服务
	 */
	@Bean
	RemoteLogService remoteLogService(WebClient client) {
		HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();
		return factory.createClient(RemoteLogService.class);
	}

	/**
	 * 远程客户端
	 * @param client 客户端
	 * @return 远程日志服务
	 */
	@Bean
	RemoteClientDetailsService remoteClientDetailsService(WebClient client) {
		HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();
		return factory.createClient(RemoteClientDetailsService.class);
	}

	/**
	 * 用户
	 * @param client 客户端
	 * @return 远程用户服务
	 */
	@Bean
	RemoteUserService remoteUserService(WebClient client) {
		HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();
		return factory.createClient(RemoteUserService.class);
	}

	/**
	 * 字典
	 * @param client 客户端
	 * @return 字典
	 */
	@Bean
	RemoteDictService remoteDictService(WebClient client) {
		HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();
		return factory.createClient(RemoteDictService.class);
	}

	/**
	 * 参数
	 * @param client 客户端
	 * @return 参数
	 */
	@Bean
	RemoteParamService remoteParamService(WebClient client) {
		HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();
		return factory.createClient(RemoteParamService.class);
	}

}
