package com.cool.store.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;

import java.awt.*;
import java.io.*;
import java.util.Date;

//生成资格面试通过函的工具
public class PassLetterUtils {

    /**
     * 生成通过函的方法
     * 返回通过函编码
     * @param partnerName 加盟商姓名
     * @param verifyCity  审批城市
     * @param passCode 通过函编码
     * @param passTime 审批通过时间
     */
    public static ByteArrayOutputStream genPassLetter(String partnerName, String passCode, String verifyCity, Date passTime) {
        String passTimeStr = DateUtil.format(passTime, "yyyy年MM月dd日");
        Document document = new Document();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            //1. 创建 pdf document
//            FileOutputStream outputStream = new FileOutputStream("passLetter.pdf");
            PdfWriter.getInstance(document, outputStream);
            document.open();

            //2. 添加背景图片
            Image img = Image.getInstance(PassLetterUtils.class.getResource("/static/passLetterBg.jpg").toString());
            PDFUtils.setBackgroundImgA4(document, img);
            document.close();

            //3. 填写通过函模板信息
            addContentToPdf(outputStream, partnerName + "  先生/女士", 122, 638);
            addContentToPdf(outputStream, passCode, 122, 557);
            addContentToPdf(outputStream, verifyCity, 155, 492);
            addContentToPdf(outputStream, "60天", 135, 448);
            addContentToPdf(outputStream, passTimeStr, 385, 152);
            return outputStream;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        finally {
            try {
                document.close();
                outputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 生成 passCode 的方法，拆分出来方便单独获取 passCode
     * @return
     */
    public static String genPassCode(Date passTime) {
        String randomNum = RandomUtil.randomNumbers(5);
        String passCode = "HSAY" + DateUtil.format(passTime, "yyMMdd") + "-" + randomNum;
        return passCode;
    }

    private static void addContentToPdf(ByteArrayOutputStream outputStream, String content, float x, float y) {
        try {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
            PdfReader pdfReader = new PdfReader(inputStream);
            String fontFamilyPath = PassLetterUtils.class.getResource("/static/Alibaba-PuHuiTi-Regular.ttf").toString();
            BaseFont font = BaseFont.createFont(fontFamilyPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            PDFUtils.putParagraphAbsolutely(pdfReader, outputStream, x, y, content, font, 19, 1, new Color(255, 82,25));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
