package cn.util.file;

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

import cn.util.http.MimeTypeKit;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件类型工具类
 */
public class FileTypeKit 
{
	public static final Map<String, String> FILE_TYPE_MAP = new HashMap<>();

	/**
	 * 常见文件头信息
	 */
	static {
		FILE_TYPE_MAP.put("jpg", "FFD8FF");  //JPEG (jpg)
		FILE_TYPE_MAP.put("png", "89504E47");  //PNG (png)
		FILE_TYPE_MAP.put("gif", "47494638");  //GIF (gif)
		FILE_TYPE_MAP.put("tif", "49492A00");  //TIFF (tif)
		FILE_TYPE_MAP.put("bmp", "424D"); //Windows Bitmap (bmp)
		FILE_TYPE_MAP.put("dwg", "41433130"); //CAD (dwg)
		FILE_TYPE_MAP.put("html", "68746D6C3E");  //HTML (html)
		FILE_TYPE_MAP.put("rtf", "7B5C727466");  //Rich Text Format (rtf)
		FILE_TYPE_MAP.put("xml", "3C3F786D6C");
		FILE_TYPE_MAP.put("zip", "504B0304");
		FILE_TYPE_MAP.put("rar", "52617221");
		FILE_TYPE_MAP.put("psd", "38425053");  //Photoshop (psd)
		FILE_TYPE_MAP.put("eml", "44656C69766572792D646174653A");  //Email [thorough only] (eml)
		FILE_TYPE_MAP.put("dbx", "CFAD12FEC5FD746F");  //Outlook Express (dbx)
		FILE_TYPE_MAP.put("pst", "2142444E");  //Outlook (pst)
		FILE_TYPE_MAP.put("xls", "D0CF11E0");  //MS Excel
		FILE_TYPE_MAP.put("doc", "D0CF11E0");  //MS Word 注意：word 和 excel的文件头一样
		FILE_TYPE_MAP.put("mdb", "5374616E64617264204A");  //MS Access (mdb)
		FILE_TYPE_MAP.put("wpd", "FF575043"); //WordPerfect (wpd)
		FILE_TYPE_MAP.put("eps", "252150532D41646F6265");
		FILE_TYPE_MAP.put("ps", "252150532D41646F6265");
		FILE_TYPE_MAP.put("pdf", "255044462D312E");  //Adobe Acrobat (pdf)
		FILE_TYPE_MAP.put("qdf", "AC9EBD8F");  //Quicken (qdf)
		FILE_TYPE_MAP.put("pwl", "E3828596");  //Windows Password (pwl)
		FILE_TYPE_MAP.put("wav", "57415645");  //Wave (wav)
		FILE_TYPE_MAP.put("avi", "41564920");
		FILE_TYPE_MAP.put("ram", "2E7261FD");  //Real Audio (ram)
		FILE_TYPE_MAP.put("rm", "2E524D46");  //Real Media (rm)
		FILE_TYPE_MAP.put("mpg", "000001BA");  //
		FILE_TYPE_MAP.put("mov", "6D6F6F76");  //Quicktime (mov)
		FILE_TYPE_MAP.put("asf", "3026B2758E66CF11"); //Windows Media (asf)
		FILE_TYPE_MAP.put("mid", "4D546864");  //MIDI (mid)
	}
	/**
	 * 获取文件类型
	 * <p>
	 * 例如: mooding.txt, 返回: txt
	 *
	 * @param file 文件名
	 * @return 后缀（不含".")
	 */
	public static String getFileType(File file) {
		if (null == file) {
			return StringUtils.EMPTY;
		}
		return getFileType(file.getName());
	}

	/**
	 * 获取文件类型
	 * <p>
	 * 例如: ruoyi.txt, 返回: txt
	 *
	 * @param fileName 文件名
	 * @return 后缀（不含".")
	 */
	public static String getFileType(String fileName) {
		int separatorIndex = fileName.lastIndexOf(".");
		if (separatorIndex < 0) {
			return "";
		}
		return fileName.substring(separatorIndex + 1).toLowerCase();
	}
	/**
	 * 获取文件名的后缀
	 * @param file 表单文件
	 * @return 后缀名
	 */
	public static final String getExtension(MultipartFile file) {
		String extension = FilenameUtils.getExtension(file.getOriginalFilename());
		if (StringUtils.isEmpty(extension)) {
			extension = MimeTypeKit.getExtension(file.getContentType());
		}
		return extension;
	}

	/**
	 * 获取文件类型
	 *
	 * @param photoByte 文件字节码
	 * @return 后缀（不含".")
	 */
	public static String getFileExtendName(byte[] photoByte) 
	{
		String strFileExtendName = "JPG";
		if ((photoByte[0] == 71) && (photoByte[1] == 73) && (photoByte[2] == 70) && (photoByte[3] == 56)
				&& ((photoByte[4] == 55) || (photoByte[4] == 57)) && (photoByte[5] == 97)) {
			strFileExtendName = "GIF";
		} else if ((photoByte[6] == 74) && (photoByte[7] == 70) && (photoByte[8] == 73) && (photoByte[9] == 70)) {
			strFileExtendName = "JPG";
		} else if ((photoByte[0] == 66) && (photoByte[1] == 77)) {
			strFileExtendName = "BMP";
		} else if ((photoByte[1] == 80) && (photoByte[2] == 78) && (photoByte[3] == 71)) {
			strFileExtendName = "PNG";
		}
		return strFileExtendName;
	}

	
	public static boolean isWord(byte[] fileBytes) {
		String type = getFileTypeByBytes(fileBytes);
		return "doc".equals(type);
	}

	public static String getFileTypeByBytes(byte[] acceptBytes) {
		byte[] bytes = new byte[30];
		if (acceptBytes.length > 30) {
			System.arraycopy(acceptBytes, 0, bytes, 0, 30);
		}
		String filetypeHex = String.valueOf(getFileHexString(acceptBytes.length > 30 ? bytes : acceptBytes));
		for (Map.Entry<String, String> entry : FILE_TYPE_MAP.entrySet()) {
			String fileTypeHexValue = entry.getValue();
			if (filetypeHex.toUpperCase().startsWith(fileTypeHexValue)) {
				return entry.getKey();
			}
		}
		return null;
	}

	/**
	 * 获取文件
	 * @return fileTypeHex
	 */
	public static String getFileHexString(byte[] bytes) {
		StringBuilder stringBuilder = new StringBuilder();
		if (bytes == null || bytes.length <= 0) {
			return null;
		}
		for (byte value : bytes) {
			int v = value & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}
}
