package cn.superdesk.uniorder.common.rest.response;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.json.JSONUtil;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2022/3/11 下午12:54
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022/3/11 下午12:54
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@NoArgsConstructor
public class AnResponseStreamUtils {

    /**
     * @param fileFullPath
     * @param fileName
     * @param response
     * @throws IOException
     */
    public static void download(
            /* 文件绝对地址 全路径 */String fileFullPath,
            /* 下载文件名         */String fileName,
                            HttpServletResponse response) throws IOException {

        log.info("AnResponseStreamUtils.download \n#{}#", fileFullPath);

        if (!FileUtil.exist(fileFullPath)) {
            outputJson(JSONUtil.toJsonStr(ResponseResultOuterUtils.createErrorResult("500", "文件不存在或过期被清理")), response);
            return;
        }

        // 准备将Excel的输出流通过response输出到页面下载
//        response.reset();
        response.setCharacterEncoding("UTF-8");
        // 二进制输出流
//        response.setContentType("application/octet-stream");
        response.setContentType("application/force-download");
//		response.addHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        // 设置编码,避免文件名中文乱码
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

//      ServletOutputStream outputStream = response.getOutputStream();
//      outputStream.write(FileUtil.readBytes(fileFullPath));
//      IoUtil.close(outputStream);

        OutputStream outputStream = null;

        try {
            outputStream = response.getOutputStream();
            outputStream.write(FileUtil.readBytes(fileFullPath));
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    public static void previewPdf(
            /* 文件绝对地址 全路径 */String fileFullPath,
            /* 下载文件名         */String fileName,
                            HttpServletResponse response) throws IOException {
//		produces = MediaType.APPLICATION_PDF_VALUE

        if (!FileUtil.exist(fileFullPath)) {
            outputJson(JSONUtil.toJsonStr(ResponseResultOuterUtils.createErrorResult("500", "文件不存在或过期被清理")), response);
            return;
        } else {


            // 准备将Excel的输出流通过response输出到页面下载
            response.reset();
            response.addHeader("Access-Control-Allow-Origin", "*");
            // 二进制输出流
            response.setContentType(MediaType.APPLICATION_PDF_VALUE);
            response.setCharacterEncoding("UTF-8");

            // 设置编码,避免文件名中文乱码
            response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(fileName, "UTF-8"));

            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(FileUtil.readBytes(fileFullPath));
            IoUtil.close(outputStream);
        }


    }

    public static void outputJson(String json, HttpServletResponse response) {

//        response.setContentType("application/json;charset=utf-8");
//
//        PrintWriter out;
//
//        try {
//            out = response.getWriter();
//
//            out.print(json);
//            out.flush();
//            out.close();
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }
}
