package com.wmyun.module.system.controller.admin.pdf;

import com.wmyun.module.system.controller.admin.pdf.vo.PdfResultVO;
import com.wmyun.module.system.service.pdf.PdfSplitService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author zhouc
 * @date 2025/3/1 14:33
 * @className PDFController
 * @description
 */
@Tag(name = "管理后台 - PDF处理")
@RestController
@RequestMapping("/system/pdf")
@Validated
@Transactional
public class PDFController {

    @Resource
    private PdfSplitService pdfSplitService;
    private String uploadDir; // 配置文件设置存储路径

    @PostMapping("/split_pdf")
    @Operation(summary = "PDF拆分")
    @PreAuthorize("@ss.hasPermission('system:pdf:split_pdf')")
    public ResponseEntity<?> splitPdfresult(@RequestParam("file") MultipartFile file) {
        try {
            if (!file.getContentType().equals("application/pdf")) {
                throw new IllegalArgumentException("仅支持 PDF 文件");
            }
            // 保存临时文件
            File tempFile = File.createTempFile("split-", ".pdf");
            file.transferTo(tempFile);
            // 执行拆分
            List<PdfResultVO> resultList = pdfSplitService.splitPdf(tempFile, uploadDir);
            tempFile.delete();
            // 返回结果
            return ResponseEntity.ok(resultList);

        } catch (IOException e) {
            return ResponseEntity.status(500)
                    .body(Map.of("error", "处理失败: " + e.getMessage()));
        }
    }

    @PostMapping("/split_image")
    @Operation(summary = "PDF拆分图片")
    @PreAuthorize("@ss.hasPermission('system:pdf:split_image')")
    public ResponseEntity<?> PdfToImageConverter(@RequestParam("file") MultipartFile file) {
        try {
            if (!file.getContentType().equals("application/pdf")) {
                throw new IllegalArgumentException("仅支持 PDF 文件");
            }
            // 保存临时文件
            File tempFile = File.createTempFile("split-", ".pdf");
            file.transferTo(tempFile);
            // 执行拆分
            List<PdfResultVO> resultList = pdfSplitService.PdfToImageConverter(tempFile, uploadDir);
            tempFile.delete();
            // 返回结果
            return ResponseEntity.ok(resultList);
        } catch (IOException e) {
            return ResponseEntity.status(500).body(Map.of("error", "处理失败: " + e.getMessage()));
        }
    }
//    @PostMapping("/split")
//    public ResponseEntity<String> splitPdf(
//            @RequestParam("file") MultipartFile file,
//            @RequestParam(defaultValue = "1") int startPage,
//            @RequestParam(defaultValue = "1") int endPage) {
//        try {
//            File tempFile = File.createTempFile("temp",  ".pdf");
//            file.transferTo(tempFile);
//            String outputPath = "/output/";
//            pdfSplitService.splitPdfByPages(tempFile,  outputPath, startPage, endPage);
//            return ResponseEntity.ok(" 拆分成功");
//        } catch (IOException e) {
//            return ResponseEntity.status(500).body(" 处理失败: " + e.getMessage());
//        }
//    }
}
