package stirling.software.SPDF.controller.web;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;

import io.swagger.v3.oas.annotations.Hidden;

import lombok.RequiredArgsConstructor;

import stirling.software.common.model.ApplicationProperties;
import stirling.software.common.util.CheckProgramInstall;

// @Controller // Disabled - Backend-only mode, no Thymeleaf UI
@RequiredArgsConstructor
public class OtherWebController {

    private final ApplicationProperties applicationProperties;

    @Deprecated
    // @GetMapping("/compress-pdf")
    @Hidden
    public String compressPdfForm(Model model) {
        model.addAttribute("currentPage", "compress-pdf");
        return "misc/compress-pdf";
    }

    @Deprecated
    // @GetMapping("/replace-and-invert-color-pdf")
    @Hidden
    public String replaceAndInvertColorPdfForm(Model model) {
        model.addAttribute("currentPage", "replace-invert-color-pdf");
        return "misc/replace-color";
    }

    @Deprecated
    // @GetMapping("/extract-image-scans")
    @Hidden
    public ModelAndView extractImageScansForm() {
        ModelAndView modelAndView = new ModelAndView("misc/extract-image-scans");
        boolean isPython = CheckProgramInstall.isPythonAvailable();
        modelAndView.addObject("isPython", isPython);
        modelAndView.addObject("currentPage", "extract-image-scans");
        return modelAndView;
    }

    @Deprecated
    // @GetMapping("/show-javascript")
    @Hidden
    public String extractJavascriptForm(Model model) {
        model.addAttribute("currentPage", "show-javascript");
        return "misc/show-javascript";
    }

    @Deprecated
    // @GetMapping("/stamp")
    @Hidden
    public String stampForm(Model model) {
        model.addAttribute("currentPage", "stamp");
        return "misc/stamp";
    }

    @Deprecated
    // @GetMapping("/add-page-numbers")
    @Hidden
    public String addPageNumbersForm(Model model) {
        model.addAttribute("currentPage", "add-page-numbers");
        return "misc/add-page-numbers";
    }

    @Deprecated
    // @GetMapping("/scanner-effect")
    @Hidden
    public String scannerEffectForm(Model model) {
        model.addAttribute("currentPage", "scanner-effect");
        return "misc/scanner-effect";
    }

    @Deprecated
    // @GetMapping("/extract-images")
    @Hidden
    public String extractImagesForm(Model model) {
        model.addAttribute("currentPage", "extract-images");
        return "misc/extract-images";
    }

    @Deprecated
    // @GetMapping("/flatten")
    @Hidden
    public String flattenForm(Model model) {
        model.addAttribute("currentPage", "flatten");
        return "misc/flatten";
    }

    @Deprecated
    // @GetMapping("/change-metadata")
    @Hidden
    public String addWatermarkForm(Model model) {
        model.addAttribute("currentPage", "change-metadata");
        return "misc/change-metadata";
    }

    @Deprecated
    // @GetMapping("/unlock-pdf-forms")
    @Hidden
    public String unlockPDFForms(Model model) {
        model.addAttribute("currentPage", "unlock-pdf-forms");
        return "misc/unlock-pdf-forms";
    }

    @Deprecated
    // @GetMapping("/compare")
    @Hidden
    public String compareForm(Model model) {
        model.addAttribute("currentPage", "compare");
        return "misc/compare";
    }

    @Deprecated
    // @GetMapping("/print-file")
    @Hidden
    public String printFileForm(Model model) {
        model.addAttribute("currentPage", "print-file");
        return "misc/print-file";
    }

    public List<String> getAvailableTesseractLanguages() {
        String tessdataDir = applicationProperties.getSystem().getTessdataDir();
        File[] files = new File(tessdataDir).listFiles();
        if (files == null) {
            return Collections.emptyList();
        }
        return Arrays.stream(files)
                .filter(file -> file.getName().endsWith(".traineddata"))
                .map(file -> file.getName().replace(".traineddata", ""))
                .filter(lang -> !"osd".equalsIgnoreCase(lang))
                .sorted()
                .toList();
    }

    @Deprecated
    // @GetMapping("/ocr-pdf")
    @Hidden
    public ModelAndView ocrPdfPage() {
        ModelAndView modelAndView = new ModelAndView("misc/ocr-pdf");
        List<String> languages = getAvailableTesseractLanguages();
        modelAndView.addObject("languages", languages);
        modelAndView.addObject("currentPage", "ocr-pdf");
        return modelAndView;
    }

    @Deprecated
    // @GetMapping("/add-image")
    @Hidden
    public String overlayImage(Model model) {
        model.addAttribute("currentPage", "add-image");
        return "misc/add-image";
    }

    @Deprecated
    // @GetMapping("/adjust-contrast")
    @Hidden
    public String contrast(Model model) {
        model.addAttribute("currentPage", "adjust-contrast");
        return "misc/adjust-contrast";
    }

    @Deprecated
    // @GetMapping("/repair")
    @Hidden
    public String repairForm(Model model) {
        model.addAttribute("currentPage", "repair");
        return "misc/repair";
    }

    @Deprecated
    // @GetMapping("/remove-blanks")
    @Hidden
    public String removeBlanksForm(Model model) {
        model.addAttribute("currentPage", "remove-blanks");
        return "misc/remove-blanks";
    }

    @Deprecated
    // @GetMapping("/remove-annotations")
    @Hidden
    public String removeAnnotationsForm(Model model) {
        model.addAttribute("currentPage", "remove-annotations");
        return "misc/remove-annotations";
    }

    @Deprecated
    // @GetMapping("/auto-crop")
    @Hidden
    public String autoCropForm(Model model) {
        model.addAttribute("currentPage", "auto-crop");
        return "misc/auto-crop";
    }

    @Deprecated
    // @GetMapping("/auto-rename")
    @Hidden
    public String autoRenameForm(Model model) {
        model.addAttribute("currentPage", "auto-rename");
        return "misc/auto-rename";
    }

    @Deprecated
    // @GetMapping("/add-attachments")
    @Hidden
    public String attachmentsForm(Model model) {
        model.addAttribute("currentPage", "add-attachments");
        return "misc/add-attachments";
    }
}
