package com.inkasso.factoring.file.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.util.StringUtil;
import com.inkasso.adapter.enums.FileBusinessInterface;
import com.inkasso.adapter.response.ResponseData;
import com.inkasso.factoring.common.util.onlineConverter.OfficeConverterUtil;
import com.inkasso.factoring.common.util.onlineConverter.TextConverterPdfUtil;
import com.inkasso.factoring.file.entity.FileStorageDO;
import com.inkasso.factoring.file.enums.FileTypeEnum;
import com.inkasso.factoring.file.service.FileService;
import com.inkasso.factoring.file.util.FileUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@RestController
@RequestMapping("system")
public class FileController {

    private Logger log = LogManager.getLogger(FileController.class);
    private static String PREFIX = "sys/fileStorage/";

    @Autowired
    private FileService fileService;

    /**
     * 文件上传
     *
     * @param fileType
     * @param request
     * @return
     */
    @PostMapping(value = "fileStorage")
    public ResponseData doUploadFiles(@RequestParam("fileType") String fileType, HttpServletRequest request) {
        FileTypeEnum fileTypeEnum = FileTypeEnum.getByCode(fileType);
        FileStorageDO fileStorageDO = fileService.uploadFile(request, fileTypeEnum);
        return ResponseData.success(fileStorageDO);
    }

    /**
     * 删除附件
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "fileStorage/{id}")
    public ResponseData delete(@Validated @PathVariable("id") Long id) {
        fileService.delete(id);
        return ResponseData.success();
    }

    /**
     * 文件上传
     *
     * @param files
     * @param request
     * @return
     */
    @PostMapping(value = "fileStorages")
    public ResponseData doUploadFiles(@RequestParam("fileType") List<String> files, HttpServletRequest request) {
        if (CollectionUtil.isNotEmpty(files)) {
            List<FileBusinessInterface> list = new ArrayList<>();
            for (String s : files) {
                FileTypeEnum fileTypeEnum = FileTypeEnum.getByCode(s);
                if (fileTypeEnum != null) {
                    list.add(fileTypeEnum);
                }
            }
            fileService.uploadFiles(request, list);
        }
        return ResponseData.success();
    }

    /**
     * 文件下载（预览）
     *
     * @param absolutePath 文件路径
     * @param response
     */
    @GetMapping("/fileStorage/downloadPdfFile")
    public void downloadFile(String absolutePath, HttpServletResponse response) {
        if (absolutePath != null) {
            File file = new File(absolutePath);
            fileService.downloadPdfFile(response, file);
        }
    }


}
