package cn.hmsoft.skill.school.ticket;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import cn.hmsoft.helper.RandomHelper;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.helper.pdf.ItextPdfHelper;
import cn.hmsoft.skill.constants.StdMateialType;
import cn.hmsoft.skill.data.dao.std.StdEnrolDao;
import cn.hmsoft.skill.data.model.school.SchoolReg;
import cn.hmsoft.skill.data.model.std.StdEnrolSubject;
import cn.hmsoft.skill.data.model.std.StdReg;
import cn.hmsoft.skill.helper.SkillMateialHelper;
import cn.hmsoft.skill.helper.SkillParamHelper;

public abstract class SkillTicketHelper {
	// private static SkillTicketHelper ticketUtil = null;

	public static File makeTicketFile(StdReg std, StdEnrolDao daoEnrol, SchoolReg school, Map<String, Object> enrol) {
		SkillTicketHelper ticketUtil = null;
		String class_name = "cn.hmsoft.skill.school.ticket.T" + school.getSchool_id();
		try {
			ticketUtil = (SkillTicketHelper) Class.forName(class_name).newInstance();
		} catch (Exception e) {
			try {
				ticketUtil = (SkillTicketHelper) Class.forName("cn.hmsoft.skill.school.ticket.TicketGobal")
						.newInstance();
			} catch (Exception ex) {
				return null;
			}

		}

		List<StdEnrolSubject> subjectArray = new ArrayList<StdEnrolSubject>();
		if(school.getSchool_id()==8910) {
			System.out.println("学校1："+ school.getSchool_id());
			subjectArray = daoEnrol.listSubject8910(std.getStd_id(), (int) enrol.get("aspect_id"));
		} else {
			System.out.println("学校2："+ school.getSchool_id());
			subjectArray = daoEnrol.listSubject(std.getStd_id(), (int) enrol.get("aspect_id"));
		}
		Image stdUploadImage = SkillMateialHelper.createStdImage(std, std.getStd_image(), 94, 108);
		String file_name = SkillParamHelper.CurrentYear + "_" + school.getSchool_id() + "_" + enrol.get("ticket_num")
				+ "_" + std.getCert_id() + "_" + RandomHelper.getRandomValue(8) + ".pdf";
		File pdf = null;
		if (enrol.containsKey("titcket_file")
				&& (enrol.get("ticket_file") != null || StringHelper.isNotEmpty(enrol.get("ticket_file").toString()))) {
			pdf = SkillMateialHelper.makeStdImageFile(enrol.get("ticket_file").toString(), StdMateialType.StdTicket);
			if (pdf.exists())
				pdf.delete();
		}
		pdf = SkillMateialHelper.makeStdImageFile(file_name, StdMateialType.StdTicket);

		Document doc = null;
		try {
			doc = ItextPdfHelper.createPdf(pdf);
			PdfFont pdfFont = ItextPdfHelper.getPdfFontSTSong();
			doc.setFont(pdfFont);
			ticketUtil.makePdf(doc, std, stdUploadImage, daoEnrol, school, enrol, subjectArray);
			ItextPdfHelper.closePdf(doc);
			return pdf;
		} catch (Exception e) {
			try {
				ItextPdfHelper.closePdf(doc);
			} catch (Exception ex) {
			}
			pdf.delete();
			throw e;
		}

	}

	protected abstract void makePdf(Document doc, StdReg std, Image image, StdEnrolDao daoEnrol, SchoolReg school,
			Map<String, Object> enrol, List<StdEnrolSubject> subjectArray);
}
