package com.bofeng.support;

import com.bofeng.Consts;
import com.bofeng.service.SecurityService;
import org.apache.shiro.util.AntPathMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Configuration
public class EchoHttpAutoConfiguration {

    private static final AntPathMatcher ANT_PATH_MATCHER = new AntPathMatcher();
    private static final Logger log = LoggerFactory.getLogger("ECHO");

    @Bean
    public EchoFilter echoFilterBean() {
        return new EchoFilter();
    }

    @Bean
    public FilterRegistrationBean echoFilter() {

        FilterRegistrationBean bean = new FilterRegistrationBean(echoFilterBean());
        bean.addUrlPatterns("/*");
        bean.setName("EchoFilter");
        return bean;
    }

    public static class EchoFilter implements Filter {
        @Autowired
        private SecurityService securityService;

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            log.info("EchoFilter inited");
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
                chain.doFilter(request, response);
                return;
            }

            HttpServletResponse httpResponse = (HttpServletResponse) response;
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            boolean igor = true;
            long start = System.currentTimeMillis();
            if (log.isInfoEnabled()) {
                igor = false;
                for (String path : Consts.NOT_ECHO_PATHS) {
                    if (ANT_PATH_MATCHER.match(httpRequest.getContextPath() + path, ((HttpServletRequest) request).getRequestURI())) {
                        igor = true;
                    }
                }
            }

            chain.doFilter(httpRequest, httpResponse);

            if (!igor) {
                //打印请求日志
                if (httpRequest.getQueryString() == null) {
                    log.info("{} {} cost {}ms",
                            httpRequest.getMethod(),
                            httpRequest.getRequestURI(),
                            System.currentTimeMillis() - start);
                    //log.info(httpRequest.getMethod() + " " + httpRequest.getRequestURI() + " cost " + t + "ms");
                } else {
                    log.info("{} {}?{} cost {}ms",
                            httpRequest.getMethod(),
                            httpRequest.getRequestURI(),
                            httpRequest.getQueryString(),
                            System.currentTimeMillis() - start);
                    //log.info(httpRequest.getMethod() + " " + httpRequest.getRequestURI() + "?" + httpRequest.getQueryString());
                }
            }
        }

        @Override
        public void destroy() {
        }
    }

}
