package com.bjtdba.gateWay.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import static com.bjtdba.utils.Base64Converter.decode;
import static com.bjtdba.utils.DESUtil.decrypt;

/**
 * 自定一个全局过滤器
 */
@Component
public class LoginFilter implements GlobalFilter, Ordered {

    private final AntPathMatcher pathMatcher = new AntPathMatcher();

    private static final List<String> EXTERNAL_PATHS = Collections.singletonList(
            "/external/**"
    );
    /**
     * 业务方法用于加密
     * 1、
     * @param exchange
     * @param chain
     * @return
     */
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String path = exchange.getRequest().getURI().getPath();
        // 检查是否为外部API路径
        if (isExternalPath(path)) {
            return chain.filter(exchange); // 跳过当前过滤器
        }

        HttpHeaders headers =  exchange.getRequest().getHeaders();
        List<String> list = headers.get("volunteerdes");
        if(null != list){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String[] strs = decode(list.get(0)).split(",");
            System.out.println("bug=========+++++"+strs[0].toString());
            String[] strs1 = decrypt(strs[0],"bjtdba123").split("/");
            String c = strs1[1];
            System.out.println("bug=========+++++"+strs1[1].toString());
            Date date = null;
            try {
                date = df.parse(c);
                if(df.parse(df.format(new Date())).getTime() - date.getTime()<1000*60){
                    System.out.println("成功");
                }else{
                    System.out.println("用户名为null,非法用户.......");
                    //exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
                    //return exchange.getResponse().setComplete();
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            System.out.println("用户名为null,非法用户.......");
            //exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            //return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }

    private boolean isExternalPath(String path) {
        for (String pattern : EXTERNAL_PATHS) {
            if (pathMatcher.match(pattern, path)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 过滤方法登记
     * @return
     */
    public int getOrder() {
        return 1;
    }
}
