package com.izouma.wenlvju.service.performance;


import com.izouma.wenlvju.ApplicationTests;
import com.izouma.wenlvju.domain.performance.Participant;
import com.izouma.wenlvju.exception.BusinessException;
import com.izouma.wenlvju.repo.performance.ParticipantRepo;
import net.coobird.thumbnailator.Thumbnails;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

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

public class ParticipantServiceTest extends ApplicationTests {

    @Autowired
    private ParticipantService participantService;
    @Autowired
    private ParticipantRepo    participantRepo;

    @Test
    public void test() throws IOException {
        BufferedImage shareImg = ImageIO.read(this.getClass().getResourceAsStream("/static/certificate.jpeg"));
        BufferedImage result = new BufferedImage(1100, 778, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = result.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setComposite(AlphaComposite.SrcOver);
        g.drawImage(shareImg, 0, 0, null);

        // 头像
        BufferedImage avatar = ImageIO.read(new URL("http://yskj.njlyw.cn:8081/files/image/2021-11-04-12-36-43bJEcdPkI.jpg"));
//        int size = Math.min(avatar.getWidth(), avatar.getHeight());
//        BufferedImage subImg = avatar
//                .getSubimage((avatar.getWidth() - size) / 2, (avatar.getHeight() - size) / 2, size, size);

        BufferedImage avatarImg = new BufferedImage(68 * 2, 80 * 2, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = avatarImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2.setComposite(AlphaComposite.SrcOver);
        g2.setBackground(Color.GREEN);
//        g2.setClip(new Ellipse2D.Float(0, 0, 68 * 2, 76 * 2)); //椭圆

        g2.drawImage(avatar, 0, 0, 68 * 2, 160, null);

        g.drawImage(avatarImg, 420 * 2, 50 * 2, 68 * 2, 160, null);


        // 文字
        g.setColor(new Color(0, 0, 0)); //根据图片的背景设置水印颜色
        Font font = new Font("黑体", Font.PLAIN, 30);
        String content = "张妍:";
        // 加水印
        g.setFont(font);              //设置字体
        //设置水印的坐标
        g.drawString(content, 160, 330);  //画出水印

        String content1 = "在2021“宁艺之星”社会艺术水平考级展演(览)中，表现优";
        //设置水印的坐标
        g.drawString(content1, 215, 390);
        String content2 = "秀，被评为“一等奖”，特发此证，以资鼓励。";
        g.drawString(content2, 160, 450);

        g.dispose();
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
        Thumbnails.of(result)
                .scale(1)
                .outputQuality(.9f)
                .outputFormat("jpg")
                .toOutputStream(out);
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());

        File file = new File("/Users/qiufangchao/Desktop/2.jpg");
        byte[] buffer = new byte[in.available()];
        in.read(buffer);
        OutputStream outStream = new FileOutputStream(file);
        outStream.write(buffer);

    }

    @Test
    public void test2() {
        try {
            new URL("http://yskj.njlyw.cn:8081/files/image/idno/320115201107050346Certificate.jpg");
//            BufferedImage avatar = ImageIO.read(new URL("http://yskj.njlyw.cn:8081/files/image/idno/320115201107050346Certificate.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test3() throws IOException {
        Participant participant = participantRepo.findById(3048L).orElseThrow(new BusinessException("无记录"));
        System.out.println(participantService.createAward(participant, "二等奖"));
    }

}