package com.hotent.util.wordexcelutils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.List;
import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.IOException;

import com.alibaba.fastjson.JSON;
import com.hotent.base.util.PinyinUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文档大括号内容提取工具类
 */
@Slf4j
public class BracketContentExtractor {

    private static final Pattern BRACKET_PATTERN = Pattern.compile("\\{([^}]+)\\}");

    /**
     * 从Word文档中提取大括号内容及其拼音
     *
     * @param filePath Word文档路径
     * @return 包含原文和拼音的列表
     * @throws IOException 文件读取异常
     */
    public static List<BracketContentInfo> extractFromWord(String filePath) throws IOException {
        List<BracketContentInfo> result = new ArrayList<>();

        try (FileInputStream fis = new FileInputStream(filePath);
             XWPFDocument document = new XWPFDocument(fis)) {

            for (XWPFParagraph paragraph : document.getParagraphs()) {
                String text = paragraph.getText();
                extractBracketContent(result, text);
            }

            // 提取表格中的大括号内容
            for (org.apache.poi.xwpf.usermodel.XWPFTable table : document.getTables()) {
                for (org.apache.poi.xwpf.usermodel.XWPFTableRow row : table.getRows()) {
                    for (org.apache.poi.xwpf.usermodel.XWPFTableCell cell : row.getTableCells()) {
                        // 处理单元格中的段落
                        for (XWPFParagraph paragraph : cell.getParagraphs()) {
                            String text = paragraph.getText();
                            extractBracketContent(result, text);
                        }
                    }
                }
            }
        }
        return result;
    }

    /**
     * 从Word文档中提取大括号内容及其拼音
     *
     * @param file Word文档路径
     * @return 包含原文和拼音的列表
     * @throws IOException 文件读取异常
     */
    public static List<BracketContentInfo> extractFromWord(MultipartFile file) throws IOException {
        List<BracketContentInfo> result = new ArrayList<>();

        try (
             XWPFDocument document = new XWPFDocument(file.getInputStream())) {

            for (XWPFParagraph paragraph : document.getParagraphs()) {
                String text = paragraph.getText();
                extractBracketContent(result, text);
            }

            // 提取表格中的大括号内容
            for (org.apache.poi.xwpf.usermodel.XWPFTable table : document.getTables()) {
                for (org.apache.poi.xwpf.usermodel.XWPFTableRow row : table.getRows()) {
                    for (org.apache.poi.xwpf.usermodel.XWPFTableCell cell : row.getTableCells()) {
                        // 处理单元格中的段落
                        for (XWPFParagraph paragraph : cell.getParagraphs()) {
                            String text = paragraph.getText();
                            extractBracketContent(result, text);
                        }
                    }
                }
            }

        }catch (Exception e) {
            log.info("日志:{}",e.getMessage(),e);
        }
        log.info(JSON.toJSONString(result));
        return result;
    }

    /**
     * 从Excel文档中提取大括号内容及其拼音
     *
     * @param file Excel文档路径
     * @return 包含原文和拼音的列表
     * @throws IOException 文件读取异常
     */
    public static List<BracketContentInfo> extractFromExcel(MultipartFile file) throws IOException {
        List<BracketContentInfo> result = new ArrayList<>();

        try (
             Workbook workbook = new XSSFWorkbook(file.getInputStream())) {

            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                Sheet sheet = workbook.getSheetAt(i);
                for (Row row : sheet) {
                    for (Cell cell : row) {
                        if (cell.getCellType() == CellType.STRING) {
                            String text = cell.getStringCellValue();
                            extractBracketContent(result, text);
                        }
                    }
                }
            }
        }

        return result;
    }

    public static List<BracketContentInfo> extractFromExcel(String filePath) throws IOException {
        List<BracketContentInfo> result = new ArrayList<>();

        try (FileInputStream fis = new FileInputStream(filePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                Sheet sheet = workbook.getSheetAt(i);
                for (Row row : sheet) {
                    for (Cell cell : row) {
                        if (cell.getCellType() == CellType.STRING) {
                            String text = cell.getStringCellValue();
                            extractBracketContent(result, text);
                        }
                    }
                }
            }
        }

        return result;
    }


    /**
     * 从文本中提取大括号内容
     *
     * @param result 结果列表
     * @param text 待处理文本
     */
    private static void extractBracketContent(List<BracketContentInfo> result, String text) {
        if (text == null || text.isEmpty()) {
            return;
        }

        Matcher matcher = BRACKET_PATTERN.matcher(text);
        while (matcher.find()) {
            String content = matcher.group(1);
            // 只处理包含中文的内容
            if (content.matches(".*[\\u4E00-\\u9FA5]+.*")) {
                String pinyin = PinyinUtil.getPinyin(content);
                result.add(new BracketContentInfo(content, pinyin));
            }
        }
    }

    /**
     * 大括号内容信息类
     */
    public static class BracketContentInfo {
        private String originalText;
        private String pinyin;

        public BracketContentInfo(String originalText, String pinyin) {
            this.originalText = originalText;
            this.pinyin = pinyin;
        }

        public String getOriginalText() {
            return originalText;
        }

        public String getPinyin() {
            return pinyin;
        }

        @Override
        public String toString() {
            return "原文: " + originalText + ", 拼音: " + pinyin;
        }
    }

    public static void main(String[] args) throws IOException {
        extractFromWord("C:\\Users\\123\\Desktop\\送  达  回  证.docx");
    }
}
