package com.qmth.exam.reserve.bean.stdapply;

import java.io.File;

import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.layout.Document;
import com.qmth.exam.reserve.bean.IModel;
import com.qmth.exam.reserve.util.ItextPdfUtil;

import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ItextDocumentInfo implements IModel {

    private static final long serialVersionUID = 7598562571622959860L;

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

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

    @ApiModelProperty("页面边距")
    private String page_margin = "20 30 20 30";

    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[] array = this.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) {
            e.printStackTrace();
        }

        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;
    }

}
