package ink.wgink.module.file.controller.api;

import com.alibaba.fastjson.JSONObject;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.file.enums.UploadTypeEnum;
import ink.wgink.module.file.pojo.dtos.FileDTO;
import ink.wgink.module.file.pojo.dtos.FileInfoDTO;
import ink.wgink.module.file.service.IFileService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @ClassName: FileController
 * @Description: 文件处理
 * @Author: WangGeng
 * @Date: 2019/3/10 7:03 PM
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "文件接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/file")
public class FileController extends DefaultBaseController {

    @Autowired
    private IFileService fileService;

    @ApiOperation(value = "文件分页列表", notes = "文件分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpageinfo")
    public SuccessResultList<List<FileInfoDTO>> listPageInfo(ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return fileService.listPageInfo(page);
    }

    @ApiOperation(value = "删除文件类别(id列表)", notes = "删除文件类别(id列表)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("remove/{isRemoveSource}/{ids}")
    public synchronized SuccessResult remove(@PathVariable() Integer isRemoveSource, @PathVariable("ids") String ids) {
        if (isRemoveSource == 0) {
            fileService.remove(ids);
        }
        if (isRemoveSource == 1) {
            fileService.delete(ids);
        }
        return new SuccessResult();
    }

    @ApiOperation(value = "上传文件", notes = "上传文件接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件name", paramType = "query")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("uploadfile")
    public Callable<SuccessResultData<String>> uploadFile(@RequestParam("file") MultipartFile file) {
        Map<String, Object> params = requestParams();
        return asyncUploadSingle(file, UploadTypeEnum.FILE, params);
    }

    @ApiOperation(value = "上传图片", notes = "上传图片接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "image", value = "文件name", paramType = "query")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("uploadimage")
    public Callable<SuccessResultData<String>> uploadImage(@RequestParam("image") MultipartFile image) {
        Map<String, Object> params = requestParams();
        return asyncUploadSingle(image, UploadTypeEnum.IMAGE, params);
    }

    @ApiOperation(value = "上传视频", notes = "上传视频接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "video", value = "文件video", paramType = "query")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("uploadvideo")
    public Callable<SuccessResultData<String>> uploadVideo(@RequestParam("video") MultipartFile video) {
        Map<String, Object> params = requestParams();
        return asyncUploadSingle(video, UploadTypeEnum.VIDEO, params);
    }

    @ApiOperation(value = "上传音频", notes = "上传音频接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "audio", value = "文件audio", paramType = "query")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("uploadaudio")
    public Callable<SuccessResultData<String>> uploadAudio(@RequestParam("audio") MultipartFile audio) {
        Map<String, Object> params = requestParams();
        return asyncUploadSingle(audio, UploadTypeEnum.AUDIO, params);
    }

    @ApiOperation(value = "文件列表", notes = "文件列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id列表，逗号分隔", paramType = "query")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list")
    public List<FileDTO> listFileByFileId(@RequestParam("ids") String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        return fileService.list(idList);
    }

    /**
     * 上传文件
     *
     * @param uploadFile
     * @param params
     * @return
     */
    private SuccessResultData<String> uploadSingle(MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map<String, Object> params) {
        return new SuccessResultData<>(fileService.uploadSingle(uploadFile, uploadTypeEnum, params));
    }

    /**
     * 异步上传
     *
     * @param uploadFile
     * @param uploadTypeEnum
     * @param params
     * @return
     */
    private Callable<SuccessResultData<String>> asyncUploadSingle(MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map<String, Object> params) {
        return () -> new SuccessResultData<>(fileService.uploadSingle(uploadFile, uploadTypeEnum, params));
    }

    /**
     * 百度富文本编辑器
     *
     * @param file
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "百度富文本配置", notes = "百度富文本配置接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @RequestMapping("ueditor")
    public JSONObject uEditor(MultipartFile file, HttpServletRequest request) throws IOException {
        Map<String, Object> params = requestParams();
        return fileService.uEditor(file, request, params);
    }

    @ApiOperation(value = "wangEditor上传图片", notes = "wangEditor上传图片接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "image", value = "文件name", paramType = "query")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("wangeditorimage")
    public JSONObject wangEditorImage(MultipartFile image, HttpServletRequest request) {
        Map<String, Object> params = requestParams();
        return fileService.wangEditorImage(image, UploadTypeEnum.IMAGE, params);
    }

}
