package com.infoepoch.pms.dispatchassistant.controller.system;

import com.infoepoch.pms.dispatchassistant.common.component.BaseController;
import com.infoepoch.pms.dispatchassistant.common.component.Result;
import com.infoepoch.pms.dispatchassistant.common.constant.StoreKeys;
import com.infoepoch.pms.dispatchassistant.common.exception.NotLoginException;
import com.infoepoch.pms.dispatchassistant.common.exception.ValidationException;
import com.infoepoch.pms.dispatchassistant.common.utils.DateTool;
import com.infoepoch.pms.dispatchassistant.common.utils.ListUtils;
import com.infoepoch.pms.dispatchassistant.controller.basic.Auth;
import com.infoepoch.pms.dispatchassistant.domain.basic.store.KeyValueStoreService;
import com.infoepoch.pms.dispatchassistant.domain.basic.user.User;
import com.infoepoch.pms.dispatchassistant.domain.system.uploadFile.UploadFile;
import com.infoepoch.pms.dispatchassistant.domain.system.uploadFile.UploadFileService;
import com.infoepoch.pms.dispatchassistant.infractructure.system.file.UploadFileRepository;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


/**
 * 上传文件Controller
 *
 * @author pengh
 */
@Controller
@RequestMapping("/api/file")
public class FileController extends BaseController {

    @Autowired
    Auth auth;

    @Autowired
    UploadFileService uploadFileService;
    @Autowired
    UploadFileRepository uploadFileRepository;

    @Autowired
    private KeyValueStoreService keyValueStoreService;


    /**
     * 上传文件保存路径
     */
    @Value("${custom.upload-file.save-path}")
    private String savePath;
    @Value("${custom.upload-file.save-path2}")
    private String savePath2;


//    /**
//     * 上传文件
//     */
//    @RequestMapping(value = "/upload", method = RequestMethod.POST)
//    @ResponseBody
//    public Result upload(@RequestParam("file") MultipartFile file) throws IOException, NotLoginException {
//
//
//        return Result.successData(null);
//    }

    /**
     * 直接输出文件，主要用于图片显示
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public void response(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) throws IOException, ValidationException {

    }

    /**
     * 下载文件
     */
    @RequestMapping(value = "/download/{id}")
    @ResponseBody
    public void download(@PathVariable String id, HttpServletResponse response, HttpServletRequest request) throws IOException {

    }

    /**
     * 下载前校验文件是否存在
     */
    @RequestMapping(value = "/verify/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Result verifyFiles(@PathVariable String id) throws ValidationException {
        return Result.success();
    }

    /**
     * 批量下载文件
     */
    @RequestMapping(value = "/download-zip", method = RequestMethod.POST)
    @ResponseBody
    public void downloadZip(String ids, HttpServletResponse response, HttpServletRequest request) throws IOException {

    }

    /**
     * 根据Id查询多个文件，返回值为uploader组件格式
     */
    @RequestMapping(value = "/query-by-ids", method = RequestMethod.GET)
    @ResponseBody
    public Result query(@RequestParam(value = "ids[]", required = false) List<String> ids) {
        return Result.success();
    }

    // region pdf+file.zip

    /**
     * 根据pdf+附件下载集合压缩包
     * //     * @param pdfBase64Val
     * pdf base64值
     *
     * @param ids     附件id数组
     * @param pdfName 压缩包中pdf文件的名称
     * @param zipName 压缩包名称
     */
    @RequestMapping(value = "/download-pdf-zip", method = RequestMethod.POST)
    @ResponseBody
    public void downloadZip(@RequestParam("pdfName") String pdfName, @RequestParam("zipName") String zipName,
                            @RequestParam("ids[]") String[] ids, HttpServletResponse response, HttpServletRequest request) throws IOException {

    }

    // endregion

    /**
     * 下载项目静态资源
     */
    @RequestMapping(value = "/download-static", method = RequestMethod.GET)
    public Result downloadProjectStaticFile(@RequestParam("path") String path, HttpServletResponse response) throws ValidationException {

        return Result.success();
    }

    /**
     * 批量下载文件
     */
    @RequestMapping(value = "/download-zips", method = RequestMethod.POST)
    @ResponseBody
    public void downloadZips(String ids, String fileName, HttpServletResponse response, HttpServletRequest request) throws IOException {

    }

}
