package com.wmyun.farmwork.word.core;

import cn.hutool.core.io.IoUtil;
import com.wmyun.farmwork.word.core.enums.BookmarkType;
import com.wmyun.farmwork.word.core.model.BookmarkQueryDataModel;
import com.wmyun.farmwork.word.core.model.BookmarkReplaceDataModel;
import com.wmyun.farmwork.word.core.model.ext.PictureExData;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.impl.common.IOUtil;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description: TODO
 * @Date: 2025/3/3 9:47
 * @Created: by ZZSLL
 */

@Component
public class BookmarkQuery {

    /**
     * 查询word文件中所有书签
     */
    public static List<BookmarkQueryDataModel> queryAll(byte[] fileContent) {
        InputStream in = new ByteArrayInputStream(fileContent);
        Set<BookmarkQueryDataModel> results = new HashSet<>();
        List<CTBookmark> bookmarks = new ArrayList<>();
        try (XWPFDocument doc = new XWPFDocument(in)) {
            List<XWPFParagraph> allParagraphs = new ArrayList<>(doc.getParagraphs());
            for (XWPFParagraph paragraph : doc.getParagraphs()) {
                bookmarks.addAll(paragraph.getCTP().getBookmarkStartList());
            }

            for (XWPFTable table : doc.getTables()) {
                for (XWPFTableRow row : table.getRows()) {
                    for (XWPFTableCell cell : row.getTableCells()) {
                        for (XWPFParagraph para : cell.getParagraphs()) {
                            bookmarks.addAll(para.getCTP().getBookmarkStartList());
                            allParagraphs.add(para);
                        }
                    }
                }
            }

            for (XWPFHeader header : doc.getHeaderList()) {
                for (XWPFParagraph para : header.getParagraphs()) {
                    bookmarks.addAll(para.getCTP().getBookmarkStartList());
                    allParagraphs.add(para);
                }
                for (XWPFTable table : header.getTables()) {
                    for (XWPFTableRow row : table.getRows()) {
                        for (XWPFTableCell cell : row.getTableCells()) {
                            for (XWPFParagraph para : cell.getParagraphs()) {
                                allParagraphs.add(para);
                                bookmarks.addAll(para.getCTP().getBookmarkStartList());
                            }
                        }
                    }
                }
            }

            for (XWPFFooter footer : doc.getFooterList()) {
                for (XWPFParagraph para : footer.getParagraphs()) {
                    allParagraphs.add(para);
                    bookmarks.addAll(para.getCTP().getBookmarkStartList());
                }
                for (XWPFTable table : footer.getTables()) {
                    for (XWPFTableRow row : table.getRows()) {
                        for (XWPFTableCell cell : row.getTableCells()) {
                            for (XWPFParagraph para : cell.getParagraphs()) {
                                allParagraphs.add(para);
                                bookmarks.addAll(para.getCTP().getBookmarkStartList());
                            }
                        }
                    }
                }
            }

            for (XWPFParagraph paragraph : allParagraphs) {
                List<XWPFRun> runs = paragraph.getRuns();
                for (XWPFRun run : runs) {
                    List<XWPFPicture> pictures = run.getEmbeddedPictures();
                    for (XWPFPicture picture : pictures) {
                        String description = picture.getDescription();
                        results.add(new BookmarkQueryDataModel(description, BookmarkType.PICTURE_DESC));
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        for (CTBookmark bookmark : bookmarks) {
            String name = bookmark.getName();
            results.add(new BookmarkQueryDataModel(name, null));
        }

        return new ArrayList<>(results);
    }
}
