package com.storlead.framework.web.filter;

import lombok.extern.log4j.Log4j2;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: storlead-storlead-mail-platform
 * @description:
 * @author: chenkq
 * @create: 2025-06-17 15:46
 */

@Log4j2
@Component
public class AllRequestFilter implements Filter, Ordered {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "PUT, POST, GET, OPTIONS, DELETE");
        /// response.setHeader("Access-Control-Max-Age", "3600");
        //这里要加上content-type
        response.setHeader("Access-Control-Allow-Credentials", "true");

        response.setHeader("Access-Control-Allow-Headers","x-requested-with,Authorization,Sec-WebSocket-Protocol,token,method,Content-Type, x-requested-with, X-Custom-Header,X-Tenant-Id,x-access-token");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Keep-Alive", "60s");
        response.setHeader("Connection", "upgrade, keep-alive");

//        if ("OPTIONS".equals(request.getMethod())) {
//            Result result = Result.error("OPTIONS 请求");
//            response.setStatus(HttpStatus.OK.value());
//            SystemUtils.printJson(response, JSON.toJSONString(result));
//            return;
//        } else {
//
//        }
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }

    @Override
    public int getOrder() {
        return -99;
    }
}
