package org.ta.pddserver.lotto;

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.properties.HorizontalAlignment;

import java.io.ByteArrayOutputStream;

public class ImagePdfUnpackGenerator {

    public static byte[] generatePdfWithUnpackTextImage(String orderCode)  {
        try {
            // 1. 将文字转为图片（示例：韩文“안녕하세요，世界！”）
            byte[] imageBytes = TextToImageUnpackUtils.textToImage(
                    orderCode

            );
//        return imageBytes;


            // 2. 初始化PDF
            ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
            PdfWriter writer = new PdfWriter(pdfBaos);
            PdfDocument pdfDoc = new PdfDocument(writer);
            Document document = new Document(pdfDoc, new PageSize((int) (150 * 300 / 25.4), (int) (100 * 300 / 25.4)));

            // 3. 将图片嵌入PDF
            ImageData imageData = ImageDataFactory.create(imageBytes);
            Image image = new Image(imageData);
            // 设置图片在PDF中居中显示
            image.setHorizontalAlignment(HorizontalAlignment.LEFT);
            document.add(image);

            // 4. 关闭文档
            document.close();
            return pdfBaos.toByteArray();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


    public static byte[] generateCode() throws Exception {
        ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
        PdfWriter writer = new PdfWriter(pdfBaos);
        PdfDocument pdfDoc = new PdfDocument(writer);

        // 2. 初始化PDF
        Document document = new Document(pdfDoc, new PageSize((int) (150 * 300 / 25.4), (int) (100 * 300 / 25.4)));

        for (int i = 1; i < 10; i ++) {
            for (int i1 = 1; i1 < 12; i1 ++) {
                for (int i2 = 1; i2 < 5; i2 ++) {
                    for (int i3 = 1; i3 < 3; i3 ++) {
                        if (i3 == 2 && (i2 == 1|| i2 == 4)) {
                            continue;
                        }
                        // 1. 将文字转为图片（示例：韩文“안녕하세요，世界！”）
                        String code = "A" + i + "-" + i1 + "-" + i2 + "-" + i3;
                        byte[] imageBytes = TextToImageUtils.commonBarCodeToImage(code);

                        // 3. 将图片嵌入PDF
                        ImageData imageData = ImageDataFactory.create(imageBytes);
                        Image image = new Image(imageData);
                        // 设置图片在PDF中居中显示
                        image.setHorizontalAlignment(HorizontalAlignment.LEFT);
                        document.add(image);
                    }
                }



            }


        }
        // 4. 关闭文档
        document.close();
        return pdfBaos.toByteArray();
    }
}