package org.ta.pddserver.lotto;

import com.google.zxing.WriterException;
import com.itextpdf.commons.utils.Base64;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class TextToImageUtils {
/**
 * 将文字转换为图片（PNG格式）
 * @param text 待转换的文字（支持中文、韩文等）
 * @param code1 条形码
 * @param fontSize 字体大小
 * @param width 图片宽度
 * @param height 图片高度
 * @return 图片字节数组（PNG格式）
 */
    /**
     * 生成面单
     * @param mailCode 快递面单
     * @param receiverName 收件人姓名
     * @param phone 电话
     * @param postCode 邮编
     * @param address 地址
     * @param orderCode 出库单号
     * @param area1 区域信息1
     * @param area2 区域信息2
     * @param area3 区域信息3
     * @param area4 区域信息4
     * @return 面单byte
     * @throws Exception 异常
     */
    public static byte[] textToImage( String mailCode,
                                     String portCode,
                                     String receiverName,
                                     String phone,
                                     String postCode,
                                     String address,
                                     String orderCode,
                                     String area1,
                                     String area2,
                                     String area3,
                                     String area4) throws Exception {
        int width = 2268;
        int height = 1512;
        int startX = 60;
        int startY = 60;
        // 1. 创建 BufferedImage 作为画布
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        System.out.println("width = " + width + ", height = " + height);
        Graphics2D g2d = image.createGraphics();
        // 设置绘图属性（抗锯齿、渲染质量等）
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 文字抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); // 文字平滑
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); // 渲染质量

        // 2. 设置背景为白色
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, width, height);

        // 3、默认字体
        Font font0 = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("./HYGothic.ttf")).deriveFont((float) 86);
        Font font01 = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("./HYGothic.ttf")).deriveFont((float) 70);
        Font font1 = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("./HYGothic.ttf")).deriveFont((float) 52);
        g2d.setColor(Color.BLACK); // 文字颜色
        Font font2 = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("./HYGothic.ttf")).deriveFont((float) 46);
        // logo1
        String logo1String = "[encoded data, 6792 characters]";
        byte[] logo1Byte = Base64.decode(logo1String);
        ByteArrayInputStream logo1ByteIs = new ByteArrayInputStream(logo1Byte);
        BufferedImage logo1Img = ImageIO.read(logo1ByteIs);
        g2d.drawImage(logo1Img, startX + 0, startY + 0, 610, 70, null);

        // logo2
