package com.kospo.talk.config.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kospo.talk.config.exception.CustomErrorResponse;
import com.kospo.talk.config.exception.ErrorCode;
import com.kospo.talk.exception.*;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

public class SecurityExceptionFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException {
        ObjectMapper om = new ObjectMapper();
        try {
            filterChain.doFilter(request, response);
        } catch (CustomExpireTokenException e) {
            response.setStatus(500);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().println(
                    om.writeValueAsString(CustomErrorResponse.builder()
                            .code(ErrorCode.EXPIRE_TOKEN.getCode())
                            .message(ErrorCode.EXPIRE_TOKEN.getMessage())
                            .errTime(System.currentTimeMillis()).build())
            );
        } catch (CustomNoAccountException e) {
            response.setStatus(500);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().println(
                    om.writeValueAsString(CustomErrorResponse.builder()
                            .code(ErrorCode.NO_ACCOUNT.getCode())
                            .message(ErrorCode.NO_ACCOUNT.getMessage())
                            .errTime(System.currentTimeMillis()).build())
            );
        } catch (CustomNoMatchTokenException e) {
            response.setStatus(500);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().println(
                    om.writeValueAsString(CustomErrorResponse.builder()
                            .code(ErrorCode.NO_MATCH_TOKEN.getCode())
                            .message(ErrorCode.NO_MATCH_TOKEN.getMessage())
                            .errTime(System.currentTimeMillis()).build())
            );
        } catch (CustomNoResultException e) {
            response.setStatus(500);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().println(
                    om.writeValueAsString(CustomErrorResponse.builder()
                            .code(ErrorCode.NO_RESULT.getCode())
                            .message(ErrorCode.NO_RESULT.getMessage())
                            .errTime(System.currentTimeMillis()).build())
            );
        } catch (CustomNoTokenException e) {
            response.setStatus(500);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().println(
                    om.writeValueAsString(CustomErrorResponse.builder()
                            .code(ErrorCode.NO_TOKEN.getCode())
                            .message(ErrorCode.NO_TOKEN.getMessage())
                            .errTime(System.currentTimeMillis()).build())
            );
        } catch (ServletException e) {
            response.setStatus(500);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().println(
                    om.writeValueAsString(CustomErrorResponse.builder()
                            .code(ErrorCode.INTERNAL_SERVER_ERROR.getCode())
                            .message(ErrorCode.INTERNAL_SERVER_ERROR.getMessage())
                            .errTime(System.currentTimeMillis()).build())
            );
        } catch (IOException e) {
            response.setStatus(500);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().println(
                    om.writeValueAsString(CustomErrorResponse.builder()
                            .code(ErrorCode.INTERNAL_SERVER_ERROR.getCode())
                            .message(ErrorCode.INTERNAL_SERVER_ERROR.getMessage())
                            .errTime(System.currentTimeMillis()).build())
            );
        } catch (CustomMalformedJwtException e) {
            response.setStatus(500);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().println(
                    om.writeValueAsString(CustomErrorResponse.builder()
                            .code(ErrorCode.MALFORMED_JWT.getCode())
                            .message(ErrorCode.MALFORMED_JWT.getMessage())
                            .errTime(System.currentTimeMillis()).build())
            );
        } catch (CustomNoSameIpException e) {
            response.setStatus(500);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().println(
                    om.writeValueAsString(CustomErrorResponse.builder()
                            .code(ErrorCode.NO_SAME_IP.getCode())
                            .message(ErrorCode.NO_SAME_IP.getMessage())
                            .errTime(System.currentTimeMillis()).build())
            );
        }
    }
}
