package com.bjtdba.poster.utils;

import com.bjtdba.poster.entity.Poster;
import org.springframework.stereotype.Service;

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

@Service
public class PosterUtil {

    /**
     * 生成海报
     *
     * @return
     */
    public File drawImg(Poster poster,int uid) {
        try {
            File url = drawPoster(poster,uid);
            return url;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new File("");
    }

    File drawPoster(Poster poster,int uid) throws Exception {
        System.out.println("开始生成二维码");
        //开始时间
        long startTime = System.currentTimeMillis();
        String qrCodeUrl = poster.getQrCodeUrl();
        BufferedImage qrCodeImage = ImageIO.read(new URL(qrCodeUrl));//获取二维码图像

        int width = poster.getWidth();
        int height = poster.getHeight();
        //画布
        BufferedImage canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) canvas.getGraphics();
        BufferedImage bg = null;
        //---------------------------------------------------------------------------------------------------------------------------------
        bg = ImageIO.read(new URL(poster.getPathUrl()));
        g.drawImage(bg.getScaledInstance(width, height, Image.SCALE_DEFAULT), 0, 0, null);
        //--------------------获取头像相关信息准备画头像----------------------------------------------------------------------------------------
        System.out.println("-----------------------------------------------"+poster.getPortraitCodeUrl());
        if(poster.getPortraitCodeUrl()!=null){
            String portraitCodeUrl = poster.getPortraitCodeUrl();//微信头像
            BufferedImage portraitCodeImage = ImageIO.read(new URL(portraitCodeUrl));//头像
            // 设置文字抗锯齿
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            //画头像
            g.drawImage(portraitCodeImage.getScaledInstance(poster.getPortraitCodeWidth(),poster.getPortraitCodeHeight(),Image.SCALE_SMOOTH),poster.getPortraitCodeX(),poster.getPortraitCodeY(),null);
            //名字
            g.setColor(poster.getNameColor());
            g.setFont(poster.getNameFont());
            g.drawString(poster.getName(), poster.getNameX(), poster.getNameY());
        }
        //画二维码
        g.drawImage(qrCodeImage.getScaledInstance(poster.getQrCodeWidth(), poster.getQrCodeHeight(), Image.SCALE_SMOOTH),
                poster.getQrCodeX(), poster.getQrCodeY(), null);

        g.dispose();

        //生成图片地址
        String url = "/Users/hanyuxing/qrcode/"+uid+"/"+startTime+".png";
        File file = new File(url);
        //当文件夹不存在时，mkdirs会自动创建多层目录，区别于mkdir．(mkdir如果父目录不存在则会抛出异常)
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
        File resultImg = new File("/Users/hanyuxing/qrcode/"+uid+"/" + startTime + ".png");
        ImageIO.write(canvas, "png", resultImg);

        System.out.println("生成成功！");
        System.out.println("耗时: " + (System.currentTimeMillis() - startTime) / 1000.0 + "s");
        System.out.println("生成文件路径: " + resultImg.getAbsolutePath());

        //返回图片地址
        //return resultImg.getAbsolutePath();
        return resultImg;
    }
}
