package com.gx.obe.server.management.admin.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Base64;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.gx.obe.http.util.FileUtil;
import com.gx.obe.server.common.utils.StringUtils;
import com.gx.obe.server.common.vo.Result;
import com.gx.obe.server.management.admin.dto.DirDTO;

/**
 * @author gxcx
 *
 */
@RestController
@RequestMapping("/YWRtaW5maWxl")
public class FileAdminController {

	private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(FileAdminController.class);
	
	
	@Value("${upload.logPath}")
	private String logpath;

	/**
	 * 得到系统根路径磁盘列表
	 * 
	 * @return
	 */
	@RequestMapping("/getLogFileinfo")
	@ResponseBody
	public Result<ArrayList<DirDTO>> getRootFileList() {
		Result<ArrayList<DirDTO>> rs = new Result<ArrayList<DirDTO>>();
		FileSystemView sys = FileSystemView.getFileSystemView();
		Path paht = Paths.get(System.getProperty(logpath));
		File file = new File(paht.toString());
		File[] files = file.listFiles();
		ArrayList<DirDTO> list = new ArrayList<>();
		for (int i = 0; i < files.length; i++) {
			DirDTO d = new DirDTO();
			d.setPath(files[i].toString());
			d.setSimpleName(files[i].getName());
			d.setType(files[i].isDirectory() ? "0" : "1");
			d.setInfo(sys.getSystemTypeDescription(files[i]));
			if (!FileUtil.isDirectory(files[i])) {
				String readableSize = FileUtil.readableFileSize(files[i]);
				if ("0".equals(readableSize) || "".equals(readableSize)) {
					d.setReadableSize("-");
				} else {
					d.setReadableSize(readableSize);
				}
			} else {
				d.setReadableSize("-");
			}
			list.add(d);
		}
		rs.setCode(Result.SUCCESS);
		rs.setData(list);
		return rs;
	}

	/**
	 * 得到一个目录下的文件列表
	 * 
	 * @param path
	 * @return
	 */
	@RequestMapping("/getFileList")
	@ResponseBody
	public Result<ArrayList<DirDTO>> getFileList(@RequestParam("path") String strPath) {
		Result<ArrayList<DirDTO>> rs = new Result<ArrayList<DirDTO>>();
		FileSystemView sys = FileSystemView.getFileSystemView();
		File root = FileUtil.file(strPath);
		root.listFiles();
		File[] files = root.listFiles();
		ArrayList<DirDTO> list = new ArrayList<>();
		for (int i = 0; i < files.length; i++) {
			DirDTO d = new DirDTO();
			d.setPath(files[i].toString());
			d.setSimpleName(files[i].getName());
			d.setType(files[i].isDirectory() ? "0" : "1");
			d.setInfo(sys.getSystemTypeDescription(files[i]));
			if (!FileUtil.isDirectory(files[i])) {
				String readableSize = FileUtil.readableFileSize(files[i]);
				if ("0".equals(readableSize) || "".equals(readableSize)) {
					d.setReadableSize("-");
				} else {
					d.setReadableSize(readableSize);
				}
			} else {
				d.setReadableSize("-");
			}
			list.add(d);
		}
		rs.setCode(Result.SUCCESS);
		rs.setData(list);
		return rs;
	}
	
	@RequestMapping("/getBacktrackFileList")
	@ResponseBody
	public Result<ArrayList<DirDTO>> getBacktrackFileList(@RequestParam("path") String strPath) {
		if (StringUtils.isNotEmpty(strPath)) {
			Path paht = Paths.get(System.getProperty("user.dir"));
			if (!paht.toString().equals(strPath)) {
				strPath = strPath.substring(0, strPath.lastIndexOf(File.separator));
			}
			Result<ArrayList<DirDTO>> result = getFileList(strPath);
			result.setMsg(strPath);
			return result;
		}
		return null;
	}
	


