package com.bonus.common.core.utils.poi;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import org.junit.Before;
import org.junit.Test;

import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.mockito.Mockito;

import static org.mockito.Mockito.when;

public class PdfUtilTests {
    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 testPdf() throws IOException, DocumentException, InterruptedException {
        String currentPath = System.getProperty("user.dir");
        String fileName =  currentPath+ "/src/test/java/com/bonus/common/core/utils/poi/testExport2.pdf";
        PdfUtil pdfUtil = new PdfUtil(fileName,null,0);
        BaseFont baseFont = pdfUtil.getBaseFont();
        Document document = pdfUtil.getDocument();
        Font font = new Font(baseFont,24, Font.NORMAL);
        Paragraph paragraph = new Paragraph("字体测试数据",font);
        document.add(paragraph);
        pdfUtil.addTitle("单元测试");
        pdfUtil.addStyledParagraph("测试文字", true, false, 12, font);

        pdfUtil.addTable(new String[] { "姓名", "年龄" }, new String[][] { { "张三", "18" }, { "李四", "19" } }, 2, null);
        pdfUtil.addBarChart("柱状图", "月份", "数量", new String[] { "1", "2", "3" }, new String[] { "1", "2", "3" }, new double[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } },400,400);
        pdfUtil.addPieChart("饼状图", new String[] { "1月份", "2月份", "3月份" }, new double[] { 1, 2, 3 },400,400);
        pdfUtil.addParagraph("测试文字", null);
        pdfUtil.addStyledParagraph("测试文字样式", true, true, 12, null);
        String imagePath = currentPath + "/src/test/java/com/bonus/common/core/utils/poi/register.jpg";
        pdfUtil.addImage(imagePath,400,400);
        pdfUtil.close();
    }

    @Test
    public void testPdfToResponse() throws IOException, DocumentException, InterruptedException {
        String currentPath = System.getProperty("user.dir");
        PdfUtil pdfUtil = new PdfUtil(response,null,0);
        pdfUtil.addTitle("单元测试");
        pdfUtil.addStyledParagraph("测试文字", true, false, 12, null);
        pdfUtil.close();
    }

}
