package com.uas.report.service.impl;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.lowagie.text.pdf.PdfReader;
import com.uas.report.SpecialProperties;
import com.uas.report.schedule.model.TaskInformation;
import com.uas.report.schedule.service.Executable;
import com.uas.report.schedule.service.TaskService;
import com.uas.report.service.FileService;
import com.uas.report.util.ArrayUtils;
import com.uas.report.util.ExceptionUtils;
import com.uas.report.util.FileUtils;
import com.uas.report.util.ReportConstants;
import com.uas.report.util.ReportUtils;
import com.uas.report.util.StringUtils;
import com.uas.report.util.ZipUtils;

@Service
public class FileServiceImpl implements FileService {

	@Autowired
	private SpecialProperties specialProperties;

	@Autowired
	private TaskService taskService;

	/**
	 * 第一次执行的延迟时间间隔为30秒
	 */
	private static final long INITIAL_DELAY = 30 * 1000;

	/**
	 * 产生的pdf的有效期（最高为10分钟）
	 */
	private static final int PDF_MAX_INVALID_INTERVAL = 10 * 60 * 1000;

	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 查看、压缩文件时，对文件进行过滤
	 */
	public static final FileFilter fileFilter = new FileFilter() {
		@Override
		public boolean accept(File file) {
			if (file == null || !file.exists()) {
				return false;
			}
			// 不支持查看、压缩：jasper文件、tmp路径
			String filePath = file.getPath().toLowerCase();
			if (filePath.endsWith(".jasper") || filePath.endsWith("tmp") || filePath.contains("\\tmp\\")
					|| filePath.contains("/tmp/")) {
				return false;
			}
			return true;
		}
	};

	@Override
	public String autoDeploy(String sourceUserName, String destinationUserNames) throws IOException {
		if (StringUtils.isEmpty(sourceUserName) || StringUtils.isEmpty(destinationUserNames)) {
			throw new IllegalArgumentException("参数不能为空：sourceUserName，destinationUserNames");
		}
		byte[] data = null;
		String stantardJrxmlsUrl = String.format(specialProperties.getStandardJrxmlsUrl(), sourceUserName);
		// 如果本机提供标准模板下载，直接从本地获取数据
		if (specialProperties.hasStandardJrxmls()) {
			data = getStandardJrxmls(sourceUserName);
		}
		// 本机没有标准模板，则先下载标准模板数据
		else {
			logger.info("get standardJrxmls from " + stantardJrxmlsUrl + "...");
			JSONObject jsonObject = JSONObject.parseObject(IOUtils.toString(HttpClients.createDefault()
					.execute(new HttpGet(URI.create(stantardJrxmlsUrl))).getEntity().getContent()));
			data = jsonObject.getBytes("data");
		}
		if (ArrayUtils.isEmpty(data)) {
			throw new IllegalStateException("标准模板不存在");
		}
		String[] userNameArray = destinationUserNames.split(",");
		// 创建账套路径并解压模板zip包
		for (String userName : userNameArray) {
			ZipUtils.unzip(data, getMasterPath(userName));
		}
		return "已自动部署：" + destinationUserNames;
	}