	@RequestMapping(value = "/download")
	public void download(HttpServletRequest req, HttpServletResponse resp, @RequestParam("path") String path) {
		path = new String(Base64.getDecoder().decode(path), StandardCharsets.UTF_8);
		BufferedInputStream bis = null;
		OutputStream out = null;
		try {
			File file = new File(path);
			if (file.exists()) {
				long startLoc = 0L;
				long toLength = 0L;
				long contentLength = 0L;
				int rangeSwitch = 0;
				long fileLength;
				String rangBytes = "";
				fileLength = file.length();
				InputStream ins = new FileInputStream(file);
				bis = new BufferedInputStream(ins);
				resp.reset();
				resp.setHeader("Accept-Ranges", "bytes");
				String range = req.getHeader("Range");
				if (range != null && range.trim().length() > 0 && !"null".equals(range)) {
					resp.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);
					rangBytes = range.replaceAll("bytes=", "");
					if (rangBytes.endsWith("-")) { // bytes=270000-
						rangeSwitch = 1;
						startLoc = Long.parseLong(rangBytes.substring(0, rangBytes.indexOf("-")));
						contentLength = fileLength - startLoc; // 客户端请求的是270000之后的字节(包括bytes下标索引为270000的字节)
					} else {
						// bytes=270000-320000
						rangeSwitch = 2;
						String temp1 = rangBytes.substring(0, rangBytes.indexOf("-"));
						String temp2 = rangBytes.substring(rangBytes.indexOf("-") + 1, rangBytes.length());
						startLoc = Long.parseLong(temp1);
						toLength = Long.parseLong(temp2);
						contentLength = toLength - startLoc + 1; // 客户端请求的是 270000-320000 之间的字节
					}
				} else {
					contentLength = fileLength;
				}
				resp.setHeader("Content-Length", new Long(contentLength).toString());
				resp.setHeader("Yc-Length", new Long(contentLength).toString());
				String reqMD5 = req.getHeader("GetContent-MD5");
				if (rangeSwitch == 1) {
					String contentRange = new StringBuffer("bytes ").append(new Long(startLoc).toString()).append("-")
							.append(new Long(fileLength - 1).toString()).append(File.separator)
							.append(new Long(fileLength).toString()).toString();
					resp.setHeader("Content-Range", contentRange);
					bis.skip(startLoc);
				} else if (rangeSwitch == 2) {
					String contentRange = range.replace("=", " ") + File.separator + new Long(fileLength).toString();
					resp.setHeader("Content-Range", contentRange);
					bis.skip(startLoc);
				} else {
					String contentRange = new StringBuffer("bytes ").append("0-").append(fileLength - 1)
							.append(File.separator).append(fileLength).toString();
					resp.setHeader("Content-Range", contentRange);
				}
				String fileName = file.getName();
				resp.setContentType("application/octet-stream");
				if (req.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
					fileName = URLEncoder.encode(fileName, "UTF-8");
				} else {
					fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
				}
				resp.addHeader("Content-Disposition", "attachment;filename=" + fileName);
				resp.setHeader("Yc-filename", fileName);
				out = resp.getOutputStream();
				int n = 0;
				long readLength = 0;
				int bsize = 1024;
				byte[] bytes = new byte[bsize];
				if (rangeSwitch == 2) {
					while (readLength <= contentLength - bsize) {
						n = bis.read(bytes);
						readLength += n;
						out.write(bytes, 0, n);
					}
					if (readLength <= contentLength) {
						n = bis.read(bytes, 0, (int) (contentLength - readLength));
						out.write(bytes, 0, n);
					}
				} else {
					while ((n = bis.read(bytes)) != -1) {
						out.write(bytes, 0, n);
					}
				}
				out.flush();
				out.close();
				bis.close();
			} else {
				if (LOG.isDebugEnabled()) {
					LOG.debug("Error: file " + path + " not found.");
				}
			}
		} catch (IOException ie) {
		} catch (Exception e) {
			LOG.error(e.getMessage());
		} finally {
			try {
				if (bis != null) {
					bis.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

}