package com.bonus.common.core.utils.poi;
import com.itextpdf.text.DocumentException;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;


public class WordUtilTests {
    private HttpServletResponse response;
    private OutputStream outputStream;

    @Before
    public void setUp() throws IOException {
        response = Mockito.mock(HttpServletResponse.class);
        outputStream = new ByteArrayOutputStream();

        // 使用匿名类模拟 ServletOutputStream
        ServletOutputStream servletOutputStream = new ServletOutputStream() {
            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setWriteListener(WriteListener writeListener) {

            }

            @Override
            public void write(int b) throws IOException {
                outputStream.write(b);
            }
        };

        when(response.getOutputStream()).thenReturn(servletOutputStream);
    }

    @Test
    public void testWord() {
        String currentPath = System.getProperty("user.dir");
        String fileName =  currentPath+"/src/test/java/com/bonus/common/core/utils/poi/testExport.docx";
        WordUtil wordUtil = new WordUtil();
        wordUtil.addParagraph("hello world，这是一个正文", 14, "宋体");
        wordUtil.addTable(2, 3, new String[][] { { "1", "2", "3" }, { "4", "5", "6" } }, new int []{3000,3000,3000});
        XWPFDocument document = wordUtil.getDocument();
        try {
            wordUtil.addBarChart("柱状图", "", "数量", new String[]{"计划", "实际"}, new String[]{"第一季度", "第二季度", "第三季度"}, new double[][]{{1, 2,3}, {4, 5,6}},0,0);
            wordUtil.addPieChart("饼状图", new String[]{"1月份", "2月份", "3月份"}, new double[]{1, 2, 3},0,0);
            wordUtil.saveDocumentToLocal(fileName);
            wordUtil.saveDocumentToResponse(response);
            wordUtil. closeDocument();
        } catch (Exception ignored){
            System.out.print("异常发生" + ignored.getMessage());
        }
        File file = new File(fileName);
        assertTrue(file.exists());
    }

    @Test
    public void testWordWithImage() {
        String currentPath = System.getProperty("user.dir");
        String fileName =  currentPath+"/src/test/java/com/bonus/common/core/utils/poi/testExport1.docx";
        WordUtil wordUtil = new WordUtil();
        wordUtil.addParagraph("hello world，这是一个正文", 14, "宋体");
        wordUtil.addTable(2, 3, new String[][] { { "1", "2", "3" }, { "4", "5", "6" } }, new int []{3000,3000,3000});
        XWPFDocument document = wordUtil.getDocument();
        try {
            String imagePath = currentPath + "/src/test/java/com/bonus/common/core/utils/poi/register.jpg";
            wordUtil.insertImage(imagePath, 400, 400);
            wordUtil.saveDocumentToLocal(fileName);
            wordUtil. closeDocument();

        } catch (Exception ignored){
            System.out.print("异常发生" + ignored.getMessage());
        }
        File file = new File(fileName);
        assertTrue(file.exists());
    }
}
