package org.ta.pddserver.lotto;

import com.google.zxing.WriterException;
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 javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImagePdfGenerator {

    public static byte[] generatePdfWithTextImage(String mailCode,
                                                  String portCode,
                                                  String receiverName,
                                                  String phone,
                                                  String postCode,
                                                  String address,
                                                  String orderCode,
                                                  String area1,
                                                  String area2,
                                                  String area3,
                                                  String area4)  {
        try {
            // 1. 将文字转为图片（示例：韩文“안녕하세요，世界！”）
            byte[] imageBytes = TextToImageUtils.textToImage(
                    mailCode,
                    portCode,
                    receiverName,
                    phone,
                    postCode,
                    address,
                    orderCode,
                    area1,
                    area2,
                    area3,
                    area4
            );
//        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();
    }
}