package org.jeecg.modules.wps.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.wps.base.Response;
import org.jeecg.modules.wps.dto.FileReqDTO;
import org.jeecg.modules.wps.service.IWpsFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zm
 * 文件相关回调接口
 */
@Slf4j
@RestController
@RequestMapping("v1/3rd/file")
public class FileCallBackController{

    @Autowired
    public FileCallBackController(IWpsFileService fileService) {
        this.wpsFileService = fileService;
    }

    private final IWpsFileService wpsFileService;

    /**
     * 获取文件元数据
     */
    @GetMapping("info")
    public ResponseEntity<Object> getFileInfo(String _w_userid, String _w_filepath, String _w_filetype, HttpServletRequest request){
        String fileId = request.getHeader("x-weboffice-file-id");
        log.info("获取文件id：{}",fileId);
        log.info("获取文件元数据userId:{},path:{},type:{}",_w_userid,_w_filepath,_w_filetype);
        try {
            Map<String,Object> map = wpsFileService.getFileInfo(_w_userid,_w_filepath,_w_filetype,fileId);
            return Response.success(map);
        }catch (Exception e){
            e.printStackTrace();
            return Response.bad("获取文件元数据异常");
        }
    }

    /**
     * 通知此文件目前有哪些人正在协作
     */
    @PostMapping("online")
    public ResponseEntity<Object> fileOnline(@RequestBody JSONObject obj){
        log.info("通知此文件目前有哪些人正在协作param:{}", JSON.toJSON(obj));
        return Response.success();
    }

    /**
     * 上传文件新版本
     */
    @PostMapping("save")
    public ResponseEntity<Object> fileSave(@RequestBody MultipartFile file, String _w_userid,HttpServletRequest request){
        log.info("上传文件新版本");
        String fileId = request.getHeader("x-weboffice-file-id");
        Map<String,Object> map = wpsFileService.fileSave(file,_w_userid,fileId);
        return Response.success(map);
    }

    /**
     * 获取特定版本的文件信息
     */
    @GetMapping("version/{version}")
    public ResponseEntity<Object> fileVersion(@PathVariable Integer version){
        log.info("获取特定版本的文件信息version:{}",version);
        Map<String,Object> res = wpsFileService.fileVersion(version);
        return Response.success(res);
    }

    /**
     * 文件重命名
     */
    @PutMapping("rename")
    public ResponseEntity<Object> fileRename(@RequestBody FileReqDTO req, String _w_userid,HttpServletRequest request){
        log.info("文件重命名param:{},userId:{}", JSON.toJSON(req),_w_userid);
        String fileId = request.getHeader("x-weboffice-file-id");
        wpsFileService.fileRename(req.getName(),_w_userid,fileId);
        return Response.success();
    }

    /**
     * 获取所有历史版本文件信息
     */
    @PostMapping("history")
    public ResponseEntity<Object> fileHistory(@RequestBody FileReqDTO req){
        log.info("获取所有历史版本文件信息param:{}", JSON.toJSON(req));
        Map<String,Object> result = wpsFileService.fileHistory(req);
        return Response.success(result);
    }

    /**
     * 新建文件
     */
    @PostMapping("new")
    public ResponseEntity<Object> fileNew(@RequestBody MultipartFile file, String _w_userid){
        log.info("新建文件_w_userid:{}",_w_userid);
        Map<String,Object> res = wpsFileService.fileNew(file,_w_userid);
        return Response.success(res);
    }
}
