package com.qmth.paper.library.common.util.pdf;

import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.layout.Document;
import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.Serializable;

public class ItextDocumentInfo implements Serializable {

    private final static Logger log = LoggerFactory.getLogger(ItextDocumentInfo.class);

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("页面宽度,单位mm")
    private int page_width = 210;

    @ApiModelProperty("页面高度,单位mm")
    private int page_height = 297;

    protected int margin_top = 10;
    protected int margin_right = 10;
    protected int margin_bottom = 10;
    protected int margin_left = 10;

    protected PdfFont font = null;

    protected PdfFont getFont() {
        return font;
    }

    public Document prepareDocument(File tempFile) {
        return prepareDocument(tempFile, ItextPdfUtil.getPdfFontSTSong());
    }

    public Document prepareDocument(File tempFile, PdfFont font) {
        try {
            String page_margin = "20 30 20 30";
            String[] array = page_margin.split("\\s+");
            margin_top = Integer.parseInt(array[0]);
            margin_right = Integer.parseInt(array[1]);
            margin_bottom = Integer.parseInt(array[2]);
            margin_left = Integer.parseInt(array[3]);
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        Document doc = ItextPdfUtil.createPdf(tempFile,
                ItextPdfUtil.getPageSizeByMillimeter(this.page_width, this.page_height));

        doc.setMargins(margin_top, margin_right, margin_bottom, margin_left);
        if (font == null)
            font = ItextPdfUtil.getPdfFontSTSong();
        this.font = font;
        doc.setFont(font);

        return doc;
    }

    public void rotate() {
        int w = this.page_width;
        this.page_width = this.page_height;
        this.page_height = w;
    }

}
