package com.wmyun.farmwork.word.deprecated.core;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.template.ElementTemplate;
import com.deepoove.poi.template.MetaTemplate;
import com.deepoove.poi.template.run.RunTemplate;
import com.wmyun.farmwork.word.deprecated.config.DefaultConfig;
import com.wmyun.farmwork.word.deprecated.core.model.constant.TagType;
import com.wmyun.farmwork.word.deprecated.core.model.gen.TagsGenDataModel;
import com.wmyun.farmwork.word.deprecated.core.utils.WPUtils;
import jakarta.validation.constraints.NotNull;
import org.apache.poi.xwpf.usermodel.*;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 模板标签生成
 * @Date: 2025/3/1 11:01
 * @Created: by ZZSLL
 */



@Deprecated
public class TemplateTagsGenerate {

    public static List<TagsGenDataModel> execAllTags(@NotNull String absolutePath) {
        return execAllTags(absolutePath, DefaultConfig.def());
    }


    /**
     * 获取文件中所有标签，如果是表格，获取表格中每一行标签
     * @param absolutePath 文件路径
     * @return List<TagsGenDataModel>
     */
    public static List<TagsGenDataModel> execAllTags(@NotNull String absolutePath, Configure conf) {

        List<TagsGenDataModel> results = new ArrayList<>();

        XWPFTemplate template = XWPFTemplate.compile(absolutePath, conf);
        Pattern pattern = Pattern.compile("\\[(.*?)\\]");

        List<MetaTemplate> list = template.getElementTemplates();
        for (MetaTemplate metaTemplate : list) {
            TagsGenDataModel result = new TagsGenDataModel();
            if (metaTemplate instanceof ElementTemplate ele) {
                String source = ele.getSource();
                String tagName = ele.getTagName();
                if (source.contains(TagType.PICTURE.getSign())) {
                    result.setType(TagType.PICTURE);
                    result.setName(tagName);
                } else if (source.contains(TagType.LOOP_ROW_TABLE.getSign())) {
                    result.setType(TagType.LOOP_ROW_TABLE);
                    result.setName(tagName);
                    if (metaTemplate instanceof RunTemplate runTemplate) {
                        XWPFRun run = runTemplate.getRun();
                        XWPFTableCell tagCell = (XWPFTableCell) ((XWPFParagraph) run.getParent()).getBody();
                        XWPFTable table = tagCell.getTableRow().getTable();
                        int templateRowIndex = WPUtils.getTemplateRowIndex(tagCell, false);

                        XWPFTableRow row = table.getRow(templateRowIndex);
                        List<String> fields = new ArrayList<>();
                        for (XWPFTableCell cell : row.getTableCells()) {
                            String text = cell.getText();

                            Matcher matcher = pattern.matcher(text);

                            while (matcher.find()) {
                                String colTag = matcher.group(1);
                                fields.add(colTag);
                            }
                        }
                        result.setFields(fields);
                    }
                }

                else {
                    result.setType(TagType.TEXT);
                    result.setName(tagName);
                }
            }
            results.add(result);
        }

        return results;
    }
}
