package com.moka.gdtauto.util;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

/**
 * 图片水印工具类测试
 *
 * @author moka
 */
@Slf4j
class ImageWatermarkUtilTest {

    private static final String TEST_OUTPUT_DIR = "target/test-output/watermark";
    private static final String TEST_INPUT_IMAGE = TEST_OUTPUT_DIR + "/test-input.jpg";

    @BeforeEach
    void setUp() throws IOException {
        // 创建测试输出目录
        File outputDir = new File(TEST_OUTPUT_DIR);
        if (!outputDir.exists()) {
            outputDir.mkdirs();
        }

        // 生成测试用的图片（模拟原始图片）
        createTestImage(TEST_INPUT_IMAGE, 1920, 1080);
    }

    /**
     * 测试基本水印功能
     */
    @Test
    void testAddWatermark() throws IOException {
        String outputPath = TEST_OUTPUT_DIR + "/output-basic.jpg";
        String watermarkText = "《绝世龙王》";

        ImageWatermarkUtil.addWatermark(TEST_INPUT_IMAGE, outputPath, watermarkText);

        // 验证输出文件是否存在
        File outputFile = new File(outputPath);
        assert outputFile.exists() : "输出文件不存在";
        assert outputFile.length() > 0 : "输出文件为空";

        log.info("基本水印测试通过，输出文件: {}", outputPath);
    }

    /**
     * 测试自定义位置和字体大小
     */
    @Test
    void testAddWatermarkWithCustomSettings() throws IOException {
        String outputPath = TEST_OUTPUT_DIR + "/output-custom.jpg";
        String watermarkText = "《绝世龙王》";

        // 自定义：字体大小60，位置(100, 100)
        ImageWatermarkUtil.addWatermark(TEST_INPUT_IMAGE, outputPath, watermarkText, 60, 100, 100);

        File outputFile = new File(outputPath);
        assert outputFile.exists() : "输出文件不存在";
        assert outputFile.length() > 0 : "输出文件为空";

        log.info("自定义设置水印测试通过，输出文件: {}", outputPath);
    }

    /**
     * 测试带压缩的水印功能
     */
    @Test
    void testAddWatermarkWithCompression() throws IOException {
        String outputPath = TEST_OUTPUT_DIR + "/output-compressed.jpg";
        String watermarkText = "《绝世龙王》";

        // 缩放到0.8倍
        ImageWatermarkUtil.addWatermarkWithCompression(TEST_INPUT_IMAGE, outputPath, watermarkText, 0.8);

        File inputFile = new File(TEST_INPUT_IMAGE);
        File outputFile = new File(outputPath);

        assert outputFile.exists() : "输出文件不存在";
        assert outputFile.length() > 0 : "输出文件为空";
        assert outputFile.length() < inputFile.length() : "压缩后的文件应该更小";

        log.info("压缩水印测试通过，输入大小: {} bytes, 输出大小: {} bytes", 
                inputFile.length(), outputFile.length());
    }

    /**
     * 测试不同格式的图片
     */
    @Test
    void testAddWatermarkPNG() throws IOException {
        String inputPath = TEST_OUTPUT_DIR + "/test-input.png";
        String outputPath = TEST_OUTPUT_DIR + "/output.png";
        String watermarkText = "《绝世龙王》";

        // 创建PNG格式的测试图片
        createTestImage(inputPath, 1920, 1080);

        ImageWatermarkUtil.addWatermark(inputPath, outputPath, watermarkText);

        File outputFile = new File(outputPath);
        assert outputFile.exists() : "PNG输出文件不存在";
        assert outputFile.length() > 0 : "PNG输出文件为空";

        log.info("PNG格式水印测试通过，输出文件: {}", outputPath);
    }

    /**
     * 测试多个不同的剧名
     */
    @Test
    void testMultipleWatermarks() throws IOException {
        String[] dramaNames = {
                "《绝世龙王》",
                "《霸道总裁爱上我》",
                "《重生之都市修仙》",
                "《全职高手》"
        };

        for (int i = 0; i < dramaNames.length; i++) {
            String outputPath = TEST_OUTPUT_DIR + "/output-drama-" + i + ".jpg";
            ImageWatermarkUtil.addWatermark(TEST_INPUT_IMAGE, outputPath, dramaNames[i]);

            File outputFile = new File(outputPath);
            assert outputFile.exists() : "输出文件不存在: " + dramaNames[i];
            log.info("剧名 {} 水印添加成功", dramaNames[i]);
        }

        log.info("多剧名水印测试全部通过");
    }

    /**
     * 创建测试用的图片
     */
    private void createTestImage(String path, int width, int height) throws IOException {
        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));
        g2d.drawString("Test Image " + width + "x" + height, width / 2 - 200, height / 2);

        g2d.dispose();

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

        log.info("创建测试图片: {}, 尺寸: {}x{}", path, width, height);
    }
}
