package com.ejie.m62fb.utils.documentos;

import java.io.IOException;
import java.sql.SQLException;

import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialException;

import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import com.ejie.m62fb.model.Plantilla;
import com.ejie.m62fb.model.documento.Documento;

/**
 * DocumentosUtil.
 * 
XXXX
 */
public class DocumentosUtil {
	public enum ContentTypes {
		//office antiguos
		DOC ("application/msword",new String[]{"DOC","DOT"},"DOC"),
		DOCM ("application/vnd.ms-word.document.macroEnabled.12",new String[]{"DOCM"},"DOC"),
		DOTM ("application/vnd.ms-word.template.macroEnabled.12",new String[]{"DOTM"},"DOC"),
	    XLS  ("application/vnd.ms-excel",new String[]{"XLS","XLT","XLM","XLB","XLA"},"XLS"),
	    PPT  ("application/vnd.ms-powerpoint",new String[]{"PPT","POT","PPS","PPA"},"PPT"),
	    MDB  ("application/msaccess",new String[]{"MDB","ACCDB","ACCDE","ACCDT"},"MDB"),
	    //office nuevos
	    DOCX ("application/vnd.openxmlformats-officedocument.wordprocessingml.document",new String[]{"DOCX"},"DOC"),
	    DOTX ("application/vnd.openxmlformats-officedocument.wordprocessingml.template",new String[]{"DOTX"},"DOC"),
	    XLSX  ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",new String[]{"XLSX"},"XLS"),
	    XLTX  ("application/vnd.openxmlformats-officedocument.spreadsheetml.template",new String[]{"XLTX"},"XLS"),
	    XLSM  ("application/vnd.ms-excel.addin.macroEnabled.12",new String[]{"XLSM"},"XLS"),
	    XLSB  ("application/vnd.ms-excel.sheet.binary.macroEnabled.12",new String[]{"XLSB"},"XLS"),
	    PPTX  ("application/vnd.openxmlformats-officedocument.presentationml.presentation",new String[]{"PPTX"},"PPT"),
	    POTX  ("application/vnd.openxmlformats-officedocument.presentationml.template",new String[]{"POTX"},"PPT"),
	    PPSX  ("application/vnd.openxmlformats-officedocument.presentationml.slideshow",new String[]{"PPSX"},"PPT"),
	    SLDX  ("application/vnd.openxmlformats-officedocument.presentationml.slide",new String[]{"SLDX"},"PPT"),
	    //texto
	    TXT  ("text/plain",new String[]{"TXT"},"TXT"),
	    XML  ("text/xml",new String[]{"XML","XSD","XSLT","XSF","XSL"},""),
	    //imagen
	    ICO  ("image/x-icon",new String[]{"ICO"},"IMG"),
	    PNG  ("image/png",new String[]{"PNG","PNZ"},"IMG"),
	    GIF  ("image/gif",new String[]{"GIF"},"IMG"),
	    BMP  ("image/bmp",new String[]{"BMP"},"IMG"),
	    JPEG  ("image/jpeg",new String[]{"JPEG","JPE","JPG"},"IMG")
	    ;

	    private final String content; 
	    private final String[] types;
	    private final String codTipoPlantilla; 
	    ContentTypes(String content, String[] types,String codTipoPlantilla) {
	        this.content = content;
	        this.types = types;
	        this.codTipoPlantilla=codTipoPlantilla;
	    }
	    private String getContent() { return content; }
	    private String getCodTipoPlantilla() { return codTipoPlantilla; }
	    private boolean isType(String type) { 
	    	for (int i = 0; i < this.types.length; i++) {
				if (this.types[i].equalsIgnoreCase(type)){
					return true;
				}
			} 
	    	return false; 
	    }
	}

	/**
	 * Constructor privado
	 */
	private DocumentosUtil() {

	}

	
	public static String getContentType (String extension) {
		for (ContentTypes p : ContentTypes.values()){
			if(p.isType(extension)){
				return p.getContent();
			}
		}
		return "";
	}
	public static String getCodTipoPlantilla (String extension) {
		for (ContentTypes p : ContentTypes.values()){
			if(p.isType(extension)){
				return p.getCodTipoPlantilla();
			}
		}
		return null;
	}
	public static Documento generateM62Documento(MultipartFile file,String numExpediente)throws SerialException, SQLException, IOException {
		Documento documento = new Documento(new SerialBlob(file.getBytes()));
		documento.setNomDocumento(file.getOriginalFilename());
		documento.setCodTipoPlantilla(getCodTipoPlantilla(FilenameUtils.getExtension(file.getOriginalFilename())));
		documento.setPlantilla(new Plantilla(FilenameUtils.getExtension(file.getOriginalFilename()).toUpperCase()));
		documento.setNumExpediente(numExpediente!=null?numExpediente.toUpperCase():null);
		return documento;
	}
}