package com.qmth.exam.reserve.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.VerticalAlignment;
import com.qmth.boot.core.exception.StatusException;
import com.qmth.boot.tools.uuid.FastUUID;
import com.qmth.exam.reserve.bean.stdapply.ItextDocumentInfo;
import com.qmth.exam.reserve.bean.stdapply.MaterialTitleInfo;
import com.qmth.exam.reserve.bean.stdapply.StudentApplyVO;
import com.qmth.exam.reserve.service.MaterialGenerateService;
import com.qmth.exam.reserve.util.ItextPdfUtil;

@Service
public class MaterialGenerateServiceImpl implements MaterialGenerateService {

    private static final int LINE_NUMBER = 30;
    private static final int STUDENT_NUMBER_ROW = 2;
    private static final int FONT_SIZE = 12;
    private static final int LINE_HEIGHT = 25;

    @Override
    public File generateSignInForm(MaterialTitleInfo title, List<StudentApplyVO> studentList) {
        Document doc = null;
        ItextDocumentInfo pageModel = new ItextDocumentInfo();
        List<StudentApplyVO> tempList = new ArrayList<StudentApplyVO>();
        try {
            File file = File.createTempFile("Material" + FastUUID.get(), ".tmp");
            doc = pageModel.prepareDocument(file);
            doc.setMargins(20, 20, 20, 20);

            Paragraph p1 = new Paragraph(title.getTaskName()).setFontSize(18).setBold()
                    .setTextAlignment(TextAlignment.CENTER).setMarginTop(-2);
            doc.add(p1);
            int fontSize = 12;
            doc.add(subHead(title, studentList.size(), fontSize));
            for (int k = 0; k < studentList.size(); k++) {
                tempList.add(studentList.get(k));
                if (tempList.size() == LINE_NUMBER * STUDENT_NUMBER_ROW || k == studentList.size() - 1) {
                    createSignInTable(tempList, doc);
                    if (tempList.size() == LINE_NUMBER * STUDENT_NUMBER_ROW) {
                        doc.add(new AreaBreak());
                    }
                    tempList.clear();
                }
            }
            return file;
        } catch (Exception e) {
            e.printStackTrace();
            ItextPdfUtil.closePdf(doc);
            throw new StatusException("无法生成pdf");
        } finally {
            ItextPdfUtil.closePdf(doc);
        }

    }

    private void createSignInTable(List<StudentApplyVO> tempList, Document doc) {
        Table tableTitle = tableTitle();
        int number = LINE_NUMBER;
        if (tempList.size() < LINE_NUMBER) {
            number = tempList.size();
        }
        for (int i = 0; i < number; i++) {
            StudentApplyVO result = null;
            if (tempList.size() >= i + 1) {
                result = tempList.get(i);
                tableTitle.addCell(new Cell(1, 1).add(result != null ? result.getName() : " ")
                        .setTextAlignment(TextAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE)
                        .setFontSize(FONT_SIZE));
                tableTitle.addCell(new Cell(1, 1).add(result != null ? result.getStudentCode() : " ")
                        .setTextAlignment(TextAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE)
                        .setFontSize(FONT_SIZE));
                tableTitle.addCell(new Cell(1, 1).add(result != null ? result.getSeatNumber() : " ")
                        .setTextAlignment(TextAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE)
                        .setFontSize(FONT_SIZE));
                tableTitle.addCell(new Cell(1, 1).add(" ").setTextAlignment(TextAlignment.CENTER)
                        .setVerticalAlignment(VerticalAlignment.MIDDLE).setFontSize(FONT_SIZE));
            } else {
                tableTitle.addCell(new Cell().add(" ").setTextAlignment(TextAlignment.CENTER)
                        .setVerticalAlignment(VerticalAlignment.MIDDLE).setFontSize(10).setHeight(LINE_HEIGHT));
                tableTitle.addCell(new Cell().add(" ").setTextAlignment(TextAlignment.CENTER)
                        .setVerticalAlignment(VerticalAlignment.MIDDLE).setFontSize(10).setHeight(LINE_HEIGHT));
                tableTitle.addCell(new Cell().add(" ").setTextAlignment(TextAlignment.CENTER)
                        .setVerticalAlignment(VerticalAlignment.MIDDLE).setFontSize(10).setHeight(LINE_HEIGHT));
                tableTitle.addCell(new Cell().add(" ").setTextAlignment(TextAlignment.CENTER)
                        .setVerticalAlignment(VerticalAlignment.MIDDLE).setFontSize(10).setHeight(LINE_HEIGHT));
            }
            if (tempList.size() >= i + number + 1) {
                result = tempList.get(i + 30);
                tableTitle.addCell(new Cell(1, 1).add(result != null ? result.getName() : " ")
                        .setTextAlignment(TextAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE)
                        .setFontSize(FONT_SIZE));
                tableTitle.addCell(new Cell(1, 1).add(result != null ? result.getStudentCode() : " ")
                        .setTextAlignment(TextAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE)
                        .setFontSize(FONT_SIZE));
                tableTitle.addCell(new Cell(1, 1).add(result != null ? result.getSeatNumber() : " ")
                        .setTextAlignment(TextAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE)
                        .setFontSize(FONT_SIZE));
                tableTitle.addCell(new Cell(1, 1).add(" ").setTextAlignment(TextAlignment.CENTER)
                        .setVerticalAlignment(VerticalAlignment.MIDDLE).setFontSize(FONT_SIZE));
            } else {
                tableTitle.addCell(new Cell().add(" ").setTextAlignment(TextAlignment.CENTER)
                        .setVerticalAlignment(VerticalAlignment.MIDDLE).setFontSize(10).setHeight(LINE_HEIGHT));
                tableTitle.addCell(new Cell().add(" ").setTextAlignment(TextAlignment.CENTER)
                        .setVerticalAlignment(VerticalAlignment.MIDDLE).setFontSize(10).setHeight(LINE_HEIGHT));
                tableTitle.addCell(new Cell().add(" ").setTextAlignment(TextAlignment.CENTER)
                        .setVerticalAlignment(VerticalAlignment.MIDDLE).setFontSize(10).setHeight(LINE_HEIGHT));
                tableTitle.addCell(new Cell().add(" ").setTextAlignment(TextAlignment.CENTER)
                        .setVerticalAlignment(VerticalAlignment.MIDDLE).setFontSize(10).setHeight(LINE_HEIGHT));
            }
        }
        doc.add(tableTitle);
    }

