package org.fouram.core.util;

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

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

public class FileUpload {

	public static String fileUpload(MultipartFile file) {
		if(file == null){
			return null;
		}
		String baseFolder = ConfConfig.getConfigString("fileUpload.baseFolder");
		String year = DateUtil.getYear();
		String month = DateUtil.getMonth();
		String date = DateUtil.getDate();
		String fileName = "";
		String filePath = null;
		filePath = baseFolder + "/" + year + "/" + month + "/" + date + "/";
		String name = UUIDUtil.get32UUID();
		fileName = FileUpload.fileUpload(file, filePath, name);
		String fileUrl = "/" + year + "/" + month + "/" + date + "/" + fileName;
		/*try {
			if (ImageUtil.isImage(baseFolder + fileUrl)) {
				String newFileUrl = fileUrl.replace(name, name + "_");
				// 旋转360度，去除原有旋转角度
				ImageUtil.rotate(baseFolder + fileUrl, baseFolder + newFileUrl, 360);
				fileUrl = newFileUrl;
			}
		} catch (Exception e) {
			LoggerUtil.error(e);
		}*/
		return fileUrl;
	}
	
	public static Map<String, String> fileUploadMap(MultipartFile file) {
		Map<String, String> params = new HashMap<>();
		if(file == null){
			return null;
		}
		String baseFolder = ConfConfig.getConfigString("fileUpload.baseFolder");
		String year = DateUtil.getYear();
		String month = DateUtil.getMonth();
		String date = DateUtil.getDate();
		String fileName = "";
		String filePath = null;
		filePath = baseFolder + "" + year + "/" + month + "/" + date + "/";
//		String name = file.getOriginalFilename().substring(0,file.getOriginalFilename().lastIndexOf("."));
		String name = UUIDUtil.get32UUID();
		fileName = FileUpload.fileUpload(file, filePath, name);
		String fileUrl = "" + year + "/" + month + "/" + date + "/" + fileName;
		try {
			if (ImageUtil.isImage(baseFolder + fileUrl)) {
				String newFileUrl = fileUrl.replace(name, name + "_");
				// 旋转360度，去除原有旋转角度
				ImageUtil.rotate(baseFolder + fileUrl, baseFolder + newFileUrl, 360);
				fileUrl = newFileUrl;
			}
			params.put("fileName", fileName);
			params.put("fileDir", filePath);
			params.put("fileUrl", fileUrl);
			
		} catch (Exception e) {
			LoggerUtil.error(e);
		}
		return params;
	}

	public static String base64FileUpload(String base64String) {
		String baseFolder = ConfConfig.getConfigString("fileUpload.baseFolder");
		String year = DateUtil.getYear();
		String month = DateUtil.getMonth();
		String date = DateUtil.getDate();
		String name = UUIDUtil.get32UUID();
		String fileName = name + ".png";
		String fileUrl = "/" + year + "/" + month + "/" + date + "/" + fileName;
		try {
			Base64Util.decodeFile(base64String, baseFolder + fileUrl);
			if (ImageUtil.isImage(baseFolder + fileUrl)) {
				String newFileUrl = fileUrl.replace(name, name + "_");
				// 旋转360度，去除原有旋转角度
				ImageUtil.rotate(baseFolder + fileUrl, baseFolder + newFileUrl, 360);
				fileUrl = newFileUrl;
			}
		} catch (Exception e) {
			LoggerUtil.error(e);
		}
		return fileUrl;
	}

	public static String fileUpload(MultipartFile file, String filePath, String fileName) {
		String extName = "";
		try {
			if (file.getOriginalFilename().lastIndexOf(".") >= 0) {
				extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			}
			copyFile(file.getInputStream(), filePath, fileName + extName).replaceAll("-", "");
		} catch (IOException e) {
			LoggerUtil.error(e);
		}
		return fileName + extName;
	}

	private static String copyFile(InputStream in, String dir, String realName) throws IOException {
		File file = new File(dir, realName);
		if (!file.exists()) {
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			file.createNewFile();
		}
		FileUtils.copyInputStreamToFile(in, file);
		return realName;
	}
}
