package com.sifang.modules.customer.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

import java.io.IOException;

public class CreatePdfUtil {
    public static BaseFont bfChinese;

    /**
     * 定义全局的字体静态变量
     */
    public static Font titlefont;
    public static Font titleSecondFont;
    public static Font headfont;
    public static Font secondHeadfont;
    public static Font textfont;
    public static Font secondTitleFont;
    public static Font itemFont;
    public static Font paragraphFont;
    public static Font tableCellFont;
    public static Font accessoryFont;

    /**
     * 图标
     */
//    public static Image checkPng;
//    public static Image unCheckPng;

    /**
     * 间距
     */
    public static String shortSpacing = "      ";
    public static String mediumSpacing = "            ";
    public static String longSpacing = "                        ";
    public static String maxLongSpacing = "                                    ";
    public static String shortSlash = "   /   ";
    public static String mediumSlash = "      /      ";
    public static String longSlash = "            /            ";
    public static String maxLongSlash = "                  /                  ";

    /**
     * 模版
     */
    public static String ht;
    public static String scan;
    public static String htScan;
    public static String over;
    public static String htSign;
    public static String signatureHt;

    /**
     * 初始化字体及勾选、未勾选图标
     */
    static {
        try {
            // 不同字体（这里定义为同一种字体：包含不同字号、不同style）
            bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            titlefont = new Font(bfChinese, 16, Font.NORMAL);
            titleSecondFont = new Font(bfChinese, 14, Font.NORMAL);
            headfont = new Font(bfChinese, 14, Font.BOLD);
            secondHeadfont = new Font(bfChinese, 15, Font.BOLD);
            textfont = new Font(bfChinese, 16, Font.NORMAL);
            secondTitleFont = new Font(bfChinese,18,Font.BOLD);
            itemFont = new Font(bfChinese,16,Font.BOLD);
            paragraphFont = new Font(bfChinese,11,Font.NORMAL);
            accessoryFont = new Font(bfChinese,13,Font.NORMAL);
            tableCellFont = new Font(bfChinese,9,Font.NORMAL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static PdfPCell createCell(String value, Font font, int align, int colspan) {
        PdfPCell cell = new PdfPCell();
        cell.setMinimumHeight(20);
        cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cell.setHorizontalAlignment(align);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    public static PdfPCell createCell(String value,int align) {
        PdfPCell cell = new PdfPCell();
        cell.setBorderWidth(0);//去掉边框
        cell.setPhrase(new Phrase(value, paragraphFont));//内容 字体大小
        cell.setHorizontalAlignment(align);//是否剧中
        return cell;
    }

    public static PdfPCell createCell(String value, Font font, int align, int colspan, int rowspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setRowspan(rowspan);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    /**
     * 创建指定列宽、列数的表格
     * @param widths
     * @return
     */
    public static PdfPTable createTable(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
            table.setTotalWidth(500);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setBorder(1);
            table.setSpacingAfter(10f);//设置段落下空白
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }


    public static void addTitle(Document document, String text, int alignment) throws DocumentException, IOException {
        Paragraph paragraph = new Paragraph(text, new Font(bfChinese, 18, Font.NORMAL));
        paragraph.setAlignment(alignment); //设置文字居中 0靠左   1，居中     2，靠右
        paragraph.setIndentationLeft(0); //设置左缩进
        paragraph.setIndentationRight(0); //设置右缩进
        paragraph.setFirstLineIndent(0); //设置首行缩进
        paragraph.setLeading(10f); //行间距
        paragraph.setSpacingBefore(10f); //设置段落上空白
        paragraph.setSpacingAfter(10f); //设置段落下空白
        document.add(paragraph);
    }

    public static void addNoBorderCell(PdfPTable table,String text) {
        PdfPCell cell = new PdfPCell(new Paragraph(text,paragraphFont));
        cell.setBorderWidth(0);
        cell.setLeading(12,0); //行间距
        table.addCell(cell);
    }
}
