package com.microservices.dms.utils;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class ExportWordUtilCopy001 extends Component {

    public static void exportToWord(String outputPath,List<TestCase> testCases) throws Exception {
        XWPFDocument document = new XWPFDocument();
        // 添加5.1.1项目标题
        addTitle(document, "5.1.1项目", 1);

        // 添加迭代标题
        addTitle(document, "5.1.1.1迭代", 2);

        // 添加描述段落
        addParagraph(document, "迭代测试项共包含" + testCases.size() + "个测试用例，相关用例概述如表5-2所示。");

        // 创建表格
        XWPFTable table = createStyledTable(document, testCases.size() + 1, 4); // 行数=用例数+表头

        // 设置表格标题
        setTableCaption(document, "表5-2  迭代功能测试用例概述表");

        // 填充表头
        fillTableHeader(table, new String[]{"序号", "测试用例标识", "测试用例名称", "测试用例说明", "正常/异常用例"});

        // 填充测试用例数据
        fillTestCases(table, testCases);

        // 保存文档
        try (FileOutputStream out = new FileOutputStream(outputPath)) {
            document.write(out);
        }

        document.close();
    }

    // 添加标题
    private static void addTitle(XWPFDocument document, String text, int level) {
        XWPFParagraph paragraph = document.createParagraph();
        paragraph.setStyle("Heading" + level);
        XWPFRun run = paragraph.createRun();
        run.setText(text);
        run.setBold(true);
        run.setFontSize(20 - level * 2);
    }

    // 添加普通段落
    private static void addParagraph(XWPFDocument document, String text) {
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText(text);
    }

    // 设置表格标题
    private static void setTableCaption(XWPFDocument document, String caption) {
        XWPFParagraph paragraph = document.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun run = paragraph.createRun();
        run.setText(caption);
        run.setItalic(true);
    }

    // 创建带样式的表格
    private static XWPFTable createStyledTable(XWPFDocument document, int rows, int cols) {
        XWPFTable table = document.createTable(rows, cols);
        // 表格宽度设置
        CTTblPr tblPr = table.getCTTbl().getTblPr();
        CTTblWidth tblWidth = tblPr.addNewTblW();
        tblWidth.setW(BigInteger.valueOf(9000));
        tblWidth.setType(STTblWidth.DXA);

        // 表格边框
        CTTblBorders borders = tblPr.addNewTblBorders();
        setBorder(borders.addNewTop(), STBorder.SINGLE, 4, "auto");
        setBorder(borders.addNewLeft(), STBorder.SINGLE, 4, "auto");
        setBorder(borders.addNewBottom(), STBorder.SINGLE, 4, "auto");
        setBorder(borders.addNewRight(), STBorder.SINGLE, 4, "auto");
        setBorder(borders.addNewInsideH(), STBorder.SINGLE, 2, "auto");
        setBorder(borders.addNewInsideV(), STBorder.SINGLE, 2, "auto");

        // 表格对齐
        tblPr.addNewJc().setVal(STJc.CENTER);

        return table;
    }

    private static void setBorder(CTBorder border, STBorder.Enum style, int size, String color) {
        border.setVal(style);
        border.setSz(BigInteger.valueOf(size));
        border.setColor(color);
    }

    // 填充表头
    private static void fillTableHeader(XWPFTable table, String[] headers) {
        XWPFTableRow headerRow = table.getRow(0);
        for (int i = 0; i < headers.length; i++) {
            XWPFTableCell cell = headerRow.getCell(i);

            // 清除单元格原有内容
            for (int k = cell.getParagraphs().size() - 1; k >= 0; k--) {
                cell.removeParagraph(k);
            }

            // 创建新段落和文本
            XWPFParagraph para = cell.addParagraph();
            para.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun run = para.createRun();
            run.setText(headers[i]);
            run.setBold(true);

            // 设置表头背景色 - 更兼容的写法
            if (cell.getCTTc().getTcPr() == null) {
                cell.getCTTc().addNewTcPr();
            }
            CTShd shd = cell.getCTTc().getTcPr().isSetShd() ?
                    cell.getCTTc().getTcPr().getShd() :
                    cell.getCTTc().getTcPr().addNewShd();
            shd.setFill("D3D3D3");
            shd.setVal(STShd.CLEAR);  // 设置填充模式
        }
    }

    // 填充测试用例数据
    private static void fillTestCases(XWPFTable table, List<TestCase> testCases) {
        for (int i = 0; i < testCases.size(); i++) {
            TestCase testCase = testCases.get(i);
            XWPFTableRow row = table.getRow(i + 1); // 第一行是表头

            // 序号
            setCellText(row, 0, String.valueOf(i + 1));

            // 测试用例标识
            setCellText(row, 1, testCase.getIdentifier());

            // 测试用例名称
            setCellText(row, 2, testCase.getName());

            // 测试用例说明
            setCellText(row, 3, testCase.getDescription());

            // 正常/异常用例
            setCellText(row, 4, testCase.getType());
        }
    }

    private static void setCellText(XWPFTableRow row, int col, String text) {
        XWPFTableCell cell = row.getCell(col);
        cell.removeParagraph(0);
        XWPFParagraph para = cell.addParagraph();
        para.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun run = para.createRun();
        run.setText(text);
    }
}
