package com.hotwj.platform.common.utils;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.sl.extractor.SlideShowExtractor;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Slf4j
public class FileTextExtractor {

    public static String extractText(InputStream is, String fileExtension) {
        if (is == null || StrUtil.isBlank(fileExtension)) {
            return "";
        }

        String ext = fileExtension.toLowerCase();
        // Remove dot if present
        if (ext.startsWith(".")) {
            ext = ext.substring(1);
        }

        try {
            switch (ext) {
                case "txt":
                    return IoUtil.read(is, StandardCharsets.UTF_8);
                case "pdf":
                    try (PDDocument document = PDDocument.load(is)) {
                        PDFTextStripper stripper = new PDFTextStripper();
                        return stripper.getText(document);
                    }
                case "doc":
                    try (HWPFDocument doc = new HWPFDocument(is);
                         WordExtractor extractor = new WordExtractor(doc)) {
                        return extractor.getText();
                    }
                case "docx":
                    try (XWPFDocument doc = new XWPFDocument(is);
                         XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
                        return extractor.getText();
                    }
                case "xls":
                    try (HSSFWorkbook wb = new HSSFWorkbook(is);
                         ExcelExtractor extractor = new ExcelExtractor(wb)) {
                        extractor.setFormulasNotResults(false);
                        extractor.setIncludeSheetNames(false);
                        return extractor.getText();
                    }
                case "xlsx":
                    try (XSSFWorkbook wb = new XSSFWorkbook(is);
                         XSSFExcelExtractor extractor = new XSSFExcelExtractor(wb)) {
                        extractor.setFormulasNotResults(false);
                        extractor.setIncludeSheetNames(false);
                        return extractor.getText();
                    }
                case "ppt":
                    try (HSLFSlideShow ppt = new HSLFSlideShow(is);
                         SlideShowExtractor extractor = new SlideShowExtractor(ppt)) {
                        return extractor.getText();
                    }
                case "pptx":
                    try (XMLSlideShow ppt = new XMLSlideShow(is);
                         SlideShowExtractor extractor = new SlideShowExtractor(ppt)) {
                        return extractor.getText();
                    }
                default:
                    log.warn("Unsupported file extension for text extraction: {}", ext);
                    return "";
            }
        } catch (Exception e) {
            log.error("Failed to extract text from file type: " + ext, e);
            return "";
        }
    }
}