	@Override
	public String upload(final String userName, String fileType, final MultipartFile file) {
		if (StringUtils.isEmpty(userName)) {
			return "未传入当前账套名称！";
		}
		if (file == null) {
			return "文件为空，无法进行上传！";
		}
		if (StringUtils.isEmpty(fileType)) {
			fileType = "jrxml";
		}

		// 如果上传的是模板zip包，直接获取字节数据，将其解压到相应的账套下
		if (fileType.equals("zip")) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						ZipUtils.unzip(file.getBytes(), getMasterPath(userName));
					} catch (IOException e) {
						throw new IllegalStateException("文件解压失败\n" + ExceptionUtils.getDetailedMessage(e));
					}
				}
			}).start();
			return "文件上传成功";
		}

		String fileName = file.getOriginalFilename();
		StringBuilder stringBuilder = new StringBuilder(specialProperties.getLocalBaseDir()).append("/");
		// jrxml模板和图片分别放在jrxml和Picture文件夹下，其他资源放在当前账套根路径下
		if (fileType.equals("jrxml")) {
			stringBuilder.append(userName).append("/").append("jrxml").append("/");
		} else if (fileType.equals("image")) {
			stringBuilder.append(userName).append("/").append("Picture").append("/");
		} else if (fileType.equals("other")) {
			stringBuilder.append(userName).append("/");
		} else {
			throw new IllegalArgumentException("不支持上传该类型的文件：" + fileType);
		}

		stringBuilder.append(fileName);
		String targetFilePath = stringBuilder.toString();
		final File targetFile = new File(targetFilePath);
		if (!targetFile.getParentFile().exists()) {
			targetFile.getParentFile().mkdirs();
		}
		try {
			FileUtils.write(targetFile.getAbsolutePath(), file.getBytes(), true);
			return "成功上传文件至：" + targetFile.getPath();
		} catch (IllegalStateException | IOException e) {
			logger.error("", e);
			return "文件上传失败： " + fileName;
		}
	}

	@Override
	public String upload(String filePath, Boolean isAbsolutePath, MultipartFile[] files) throws IOException {
		if (ArrayUtils.isEmpty(files)) {
			return "文件为空，无法进行上传！";
		}
		filePath = getAbsolutePath(filePath, isAbsolutePath);
		File targetFile = new File(filePath + "/" + files[0].getOriginalFilename());
		// 检查路径是否存在
		if (!targetFile.getParentFile().exists()) {
			targetFile.getParentFile().mkdirs();
		}

		StringBuilder stringBuilder = new StringBuilder();
		for (MultipartFile file : files) {
			String fileName = file.getOriginalFilename();
			targetFile = new File(targetFile.getParent() + "/" + fileName);
			// 不能使用file.transferTo(targetFile)，
			// 因为在spring boot下上传文件时会对临时路径进行处理，
			// 导致最终文件路径不正确，如果手动设置临时路径为根路径，
			// 又会因为权限问题导致文件写入失败，
			// 所以自己在指定路径创建文件，而不使用transferTo方法
			FileUtils.write(targetFile.getAbsolutePath(), file.getBytes(), true);
			stringBuilder.append("上传成功：").append(fileName).append("\n");
		}
		return stringBuilder.toString();
	}

	@Override
	public String getJrxmlFilePath(String userName, String reportName) {
		ReportUtils.checkParameters(userName, reportName);
		return new StringBuilder(getMasterPath(userName)).append(specialProperties.getLocalJrxmlDir()).append("/")
				.append(reportName).append(".jrxml").toString();
	}

	@Override
	public String getMasterPath(String userName) {
		if (StringUtils.isEmpty(userName)) {
			throw new IllegalArgumentException("参数不能为空：userName");
		}
		return new StringBuilder(specialProperties.getLocalBaseDir()).append("/").append(userName).toString();
	}

	@Override
	public byte[] getStandardJrxmls(String userName) throws IOException {
		if (StringUtils.isEmpty(userName)) {
			throw new IllegalArgumentException("未传入当前账套名称！");
		}
		if (!specialProperties.hasStandardJrxmls()) {
			throw new IllegalStateException("没有" + userName + "标准模板！");
		}
		try {
			return ZipUtils.zipFolder(getMasterPath(specialProperties.getStandardMaster()) + "/" + userName,
					FileServiceImpl.fileFilter);
		} catch (Throwable e) {
			throw new IOException("压缩失败\n" + ExceptionUtils.getDetailedMessage(e));
		}
	}

	@Override
	public void downloadStandardJrxmls(String userName, HttpServletResponse response) throws IOException {
		if (StringUtils.isEmpty(userName)) {
			throw new IllegalArgumentException("未传入当前账套名称！");
		}
		if (!specialProperties.hasStandardJrxmls()) {
			throw new IllegalStateException("没有" + userName + "标准模板！");
		}
		downloadZip(specialProperties.getStandardMaster() + "/" + userName, response);
	}

	@Override
	public void downloadZip(String userName, HttpServletResponse response) throws IOException {
		if (StringUtils.isEmpty(userName)) {
			throw new IllegalArgumentException("未传入当前账套名称！");
		}
		String masterPath = getMasterPath(userName);
		try {
			byte[] data = ZipUtils.zipFolder(masterPath, FileServiceImpl.fileFilter);
			download(data, new File(masterPath).getName() + ".zip", response);
		} catch (Throwable e) {
			throw new IOException("压缩失败\n" + ExceptionUtils.getDetailedMessage(e));
		}
	}

	@Override
	public void download(String filePath, Boolean isAbsolutePath, HttpServletResponse response) throws IOException {
		if (StringUtils.isEmpty(filePath) || response == null) {
			throw new IllegalArgumentException("参数不能为空：filePath，response");
		}
		filePath = getAbsolutePath(filePath, isAbsolutePath);
		File file = new File(filePath);
		if (!file.exists()) {
			throw new FileNotFoundException("文件不存在：" + filePath);
		}

		byte[] data = null;
		String fileName = "";
		// 下载文件夹之前，需进行压缩
		if (file.isDirectory()) {
			fileName = file.getName() + ".zip";
			data = ZipUtils.zipFolder(filePath, fileFilter);
		} else {
			fileName = file.getName();
			InputStream inputStream = new FileInputStream(file);
			data = new byte[inputStream.available()];
			inputStream.read(data);
			inputStream.close();
		}
		if (ArrayUtils.isEmpty(data)) {
			throw new IOException("下载失败：" + filePath);
		}
		download(data, fileName, response);
	}

	@Override
	public void download(byte[] data, String fileName, HttpServletResponse response) {
		if (ArrayUtils.isEmpty(data) || StringUtils.isEmpty(fileName) || response == null) {
			throw new IllegalArgumentException("参数不能为空：data，filePath，response");
		}
		try {
			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
			OutputStream outputStream = response.getOutputStream();
			outputStream.write(data);
			outputStream.flush();
			outputStream.close();
		} catch (IOException e) {
			logger.error("", e);
		}
	}

	@Override
	public String delete(String filePath, Boolean isAbsolutePath) throws IOException {
		// 路径不能为空，不能只包含'/'（根路径），不能含有'..'（不允许通过该方式删除上一级）
		if (StringUtils.isEmpty(filePath) || StringUtils.isEmpty(filePath.replaceAll("/", ""))
				|| filePath.contains("..")) {
			throw new IllegalArgumentException("路径不合法：" + filePath);
		}
		filePath = getAbsolutePath(filePath, isAbsolutePath);
		if (StringUtils.isEmpty(filePath)) {
			throw new IllegalArgumentException("参数不能为空：filePath");
		}
		File file = new File(filePath);
		if (!file.exists()) {
			throw new IOException("文件不存在，不必删除：" + filePath);
		}
		FileUtils.delete(file);
		return filePath;
	}

	@Override
	public List<Map<String, Object>> listFiles(String filePath, Boolean isAbsolutePath) throws IOException {
		filePath = getAbsolutePath(filePath, isAbsolutePath);
		File file = new File(filePath);
		if (!file.exists()) {
			throw new FileNotFoundException("文件不存在：" + filePath);
		}

		List<Map<String, Object>> result = new ArrayList<>();
		// 如果是文件，直接获取文件信息
		if (file.isFile()) {
			result.add(getFileInformation(file, isAbsolutePath));
		} else {
			File[] files = file.listFiles(fileFilter);
			// 文件夹放在前面展示
			List<File> directoryList = new ArrayList<>();
			List<File> fileList = new ArrayList<>();
			for (File f : files) {
				if (f.isDirectory()) {
					directoryList.add(f);
				} else {
					fileList.add(f);
				}
			}
			result.addAll(getFileInformations(directoryList, isAbsolutePath));
			result.addAll(getFileInformations(fileList, isAbsolutePath));
		}
		return result;
	}

	/**
	 * 获取多个文件的信息
	 * 
	 * @param files
	 *            文件
	 * @param isAbsolutePath
	 *            文件路径是否为绝对路径
	 * @return 文件信息，包括name(String)、lastModified(String)、size(Long)、filePath(
	 *         String，可能是相对路径，也可能是绝对路径，具体取决于isAbsolutePath)、isDirectory(Boolean)
	 */
	private List<Map<String, Object>> getFileInformations(List<File> files, Boolean isAbsolutePath) {
		List<Map<String, Object>> informationList = new ArrayList<>();
		for (File file : files) {
			informationList.add(getFileInformation(file, isAbsolutePath));
		}
		return informationList;
	}

	/**
	 * 获取文件信息
	 * 
	 * @param file
	 *            文件
	 * @param isAbsolutePath
	 *            文件路径是否为绝对路径
	 * @return 文件信息，包括name(String)、lastModified(String)、size(Long)、filePath(
	 *         String，可能是相对路径，也可能是绝对路径，具体取决于isAbsolutePath)、isDirectory(Boolean)
	 */
	private Map<String, Object> getFileInformation(File file, Boolean isAbsolutePath) {
		if (file == null || !file.exists()) {
			return null;
		}
		Map<String, Object> information = new HashMap<>();
		information.put("name", file.getName());
		if (isAbsolutePath == null || !isAbsolutePath) {
			information.put("filePath", getRelativePath(file));
		} else {
			information.put("filePath", file.getPath());
		}
		information.put("lastModified",
				new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(file.lastModified())));
		if (file.isFile()) {
			information.put("size", file.length());
			information.put("isDirectory", false);
		} else {
			information.put("isDirectory", true);
		}
		return information;
	}

	/**
	 * 获取文件相对于本地资源根路径的路径
	 * 
	 * @param file
	 *            文件
	 * @return 相对于本地资源根路径的路径
	 */
	private String getRelativePath(File file) {
		if (file == null || !file.exists()) {
			return null;
		}
		// 获取相对路径，须将本地资源根路径替换掉，并且文件分隔符统一使用 '/'
		return file.getPath().replace(new File(specialProperties.getLocalBaseDir()).getPath(), "").replace("\\", "/");
	}

	@Override
	public boolean isFileValid(String fileAbsolutePath, String jrxmlFileAbsolutePath) {
		if (!StringUtils.isEmpty(fileAbsolutePath) && !StringUtils.isEmpty(jrxmlFileAbsolutePath)) {
			File file = new File(fileAbsolutePath);
			File jrxmlFile = new File(jrxmlFileAbsolutePath);
			if (file.exists() && jrxmlFile.exists()) {
				long interval = new Date().getTime() - file.lastModified();
				long validity = PDF_MAX_INVALID_INTERVAL - interval;
				// 有效期大于0并且比模板文件新
				if (validity > 0 && file.lastModified() > jrxmlFile.lastModified()) {
					logger.info(file.getName() + " will be expired after " + validity / 1000.0 + "s");
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public String generateFileName(String userName, String profile, String whereCondition, String otherParameters,
			String exportFileType) {
		if (StringUtils.isEmpty(userName)) {
			return null;
		}
		StringBuilder stringBuilder = new StringBuilder(userName);
		if (!StringUtils.isEmpty(profile)) {
			stringBuilder.append(profile);
		}
		if (!StringUtils.isEmpty(whereCondition)) {
			stringBuilder.append(whereCondition);
		}
		if (!StringUtils.isEmpty(otherParameters)) {
			stringBuilder.append(otherParameters);
		}
		if (!StringUtils.isEmpty(exportFileType)) {
			stringBuilder.append(exportFileType);
		}
		// 文件名： hashCode
		return String.valueOf(stringBuilder.toString().hashCode());
	}

	@Override
	public int getPageSize(String pdfFileAbsolutePath) throws IOException {
		return new PdfReader(pdfFileAbsolutePath).getNumberOfPages();
	}

	/**
	 * 根据指定路径获取绝对路径，如果isAbsolutePath为真，直接返回指定路径，否则拼接上模板根路径
	 * 
	 * @param filePath
	 *            指定路径
	 * @param isAbsolutePath
	 *            是否为绝对路径
	 * @return 绝对路径
	 * @throws IOException
	 */
	private String getAbsolutePath(String filePath, Boolean isAbsolutePath) throws IOException {
		if (StringUtils.isEmpty(filePath)) {
			throw new IllegalArgumentException("路径不合法：" + filePath);
		}
		// 不是绝对路径的话，则相对于模板根路径
		if (isAbsolutePath == null || !isAbsolutePath) {
			filePath = specialProperties.getLocalBaseDir() + "/" + filePath;
		}
		return filePath;
	}

	@Override
	public TaskInformation newDeleteGeneratedFilesTask(Integer taskPeriod) {
		if (taskPeriod == null) {
			throw new NullPointerException();
		}
		String title = "定时删除pdf等文件";
		Executable command = new Executable() {
			@Override
			public String execute() {
				FileUtils.delete(new File(ReportConstants.GENERATED_FILES_ABSOLUTE_PATH), new FileFilter() {
					@Override
					public boolean accept(File file) {
						// 只删除已过期的文件
						if (new Date().getTime() - file.lastModified() - PDF_MAX_INVALID_INTERVAL > 0) {
							return true;
						}
						return false;
					}
				});
				return "success";
			}
		};
		TaskInformation taskInformation = new TaskInformation(title, command, INITIAL_DELAY, taskPeriod);
		taskService.newTask(taskInformation);
		return taskInformation;
	}

}
