package com.madeu.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.io.IOException;
import java.net.URLDecoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Component
public class ImageInterceptor implements HandlerInterceptor {

    @Value("${file.upload-path}")
    private String F_PATH;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        String uri = request.getRequestURI();
        String filename = null;
        Path filePath = null;

        if (uri.contains("/appeventbanner/")) {
            filename = uri.split("/appeventbanner/")[1];
            filePath = Paths.get(F_PATH + "/appeventbanner/" + URLDecoder.decode(filename));
        }
        else if (uri.contains("/categoryItem/")) {
            filename = uri.split("/categoryItem/")[1];
            filePath = Paths.get(F_PATH + "/categoryItem/" + URLDecoder.decode(filename));
        }
        else if (uri.contains("/appcontentthumbnail/")) {
            filename = uri.split("/appcontentthumbnail/")[1];
            filePath = Paths.get(F_PATH + "/appcontentthumbnail/" + URLDecoder.decode(filename));
        }
        else if (uri.contains("/contentdetail/")) {
            filename = uri.split("/contentdetail/")[1];
            filePath = Paths.get(F_PATH + "/contentdetail/" + URLDecoder.decode(filename));
        }
        else if (uri.contains("/mainbanner/")) {
            filename = uri.split("/mainbanner/")[1];
            filePath = Paths.get(F_PATH + "/mainbanner/" + URLDecoder.decode(filename));
        }
        else if (uri.contains("/popup/")) {
            filename = uri.split("/popup/")[1];
            filePath = Paths.get(F_PATH + "/popup/" + URLDecoder.decode(filename));
        }
        else if (uri.contains("/event/")) {
            filename = uri.split("/event/")[1];
            filePath = Paths.get(F_PATH + "/event/" + URLDecoder.decode(filename));
        }
        else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid path");
            return false;
        }

        if (!Files.exists(filePath) || !Files.isReadable(filePath)) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "File not found or not readable");
            return false;
        }

        Resource file = new UrlResource(filePath.toUri());
        String mimeType = Files.probeContentType(filePath);
        if (mimeType == null) {
            mimeType = "application/octet-stream";
        }

        response.setContentType(mimeType);
        response.setHeader("Content-Disposition", "inline; filename=\"" + file.getFilename() + "\"");
        file.getInputStream().transferTo(response.getOutputStream());
        return false;
    }
}
