package com.fs.hisStore.utils;

import com.alibaba.fastjson.JSON;
import com.fs.common.exception.CustomException;
import com.fs.hisStore.domain.FsTestReportScrm;
import com.fs.hisStore.mapper.FsTestReportScrmMapper;
import com.fs.hisStore.vo.FsTestReportVO;
import com.fs.system.oss.CloudStorageService;
import com.fs.system.oss.OSSFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.Map;


@Service
public class TestReportUtilScrm {
    @Autowired
    FsTestReportScrmMapper fsTestReportMapper;
    @Async
    public String getTestReportImageUrlByReportId(Long reportId) {
        FsTestReportVO fsTestReport = fsTestReportMapper.selectFsTestReportByReportIdVO(reportId);
        String testResult = fsTestReport.getTestResult();
        String conditioningPlanJson = fsTestReport.getConditioningPlanJson();
        List<Map<String,String>> maps = (List<Map<String,String>>) JSON.parse(conditioningPlanJson);
        String jx="";
        for (Map map : maps) {
            if (map.get("name").equals("解析")){
                jx=(String) map.get("value");
                maps.remove(map);
                break;
            }
        }
        File mb = new File("C:\\fs\\test.jpg");
        if (!mb.exists()) {
            // 创建目录
            throw  new CustomException("模板文件不存在");
        }
        BufferedImage image = null;
        try {
            image = ImageIO.read(mb);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 创建画笔(image为上一步的图片对象)
        Graphics2D pen = image.createGraphics();
        pen.setColor(Color.black);
        //处理字体 高清加边缘模糊
        pen.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        pen.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        pen.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        pen.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
        pen.setFont(new Font("黑体", Font.BOLD | Font.ITALIC, 102));
        pen.setColor(Color.decode("#996533"));

        String na = fsTestReport.getName();
        if (na.length()>9){
            na = na.substring(0, 8)+"...";
        }else {
            if (na.length()<7){
                penRect(pen,850,85);
                penRect(pen,940,85);
                penRect(pen,1030,85);
            }
        }
        penDrawString(pen,na,60,165);
        pen.setFont(new Font("黑体", Font.BOLD, 88));
        penDrawString(pen,fsTestReport.getTestResult(),476,413);

        pen.setFont(new Font("黑体", Font.BOLD, 43));
        if (jx!=""){
            if (jx.length()>22){
                String jx1 = jx.substring(0, 21);
                penDrawString(pen,jx1,100,500);
                String jx2 = jx.substring(21);
                if (jx2.length()>22){
                    jx2 = jx2.substring(0, 18);
                    jx2+="...";
                }
                penDrawString(pen,jx2,100,560);
            }else {
                penDrawString(pen,jx,100,500);
            }
        }
        String hl="--------------------------------------------------";
        int y=638;
        for (Map<String, String> map : maps) {
            if (y>1600){
                break;
            }
            if (y!=638){
                pen.setFont(new Font("黑体", Font.BOLD, 35));
                penDrawString(pen,hl,60,y+=60);
            }
            String name = map.get("name");
            String value = map.get("value");
            pen.fillRect(73, y+=30, 11, 52);
            pen.setFont(new Font("黑体", Font.BOLD , 58));
            penDrawString(pen,name,100,y+=47);
            pen.setFont(new Font("黑体", Font.BOLD, 38));

            if (value.length()>25){
                String v1 = value.substring(0, 24);
                if (y>1700){
                    penDrawString(pen,v1.substring(0,22)+"...",73,y+=70);
                    break;
                }
                penDrawString(pen,v1,73,y+=70);
                String v2 = value.substring(24);
                if (v2.length()>25){
                    String vt2 = value.substring(24, 48);
                    if (y>1700){
                        penDrawString(pen,vt2.substring(0,22)+"...",73,y+=70);
                        break;
                    }
                    penDrawString(pen,vt2,73,y+=60);
                    String v3 = v2.substring(24);
                    if (v3.length()>25){
                        String vt3 = v3.substring(0, 22);
                        penDrawString(pen,vt3+"...",73,y+=60);
                    }else {
                        penDrawString(pen,v3,73,y+=60);
                    }
                }else {
                    penDrawString(pen,v2,73,y+=60);
                }
            }else {
                penDrawString(pen,value,73,y+=70);
            }
            if(y>1700){
                break;
            }
        }
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png", os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        InputStream inputStream = new ByteArrayInputStream(os.toByteArray());
        CloudStorageService storage = OSSFactory.build();
        String url = storage.uploadSuffix(inputStream, ".jpg");
        System.out.println(url);
        FsTestReportScrm t = new FsTestReportScrm();
        t.setReportId(reportId);
        t.setImgUrl(url);
        fsTestReportMapper.updateFsTestReport(t);
        return url;
    }

    public void penDrawString( Graphics2D pen,String value,int x,int y){
        if (value!=null){
            pen.drawString(value,x,y);
        }
    }

    public void penRect( Graphics2D pen,int x,int y){
        double angle = Math.toRadians(10);
        int rectWidth = 10;
        int rectHeight = 90;
        pen.rotate(angle, x + rectWidth / 2, y + rectHeight / 2);
        pen.fillRect(x,y , rectWidth, rectHeight);
        pen.rotate(-angle, x + rectWidth / 2, y + rectHeight / 2);
    }

}
