package com.ywt.mg.core.utils;

/**
 * @author daiyihua
 * @create 2023-02-27 11:01
 * @program download_service
 * @description 创建PDF
 **/

import com.itextpdf.text.*;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class GeneratePdf {

    private static final Logger LOGGER = LoggerFactory.getLogger(GeneratePdf.class);

    public void handleText(PdfWriter writer, String content, String color,
                           float x, float y, float z) {
        PdfContentByte canvas = writer.getDirectContent();
        Phrase phrase = new Phrase(content);
        if (color != null) {
            phrase = new Phrase(content, FontFactory.getFont(FontFactory.COURIER, 12, Font.NORMAL));
        }
        ColumnText.showTextAligned(canvas, Element.ALIGN_UNDEFINED, phrase, x, y, z);
    }

    public File Pdf(String imagePath, String mOutputPdfFileName) {
        Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
        try {
            PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(
                    mOutputPdfFileName));
            doc.open();

            doc.newPage();
            Image png1 = Image.getInstance(imagePath);
            float height = png1.getHeight();
            float width = png1.getWidth();
            int percent = this.getPercent2(height, width);
            png1.setAlignment(Image.MIDDLE);
            png1.setAlignment(Image.TEXTWRAP);
            png1.scalePercent(percent + 3);
            doc.add(png1);
            this.handleText(writer, "This is a test", "red", 400, 725, 0);
            doc.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        File mOutputPdfFile = new File(mOutputPdfFileName);
        if (!mOutputPdfFile.exists()) {
            mOutputPdfFile.deleteOnExit();
            return null;
        }
        return mOutputPdfFile;
    }

    public void insertText(String content, String mOutputPdfFileName) {
        Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
        try {
            PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName));
            doc.open();
            doc.newPage();
            Phrase phrase = new Phrase(content);
            doc.add(phrase);
            doc.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建pdf，添加文字和图片
     *
     * @param content            文字
     * @param imagePathList      图片list
     * @param mOutputPdfFileName 文件名
     */
    public void Pdf(String content, List<String> imagePathList, String mOutputPdfFileName) {
        Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
        try {
            PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName));
            doc.open();
            doc.newPage();
            // 使用宋体显示中文
            String songtiFontName = "./config_file/simsun.ttc";
            songtiFontName += ",1";
            Font songtiFont = new Font(BaseFont.createFont(songtiFontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED));
            Phrase phrase = new Phrase(content, songtiFont);
            doc.add(phrase);
            for (String imagePath : imagePathList) {
                doc.newPage();
                Image png1 = Image.getInstance(imagePath);
                float height = png1.getHeight();
                float width = png1.getWidth();
                int percent = this.getPercent2(height, width);
                png1.setAlignment(Image.MIDDLE);
                png1.setAlignment(Image.TEXTWRAP);
                png1.scalePercent(percent + 3);
                doc.add(png1);
            }
            doc.close();
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("GeneratePdfUtil#Pdf({},{},{}),{}", content, imagePathList, mOutputPdfFileName, e.getMessage(), e);
        }
    }

    public int getPercent1(float h, float w) {
        int p = 0;
        float p2 = 0.0f;
        if (h > w) {
            p2 = 297 / h * 100;
        } else {
            p2 = 210 / w * 100;
        }
        p = Math.round(p2);
        return p;
    }

    private int getPercent2(float h, float w) {
        int p = 0;
        float p2 = 0.0f;
        p2 = 530 / w * 100;
        p = Math.round(p2);
        return p;
    }
/*
    public static void main(String[] args) {
        GeneratePdf gp = new GeneratePdf();
        String pdfUrl = "C:\\pdf.pdf";
        File file = gp
                .Pdf("C:\\683.JPG",pdfUrl);
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
*/
}