package com.ycwl.basic.image.watermark.operator;

import com.ycwl.basic.image.watermark.entity.WatermarkInfo;
import com.ycwl.basic.image.watermark.exception.ImageWatermarkException;
import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class NormalWatermarkOperator implements IOperator {
    private static final String FONT_PATH = "/PingFang_SC.ttf";
    public static String defaultFontName;
    public static float FONT_GLOBAL_OFFSET_PERCENT = 0;
    static {
        try {
            // 加载字体文件流
            InputStream fontStream = LeicaWatermarkOperator.class.getResourceAsStream(FONT_PATH);
            if (fontStream == null) {
                throw new RuntimeException("字体文件未找到！路径：" + FONT_PATH);
            }

            // 创建字体对象
            Font customFont = Font.createFont(Font.TRUETYPE_FONT, fontStream);

            // 注册字体到系统
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            ge.registerFont(customFont);

            // 更新默认字体名称为新字体的逻辑名称
            defaultFontName = customFont.getName(); // 如 "PingFang SC"
            FONT_GLOBAL_OFFSET_PERCENT = -0.3f;
        } catch (FontFormatException | IOException e) {
            log.error("加载字体文件失败", e);
            defaultFontName = "宋体";
        }
    }
    public static int EXTRA_BORDER_PX = 0;
    public static int OFFSET_Y = 90;
    public static Color BG_COLOR = Color.WHITE;
    public static int QRCODE_SIZE = 100;
    public static int QRCODE_OFFSET_X = 10;

    public static int SCENIC_FONT_SIZE = 42;
    public static Color scenicColor = Color.white;
    public static int DATETIME_FONT_SIZE = 42;
    public static Color datetimeColor = Color.white;

    @Override
    public File process(WatermarkInfo info) throws ImageWatermarkException {
        BufferedImage baseImage;
        BufferedImage qrcodeImage;
        try {
            baseImage = ImageIO.read(info.getOriginalFile());
            qrcodeImage = ImageIO.read(info.getQrcodeFile());
        } catch (IOException e) {
            throw new ImageWatermarkException("图片打开失败");
        }
        // 新图像画布
        BufferedImage newImage = new BufferedImage(baseImage.getWidth() + 2 * EXTRA_BORDER_PX, baseImage.getHeight() + 2 * EXTRA_BORDER_PX, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = newImage.createGraphics();
        g2d.setColor(BG_COLOR);
        g2d.fillRect(0, 0, newImage.getWidth(), newImage.getHeight());
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.drawImage(baseImage, EXTRA_BORDER_PX, EXTRA_BORDER_PX, null);
        int newQrcodeHeight = QRCODE_SIZE;
        int newQrcodeWidth = (int) (newQrcodeHeight * 1.0 / qrcodeImage.getHeight() * qrcodeImage.getWidth());
        Font scenicFont = new Font(defaultFontName, Font.PLAIN, SCENIC_FONT_SIZE);
        Font datetimeFont = new Font(defaultFontName, Font.PLAIN, DATETIME_FONT_SIZE);
        FontMetrics scenicFontMetrics = g2d.getFontMetrics(scenicFont);
        FontMetrics datetimeFontMetrics = g2d.getFontMetrics(datetimeFont);
        int scenicLineHeight = scenicFontMetrics.getHeight();
        int dtLineHeight = datetimeFontMetrics.getHeight();
        int scenicLineWidth = scenicFontMetrics.stringWidth(info.getScenicLine());
        int datetimeLineWidth = scenicFontMetrics.stringWidth(info.getDatetimeLine());
        int offsetX = (newImage.getWidth() - newQrcodeWidth - QRCODE_OFFSET_X - Math.max(scenicLineWidth, datetimeLineWidth)) / 2;
        int offsetY = EXTRA_BORDER_PX + baseImage.getHeight() - OFFSET_Y - newQrcodeHeight;
        g2d.drawImage(qrcodeImage, offsetX, offsetY, newQrcodeWidth, newQrcodeHeight, null);
        g2d.setFont(scenicFont);
        g2d.setColor(scenicColor);
        g2d.drawString(info.getScenicLine(), offsetX + newQrcodeWidth + QRCODE_OFFSET_X, offsetY + scenicLineHeight + FONT_GLOBAL_OFFSET_PERCENT * scenicLineHeight);
        g2d.setFont(datetimeFont);
        g2d.setColor(datetimeColor);
        g2d.drawString(info.getDatetimeLine(), offsetX + newQrcodeWidth + QRCODE_OFFSET_X, offsetY + scenicLineHeight + dtLineHeight + FONT_GLOBAL_OFFSET_PERCENT * dtLineHeight);
        try {
            ImageIO.write(newImage, "png", info.getWatermarkedFile());
        } catch (IOException e) {
            throw new ImageWatermarkException("图片保存失败");
        } finally {
            g2d.dispose();
        }
        return info.getWatermarkedFile();
    }
}
