package com.persagy.dmp.file.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import com.persagy.dmp.common.utils.ResourceUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.Charset;

/***
 * Description: 兼容旧的文件服务接口
 * @author : lijie
 * @date :2021/11/18 10:06
 * Update By lijie 2021/11/18 10:06
 */
@RestController
@RequiredArgsConstructor
public class CompatibleOldFileController {

    private static final String DOCUMENTATION= "doc/documentation.md";
    private static final String TEMPLATE= "doc/template.html";
    private static final String ERROR_HTML="<html><head>" +
            "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>" +
            "<title>image-service</title></head></html><body>{}</body>";
    /***
     * Description: 获取接口文档
     * @param response : 响应体
     * @return : void
     * @author : lijie
     * @date :2021/11/19 9:57
     * Update By lijie 2021/11/19 9:57
     */
    @GetMapping(value = "/doc",produces = {"text/html;charset=UTF-8"})
    public void doc(HttpServletResponse response) throws IOException {
        Resource[] docResources = ResourceUtil.getResource(DOCUMENTATION, DOCUMENTATION);
        Resource[] templateResources = ResourceUtil.getResource(TEMPLATE, TEMPLATE);
        if (templateResources.length<=0
                || docResources.length<=0
                || !templateResources[0].exists()
                || !docResources[0].exists()){
            response.getWriter()
                    .write(StrUtil.format(ERROR_HTML,"暂无内容"));
            return;
        }
        Resource docResource = docResources[0];
        Resource templateResource = templateResources[0];
        String tempStr = FileUtil.readString(templateResource.getFile(), Charset.defaultCharset());
        String docContentStr = FileUtil.readString(docResource.getFile(), Charset.defaultCharset());
        response.getWriter()
                .write(StrUtil.replace(tempStr, "#replaceMePlease#", docContentStr));
    }
    /***
     * Description: 文件上传
     * @return : void
     * @author : lijie
     * @date :2021/11/19 9:58
     * Update By lijie 2021/11/19 9:58
     */
    @PostMapping(value = "/common/file_upload",produces = {MediaType.APPLICATION_JSON})
    public void fileUpload(HttpServletResponse response) throws IOException {
        Resource[] docResources = ResourceUtil.getResource(DOCUMENTATION, DOCUMENTATION);
        Resource[] templateResources = ResourceUtil.getResource(TEMPLATE, TEMPLATE);
        if (templateResources.length<=0
                || docResources.length<=0
                || !templateResources[0].exists()
                || !docResources[0].exists()){
            response.getWriter()
                    .write(StrUtil.format(ERROR_HTML,"暂无内容"));
            return;
        }
        Resource docResource = docResources[0];
        Resource templateResource = templateResources[0];
        String tempStr = FileUtil.readString(templateResource.getFile(), Charset.defaultCharset());
        String docContentStr = FileUtil.readString(docResource.getFile(), Charset.defaultCharset());
        response.getWriter()
                .write(StrUtil.replace(tempStr, "#replaceMePlease#", docContentStr));
    }





}
