/*
 *  Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
 *  <p>
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  <p>
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  <p>
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.easyagents.core.file2text.extractor.impl;

import com.easyagents.core.file2text.DocumentReadErrorCode;
import com.easyagents.core.file2text.DocumentReadException;
import com.easyagents.core.file2text.DocumentReadSupport;
import com.easyagents.core.file2text.DocumentTextSegment;
import com.easyagents.core.file2text.LightweightDocumentReadRequest;
import com.easyagents.core.file2text.LightweightDocumentReadResult;
import com.easyagents.core.file2text.extractor.FileExtractor;
import com.easyagents.core.file2text.source.DocumentSource;
import org.apache.poi.xslf.usermodel.*;
import org.apache.xmlbeans.XmlException;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * PPTX 文档提取器（.pptx）
 * 提取幻灯片中的标题、段落、表格文本
 */
public class PptxExtractor implements FileExtractor {

    private static final Set<String> SUPPORTED_MIME_TYPES;
    private static final String MIME_PREFIX = "application/vnd.openxmlformats-officedocument.presentationml";
    private static final Set<String> SUPPORTED_EXTENSIONS;

    static {
        // 精确 MIME（可选）
        Set<String> mimeTypes = new HashSet<>();
        mimeTypes.add("application/vnd.openxmlformats-officedocument.presentationml.presentation");
        mimeTypes.add("application/vnd.openxmlformats-officedocument.presentationml.slideshow");
        SUPPORTED_MIME_TYPES = Collections.unmodifiableSet(mimeTypes);

        // 支持的扩展名
        Set<String> extensions = new HashSet<>();
        extensions.add("pptx");
        extensions.add("ppsx");
        extensions.add("potx");
        SUPPORTED_EXTENSIONS = Collections.unmodifiableSet(extensions);
    }

    @Override
    public boolean supports(DocumentSource source) {
        String mimeType = source.getMimeType();
        String fileName = source.getFileName();

        // 1. MIME 精确匹配
        if (mimeType != null && SUPPORTED_MIME_TYPES.contains(mimeType)) {
            return true;
        }

        // 2. MIME 前缀匹配
        if (mimeType != null && mimeType.startsWith(MIME_PREFIX)) {
            return true;
        }

        // 3. 扩展名匹配
        if (fileName != null) {
            String ext = getExtension(fileName);
            if (ext != null && SUPPORTED_EXTENSIONS.contains(ext.toLowerCase())) {
                return true;
            }
        }

        return false;
    }

    @Override
    public String extractText(DocumentSource source) throws IOException {
        return read(new LightweightDocumentReadRequest(source)).getText();
    }

    /**
     * 按幻灯片读取 PPTX 文本。
     *
     * @param request 读取请求
     * @return 幻灯片结构化结果
     * @throws IOException 文档 I/O 失败
     */
    @Override
    public LightweightDocumentReadResult read(LightweightDocumentReadRequest request) throws IOException {
        DocumentSource source = request.getSource();
        try (InputStream is = source.openStream();
             XMLSlideShow slideShow = new XMLSlideShow(is)) {

            List<XSLFSlide> slides = slideShow.getSlides();
            if (slides.size() > request.getMaxSlides()) {
                throw new DocumentReadException(DocumentReadErrorCode.DOCUMENT_STRUCTURE_LIMIT_EXCEEDED,
                        "Slide count exceeds " + request.getMaxSlides());
            }
            List<DocumentTextSegment> segments = new ArrayList<>();
            int offset = 0;
            for (int i = 0; i < slides.size(); i++) {
                request.checkCancelled();
                XSLFSlide slide = slides.get(i);
                StringBuilder text = new StringBuilder();

                // 提取所有形状中的文本
                for (XSLFShape shape : slide.getShapes()) {
                    if (shape instanceof XSLFTextShape textShape && !(shape instanceof XSLFTable)) {
                        String shapeText = textShape.getText();
                        if (shapeText != null && !shapeText.trim().isEmpty()) {
                            text.append(shapeText).append("\n");
                        }
                    }
                }

                // 可选：提取表格
                extractTablesFromSlide(slide, text);
                if (!text.toString().isBlank()) {
                    DocumentTextSegment segment = DocumentReadSupport.segment(
                            "slide-" + (i + 1), text.toString(), "SLIDE",
                            "第 " + (i + 1) + " 张幻灯片", offset, List.of());
                    segments.add(segment);
                    offset = segment.getEndIndex() + 1;
                }
            }
            return DocumentReadSupport.result(source, segments, request);
        } catch (DocumentReadException e) {
            throw e;
        } catch (XmlException e) {
            throw new DocumentReadException(DocumentReadErrorCode.DOCUMENT_CORRUPTED,
                    "Invalid PPTX structure", e);
        } catch (Exception e) {
            throw new DocumentReadException(DocumentReadErrorCode.DOCUMENT_CORRUPTED,
                    "Failed to extract PPTX", e);
        }
    }

    /**
     * 提取幻灯片中的表格内容
     */
    private void extractTablesFromSlide(XSLFSlide slide, StringBuilder text) {
        for (XSLFShape shape : slide.getShapes()) {
            if (shape instanceof XSLFTable) {
                XSLFTable table = (XSLFTable) shape;
                text.append("\n[Table Start]\n");
                for (XSLFTableRow row : table.getRows()) {
                    List<String> cellTexts = new ArrayList<>();
                    for (XSLFTableCell cell : row.getCells()) {
                        String cellText = cell.getText();
                        cellTexts.add(cellText != null ? cellText.trim() : "");
                    }
                    text.append(String.join(" | ", cellTexts)).append("\n");
                }
                text.append("[Table End]\n");
            }
        }
    }

    @Override
    public int getOrder() {
        return 10;
    }

    private String getExtension(String fileName) {
        if (fileName == null || !fileName.contains(".")) return null;
        int lastDot = fileName.lastIndexOf('.');
        return fileName.substring(lastDot + 1);
    }
}
