package com.bb.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


import com.bb.util.StringUtils;


import lombok.extern.slf4j.Slf4j;

import java.io.DataInputStream;
import java.util.Enumeration;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
 
@Slf4j
@Component
public class HttpInterceptor implements HandlerInterceptor {
	  private final static String REQUEST_POST = "POST";

 
 
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    	log.info("Requested Domain (Host): {}", request.getHeader("Host"));
        log.info("[preHandle]"+request.getRequestURI() +","+request.getMethod());
        
        
        log.info("___________________Parameter____________________________ ");
   
        	Enumeration params = request.getParameterNames();
        	while (params.hasMoreElements()) {
        	    String name = (String) params.nextElement();
        	    String value = request.getParameter(name);
        	    log.info(name + "=" + value);
        	}
        	        
        
        log.info("____________________________ Header  ____________________________ ");
        
        Enumeration<String> headers = request.getHeaderNames();
        while (headers.hasMoreElements()) {
            String name = (String) headers.nextElement();
            String value = request.getHeader(name);
            log.info(name + "=" + value);
        }
       
     
        log.info("____________________________ Request Body ____________________________ ");
        /*
        try {
	        DataInputStream dis = new DataInputStream(request.getInputStream());
	        String str = null;
	        while ((str = dis.readLine()) != null) {
	        	log.info(new String(str.getBytes("ISO-8859-1"), "euc-kr")+"<br/>"); 
	            // utf-8로 전송된 한글은 깨짐
	        	
	        }
        }catch(Exception e) {
        	  log.info("ERROR E : {}", e.getMessage());
        }
        */
		return true;
    }
 
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    	log.info("[postHandle]"+request.getRequestURI());
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception ex) throws Exception {
    	log.info("[afterCompletion]"+request.getRequestURI());
    }
  
}

