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

import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.file.enums.UploadTypeEnum;
import ink.wgink.module.file.manager.FileRemoteManager;
import ink.wgink.module.file.pojo.dtos.v2.FileUploadSuccessDTO;
import ink.wgink.module.file.service.filecenter.IFileCenterService;
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 javax.servlet.http.HttpServletResponse;

/**
 * @ClassName: FileAppCenterController
 * @Description: APP文件远程
 * @Author: WangGeng
 * @Date: 2019-08-02 15:38
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_APP_PREFIX + "文件远程调用接口")
@RestController("fileAppCenterControllerV2")
@RequestMapping(ISystemConstant.APP_PREFIX + "/file-center")
public class FileCenterAppController extends DefaultBaseController {

    @Autowired
    private IFileCenterService fileCenterService;

    @ApiOperation(value = "上传文件", notes = "上传文件接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Access-Key", value = "Access-Key", paramType = "header"),
            @ApiImplicitParam(name = "Access-Secret", value = "Access-Secret", paramType = "header"),
            @ApiImplicitParam(name = "file", value = "文件", paramType = "form"),
            @ApiImplicitParam(name = "creator", value = "创建人", paramType = "form")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("upload-file")
    public SuccessResultData<FileUploadSuccessDTO> uploadFile(@RequestHeader("Access-Key") String accessKey,
                                                              @RequestHeader("Access-Secret") String accessSecret,
                                                              @RequestParam("file") MultipartFile file,
                                                              @RequestParam("creator") String creator) {
        checkKeyAndSecret(accessKey, accessSecret);
        return new SuccessResultData<>(fileCenterService.uploadSingleByUserId(creator, file, UploadTypeEnum.FILE));
    }

    @ApiOperation(value = "上传图片", notes = "上传图片接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Access-Key", value = "Access-Key", paramType = "header"),
            @ApiImplicitParam(name = "Access-Secret", value = "Access-Secret", paramType = "header"),
            @ApiImplicitParam(name = "image", value = "图片", paramType = "form"),
            @ApiImplicitParam(name = "creator", value = "创建人", paramType = "form")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("upload-image")
    public SuccessResultData<FileUploadSuccessDTO> uploadImage(@RequestHeader("Access-Key") String accessKey,
                                                               @RequestHeader("Access-Secret") String accessSecret,
                                                               @RequestParam("image") MultipartFile image,
                                                               @RequestParam("creator") String creator) {
        checkKeyAndSecret(accessKey, accessSecret);
        return new SuccessResultData<>(fileCenterService.uploadSingleByUserId(creator, image, UploadTypeEnum.IMAGE));
    }

    @ApiOperation(value = "上传视频", notes = "上传视频接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Access-Key", value = "Access-Key", paramType = "header"),
            @ApiImplicitParam(name = "Access-Secret", value = "Access-Secret", paramType = "header"),
            @ApiImplicitParam(name = "video", value = "视频", paramType = "form"),
            @ApiImplicitParam(name = "creator", value = "创建人", paramType = "form")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("upload-video")
    public SuccessResultData<FileUploadSuccessDTO> uploadVideo(@RequestHeader("Access-Key") String accessKey,
                                                               @RequestHeader("Access-Secret") String accessSecret,
                                                               @RequestParam("video") MultipartFile video,
                                                               @RequestParam("creator") String creator) {
        checkKeyAndSecret(accessKey, accessSecret);
        return new SuccessResultData<>(fileCenterService.uploadSingleByUserId(creator, video, UploadTypeEnum.VIDEO));
    }

    @ApiOperation(value = "上传音频", notes = "上传音频接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Access-Key", value = "Access-Key", paramType = "header"),
            @ApiImplicitParam(name = "Access-Secret", value = "Access-Secret", paramType = "header"),
            @ApiImplicitParam(name = "audio", value = "音频", paramType = "form"),
            @ApiImplicitParam(name = "creator", value = "创建人", paramType = "form")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("upload-audio")
    public SuccessResultData<FileUploadSuccessDTO> uploadAudio(@RequestHeader("Access-Key") String accessKey,
                                                               @RequestHeader("Access-Secret") String accessSecret,
                                                               @RequestParam("audio") MultipartFile audio,
                                                               @RequestParam("creator") String creator) {
        checkKeyAndSecret(accessKey, accessSecret);
        return new SuccessResultData<>(fileCenterService.uploadSingleByUserId(creator, audio, UploadTypeEnum.AUDIO));
    }

    @ApiOperation(value = "文件下载", notes = "文件下载（用于直接下载）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileId", value = "文件ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("download/{fileId}")
    public void download(HttpServletRequest request,
                         HttpServletResponse response,
                         @PathVariable("fileId") String fileId) {
        fileCenterService.download(request, response, fileId);
    }

    /**
     * 校验Key与Secret
     *
     * @param accessKey
     * @param accessSecret
     */
    private void checkKeyAndSecret(String accessKey, String accessSecret) {
        if (!FileRemoteManager.getInstance().check(accessKey, accessSecret)) {
            throw new ParamsException("Key与Secret不匹配");
        }
    }

}