    private Table subHead(MaterialTitleInfo title, int studentSize, int titleFontSize) {
        Table table = new Table(new float[] { 1f, 1f }).setWidthPercent(100);
        table.addCell(new Cell(1, 1).add("考点名称：" + title.getSiteName()).setTextAlignment(TextAlignment.LEFT)
                .setVerticalAlignment(VerticalAlignment.MIDDLE).setBorder(null).setFontSize(titleFontSize)
                .setMarginLeft(20f));
        table.addCell(new Cell(1, 1).add("考试时间：" + title.getTimePeriod()).setTextAlignment(TextAlignment.LEFT)
                .setVerticalAlignment(VerticalAlignment.MIDDLE).setBorder(null).setFontSize(titleFontSize)
                .setMarginLeft(40f));
        table.addCell(new Cell(1, 1).add("考场名称：" + title.getAddress()).setTextAlignment(TextAlignment.LEFT)
                .setVerticalAlignment(VerticalAlignment.MIDDLE).setBorder(null).setFontSize(titleFontSize)
                .setMarginLeft(20f));
        table.addCell(new Cell(1, 1).add("考生数量：" + studentSize).setTextAlignment(TextAlignment.LEFT)
                .setVerticalAlignment(VerticalAlignment.MIDDLE).setBorder(null).setFontSize(titleFontSize)
                .setMarginLeft(40f));
        return table;
    }

    private Table tableTitle() {
        Table signTable = new Table(new float[] { 1f, 1.3f, 0.5f, 1f, 1f, 1.3f, 0.5f, 1f }).setWidthPercent(100);
        for (int i = 0; i < STUDENT_NUMBER_ROW; i++) {
            signTable.addCell(new Cell(1, 1).add("姓名").setTextAlignment(TextAlignment.CENTER)
                    .setVerticalAlignment(VerticalAlignment.MIDDLE).setFontSize(FONT_SIZE).setBold());
            signTable.addCell(new Cell(1, 1).add("学号").setTextAlignment(TextAlignment.CENTER)
                    .setVerticalAlignment(VerticalAlignment.MIDDLE).setFontSize(FONT_SIZE).setBold());
            signTable.addCell(new Cell(1, 1).add("座号").setTextAlignment(TextAlignment.CENTER)
                    .setVerticalAlignment(VerticalAlignment.MIDDLE).setFontSize(FONT_SIZE).setBold());
            signTable.addCell(new Cell(1, 1).add("签字").setTextAlignment(TextAlignment.CENTER)
                    .setVerticalAlignment(VerticalAlignment.MIDDLE).setFontSize(FONT_SIZE).setBold());
        }
        return signTable;
    }

}
