package com.icomsys.main_vm.common.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
//import com.icomsys.main_vm.db.jpa.repo.bot.TbBotAuthMenuRepo;
//import com.icomsys.main_vm.db.jpa.repo.bot.TbBotMenuRepo;
import com.google.gson.Gson;
import com.icomsys.main_vm.common.code.enumresource.SessionResource;
import com.icomsys.main_vm.db.mybatis.alias.LoginVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.ContentCachingResponseWrapper;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;

@Configuration
@Slf4j
@RequiredArgsConstructor
public class LoginFilter extends OncePerRequestFilter {

    private final ObjectMapper objectMapper;
//    private final TbBotAuthMenuRepo tbBotAuthMenuRepo;
//    private final TbBotMenuRepo tbBotMenuRepo;
//    @Value("${Server.servlet.context-path}")
//    private String context;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        int chk = 0;
//        ContentCachingRequestWrapper wrapRequest = new ContentCachingRequestWrapper(request); // 커스텀으로 생성한 Wrapper
//        ContentCachingResponseWrapper wrapResponse = new ContentCachingResponseWrapper(response);
//        String reqUrl = request.getRequestURI().toString().replaceAll(context, "");
        String reqUrl = request.getRequestURI();
//        log.info("meyhod-"+ reqUrl+ request.getMethod());
//        log.info("필터 요청유알엘 - {}, {}", reqUrl);


        if (reqUrl.startsWith("/re1")){
            response.sendError(490);
            return;
        }

        if (reqUrl.startsWith("/re2")){
            response.sendError(491);
            return;
        }

        if (reqUrl.startsWith("/lib")
                || reqUrl.startsWith("/css")
                || reqUrl.startsWith("/images")
                || reqUrl.startsWith("/js")
                || reqUrl.startsWith("/adm")
                || reqUrl.startsWith("/ws")
                || reqUrl.startsWith("/rest")
                || reqUrl.startsWith("/create")
                || reqUrl.startsWith("/data")
                || reqUrl.startsWith("/createEnv")
                || reqUrl.startsWith("/error")
                || reqUrl.startsWith("/files")
                || reqUrl.equals("/")
        ) {
            filterChain.doFilter(request, response);
        } else {
            if (request.getSession().getAttribute(SessionResource.UserVO.getName()) == null || request.getSession().getAttribute(SessionResource.UserVO.getName()).equals("")) {
                log.info("session Check");
                response.sendError(490);
                return;
            } else {
                log.info("Permission Check");
                //세션이 있으면 URL별 권한 확인.
                //ajax로 호출되는 url은 auth메뉴에 리스트업이 안되있어서 검사를 어떻게할지 계획이 필요함.
//                LoginVO loginVO = (LoginVO) request.getSession().getAttribute("LoginVO");
//                List<String> auths =
//                        if(!tbBotMenuRepo.findByMenuNoInOrderByMenuNoAsc(tbBotAuthMenuRepo.findByAuthCode(loginVO.getAuthCode())
//                                .stream().map(e -> e.getMenuNo())
//                                .collect(Collectors.toList()))
//                        .stream()
//                        .map(f -> f.getUrlPath())
//                        .collect(Collectors.toList())
//                        .contains(reqUrl)){
//                            wrapResponse.sendRedirect("adm/main/egovLoginUsr.do");
//                        }
//                log.info("auths - {}", new Gson().toJson(auths));
//
//                for (String e : auths) {
//                    if (e != null && !e.equals("") && !e.equals("null") && e.equals(reqUrl)) {
//                            chk+=1;
//                        break;
//                    }
//                }
//                if(chk == 0){
//                    wrapResponse.setStatus(404);
//                    wrapResponse.sendRedirect("adm/main/egovLoginUsr.do");
//                }
                filterChain.doFilter(request, response);
            }
        }
//        response.copyBodyToResponse();
    }

}
