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

import com.alibaba.fastjson.JSONObject;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.properties.FileProperties;
import ink.wgink.module.file.enums.UploadTypeEnum;
import ink.wgink.module.file.pojo.dtos.FileDTO;
import ink.wgink.module.file.service.IFileService;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResultData;
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.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: FileAppController
 * @Description: APP文件上传
 * @Author: WangGeng
 * @Date: 2019-08-02 15:38
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_APP_PREFIX + "文件管理接口")
@RestController
@RequestMapping(ISystemConstant.APP_PREFIX + "/file")
public class FileAppController extends DefaultBaseController {

    @Autowired
    private IFileService fileService;

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

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

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

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

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

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

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

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

    @ApiOperation(value = "上传图片", notes = "上传图片接口")
    @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);
    }

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

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

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

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

}
