package com.moka.gdtauto.util;

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

/**
 * 图片水印简单测试程序
 * 可以直接运行main方法进行测试
 */
public class ImageWatermarkSimpleTest {

    public static void main(String[] args) {
        try {
            String outputDir = "target/test-output/watermark";
            File dir = new File(outputDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            // 创建测试图片
            String inputPath = outputDir + "/test-input.jpg";
            createTestImage(inputPath, 1920, 1080);
            System.out.println("✓ 创建测试图片成功: " + inputPath);

            // 测试1: 基本水印功能
            String outputPath1 = outputDir + "/output-basic.jpg";
            ImageWatermarkUtil.addWatermark(inputPath, outputPath1, "《绝世龙王》");
            System.out.println("✓ 测试1通过: 基本水印功能 -> " + outputPath1);

            // 测试2: 自定义位置和字体
            String outputPath2 = outputDir + "/output-custom.jpg";
            ImageWatermarkUtil.addWatermark(inputPath, outputPath2, "《绝世龙王》", 60, 100, 100);
            System.out.println("✓ 测试2通过: 自定义位置和字体 -> " + outputPath2);

            // 测试3: 带压缩的水印
            String outputPath3 = outputDir + "/output-compressed.jpg";
            ImageWatermarkUtil.addWatermarkWithCompression(inputPath, outputPath3, "《绝世龙王》", 0.8);
            System.out.println("✓ 测试3通过: 带压缩的水印 -> " + outputPath3);

            // 测试4: PNG格式
            String inputPng = outputDir + "/test-input.png";
            createTestImage(inputPng, 1920, 1080);
            String outputPng = outputDir + "/output.png";
            ImageWatermarkUtil.addWatermark(inputPng, outputPng, "《绝世龙王》");
            System.out.println("✓ 测试4通过: PNG格式 -> " + outputPng);

            // 测试5: 多个剧名
            String[] dramaNames = {
                    "《霸道总裁爱上我》",
                    "《重生之都市修仙》",
                    "《全职高手》"
            };
            for (int i = 0; i < dramaNames.length; i++) {
                String outputPath = outputDir + "/output-drama-" + i + ".jpg";
                ImageWatermarkUtil.addWatermark(inputPath, outputPath, dramaNames[i]);
                System.out.println("✓ 测试5." + (i + 1) + "通过: 剧名 " + dramaNames[i] + " -> " + outputPath);
            }

            System.out.println("\n========================================");
            System.out.println("所有测试通过！✓");
            System.out.println("测试结果保存在: " + new File(outputDir).getAbsolutePath());
            System.out.println("========================================");

        } catch (Exception e) {
            System.err.println("✗ 测试失败: " + e.getMessage());
            e.printStackTrace();
            System.exit(1);
        }
    }

    /**
     * 创建测试图片
     */
    private static void createTestImage(String path, int width, int height) throws Exception {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();

        // 填充渐变背景
        GradientPaint gradient = new GradientPaint(0, 0, Color.BLUE, width, height, Color.CYAN);
        g2d.setPaint(gradient);
        g2d.fillRect(0, 0, width, height);

        // 添加文字
        g2d.setColor(Color.WHITE);
        g2d.setFont(new Font("Arial", Font.BOLD, 48));
        String text = "Test Image " + width + "x" + height;
        FontMetrics fm = g2d.getFontMetrics();
        int textWidth = fm.stringWidth(text);
        g2d.drawString(text, (width - textWidth) / 2, height / 2);

        g2d.dispose();

        // 保存图片
        String format = path.endsWith(".png") ? "png" : "jpg";
        ImageIO.write(image, format, new File(path));
    }
}
