package cn.hmsoft.skill.helper;

import java.io.File;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.layout.element.Image;

import cn.hmsoft.application.SpringHelper;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.helper.RandomHelper;
import cn.hmsoft.helper.pdf.ItextPdfHelper;
import cn.hmsoft.skill.constants.StdMateialType;
import cn.hmsoft.skill.data.model.std.StdReg;

/**********************
 * 材料辅助类
 * 
 * @author zxq
 * @create 2018-10-25 10:51:26
 * @version 5.0.0
 * @email: revisit@126.com
 * @Company: www.hmsoft.cn
 */
public class SkillMateialHelper {

	/**************************
	 * 创建临时文件
	 */
	public static File createTempFile() {
		try {
			return File.createTempFile("SKILL" + RandomHelper.getRandomValue(32),
					".tmp");
		} catch (Exception e) {
			throw new BusinessException("无法创建临时文件，请联系系统管理员！");
		}
	}
	/*********************
	 * 生成考生材料的名称
	 * 
	 * @param cert_id
	 * @param type
	 * @param suffix
	 * @return
	 */
	private static String makeStdFileName(String cert_id, String image_type, String suffix) {
		return image_type.toString() + "_" + cert_id + "_" + RandomHelper.getRandomValue(6) + "." + suffix;
	}
	
	/***********************
	 * 考生图片信息
	 */

	public static File makeStdImageFile(String cert_id,String stdUpload, String image_type, String suffix) {
		File f = new File(SkillParamHelper.MateialLocalPath + File.separator + stdUpload + File.separator
				+ makeStdFileName(cert_id, image_type, suffix));
		if (!f.getParentFile().exists())
			f.getParentFile().mkdirs();
		return f;
	}


	/***********************
	 * 考生图片信息
	 */

	public static File makeStdImageFile(String cert_id, String image_type, String suffix) {
		File f = new File(SkillParamHelper.MateialLocalPath + File.separator + image_type + File.separator
				+ makeStdFileName(cert_id, image_type, suffix));
		if (!f.getParentFile().exists())
			f.getParentFile().mkdirs();
		return f;
	}

	/***********************
	 * 考生图片信息
	 */

	public static File makeStdImageFile(String file_name, String image_type) {
		File f = new File(SkillParamHelper.MateialLocalPath + File.separator + image_type + File.separator + file_name);
		if (!f.getParentFile().exists())
			f.getParentFile().mkdirs();
		return f;
	}
	
	/**********************
	 * 获取考生照片
	 */
	public static String getStdImagePath(String std_image, String image_type) {
		// 判断本地文件是否存在
		File file = new File(SkillParamHelper.MateialLocalPath
				+ File.separator + image_type + File.separator + std_image);
		if (file.exists())
			return file.getPath();
		// 如果没有，返回网络地址
		return SkillParamHelper.MateialWebPath + "/" + image_type + "/"
				+ std_image;
	}

	
	public static Image createStdImage(StdReg std,String std_image, float width,
			float height) {
		String path = getStdImagePath(std_image, StdMateialType.StdUpload);
		Image imgStdUpload = null;
		try {
			imgStdUpload = new Image(ImageDataFactory.create(path));
		} catch (Exception e) {
			try {
				imgStdUpload = new Image(
						ImageDataFactory.create(std.getStd_param4()));
			}catch(Exception e1) {
				//e.printStackTrace();
				try {
					
					// 没有考生图片，返回默认值
					imgStdUpload = new Image(
							ImageDataFactory.create(SpringHelper.WebServletContext
									.getRealPath("/")
									+ File.separator
									+ "std"
									+ File.separator
									+ "image"
									+ File.separator
									+ "cert.jpg"));
				} catch (Exception ex) {
					
					
					
					e.printStackTrace();
					throw new BusinessException("无法找到考生照片，且无法用默认照片替代");
				}
			}
			
		}
		ItextPdfHelper.scaleImage(imgStdUpload, width, height);
		return imgStdUpload;
	}
}