//        String logo2String = "iVBORw0KGgoAAAANSUhEUgAAAJwAAAB4CAYAAAAHbZvZAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoV2luZG93cykiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6NEQzRkU2MjU3RjJCMTFGMDkwNzFDQzQwMUFFNEE3REMiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6NEQzRkU2MjY3RjJCMTFGMDkwNzFDQzQwMUFFNEE3REMiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo0RDNGRTYyMzdGMkIxMUYwOTA3MUNDNDAxQUU0QTdEQyIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDo0RDNGRTYyNDdGMkIxMUYwOTA3MUNDNDAxQUU0QTdEQyIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/Poa5e5EAAAETSURBVHja7NwxDkBQEEVRT+zVmqz20fxWI0Q4p/r15GaoJm0neMpsBAiOz1rGI8lmHNzl+HVbbTh8UhEcCA7BgeAQHIIDwSE4EByCA8EhOAQHgkNwIDgEB4JDcAgOBIfgQHAIDgSH4BAcCA7Bwan86eTqW2/gjdtpNhwIDsGB4BAcgjMCBIfgQHAIDgSH4BAcCA7BgeAQHAgOwSE4EByCA8EhOBAcgkNwIDgEB4JDcAgOBIfgQHAIDgSH4BAcCA7BgeAQHAgOwSE4EByCA8EhOBAcgkNwIDgEB4JDcAgOBIfgQHAIDgSH4BAcCA7BgeAQHAgOwSE4EByCA8EhOAQHgkNwIDgEByfS1hSw4RAcXLYLMADPTBHnat0YcgAAAABJRU5ErkJggg==";
//        byte[] logo2Byte = Base64.decode(logo2String);
//        ByteArrayInputStream logo2ByteIs = new ByteArrayInputStream(logo2Byte);
//        BufferedImage logo2Img = ImageIO.read(logo2ByteIs);
//        g2d.drawImage(logo2Img, 1540, 540, 610, 466, null);

        // 运单号
        g2d.setFont(font1);
        g2d.drawString("운송장번호 " + generateSplitMailCode(mailCode), startX + 682, startY + 52);

        // 打印时间
        g2d.setFont(font2);
        g2d.drawString("2025.11.13", startX + 1600, startY + 76);
        // 打印次数
        g2d.drawString("1/1", startX + 2080, startY + 76);

        // 条形码1
        byte[] code1Bytes = BarcodeUtils.generate39Barcode(portCode, 630, 240);
        ByteArrayInputStream code1BytesIs = new ByteArrayInputStream(code1Bytes);
        BufferedImage code1Img = ImageIO.read(code1BytesIs);
        g2d.drawImage(code1Img, startX + 0, startY + 106, 630, 240, null);

        // 地区码1
        g2d.setFont(font0);
        g2d.drawString(area1, startX + 710, startY + 220);

        // 地区信息1
        g2d.setFont(font01);
        g2d.drawString(area2, startX + 680, startY + 345);

        // 地区信息2
        g2d.setFont(font01);
        g2d.drawString(area3, startX + 1466, startY + 220);

        // 地区信息3
        g2d.setFont(font01);
        g2d.drawString(area4, startX + 1466, startY + 330);


        // 收件人 电话
        g2d.setFont(font1);
        g2d.drawString("받는분:" + receiverName + "  " + phone, startX + 0, startY + 440);

        // 邮编
        g2d.setFont(font1);
        g2d.drawString(postCode, startX + 0, startY + 510);

        // 详细地址
        g2d.setFont(font1);
        g2d.drawString(address, startX + 0, startY + 580);

        // 商品信息
        g2d.setFont(font1);
        g2d.drawString("상품명: E-commerce consuming goods", startX + 0, startY + 700);

        byte[] code4Bytes = BarcodeUtils.generate128Barcode(mailCode, 704, 182);
        ByteArrayInputStream code4BytesIs = new ByteArrayInputStream(code4Bytes);
        BufferedImage code4Img = ImageIO.read(code4BytesIs);
        // 条形码2
        g2d.drawImage(code4Img, startX + 1500, startY + 381, 672, 106, null);

        // 条形码3
        g2d.drawImage(code4Img, startX + 350, startY + 864, 852, 258, null);

        // 条形码4
        g2d.drawImage(code4Img, startX + 1420, startY + 1060, 704, 182, null);

        // 快递单号
        g2d.setFont(font2);
        g2d.drawString(mailCode, startX + 1548, startY + 1308);


        // 辅助文字
        g2d.setFont(font1);
        g2d.drawString("배송비고:", startX + 0, startY + 1220);

        // 集运单号
        g2d.setFont(font1);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        FontMetrics metrics = g2d.getFontMetrics();
        int x = (width - metrics.stringWidth(orderCode)) / 2; // 水平居中
        g2d.drawString(orderCode, startX + x, startY + 1370);

        g2d.dispose();

        // 6. 将图片转为字节数组（PNG格式）
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", baos);
        return baos.toByteArray();
    }

    public static String generateSplitMailCode(String mailCode) {
        return mailCode.substring(0, 4) + "-" + mailCode.substring(4, 8) + "-" + mailCode.substring(8, 12);
    }

    public static byte[] commonBarCodeToImage( String mailCode
                                     ) throws Exception {
        int width = 1770;
        int height = 1180;
        int startX = 0;
        int startY = 0;

        // 1. 创建 BufferedImage 作为画布
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        System.out.println("width = " + width + ", height = " + height);
        Graphics2D g2d = image.createGraphics();
        // 设置绘图属性（抗锯齿、渲染质量等）
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 文字抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); // 文字平滑
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); // 渲染质量
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        // 2. 设置背景为白色
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, width, height);
        g2d.setColor(Color.BLACK);

        // 集运单号
        Font font0 = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("./HYGothic.ttf")).deriveFont((float) 140);
        g2d.setFont(font0);

        FontMetrics metrics = g2d.getFontMetrics();
        int x = (width - metrics.stringWidth(mailCode)) / 2; // 水平居中

        g2d.drawString(mailCode, startX + 500, 160);
        g2d.drawString(mailCode, startX + 500, 1000);

        byte[] code4Bytes = BarcodeUtils.generate39Barcode(mailCode, 1300, 500);
        ByteArrayInputStream code4BytesIs = new ByteArrayInputStream(code4Bytes);
        BufferedImage code4Img = ImageIO.read(code4BytesIs);
        // 条形码2
        g2d.drawImage(code4Img, startX + 200, startY + 260, 1300, 500, null);



        g2d.dispose();

        // 6. 将图片转为字节数组（PNG格式）
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", baos);
        return baos.toByteArray();
    }
}
