package com.cm.common.plugin.controller.app.apis.file;

import com.alibaba.fastjson.JSONObject;
import com.cm.common.base.AbstractController;
import com.cm.common.config.properties.FileProperties;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.enums.UploadTypeEnum;
import com.cm.common.exception.base.SystemException;
import com.cm.common.plugin.service.file.IFileService;
import com.cm.common.pojo.dtos.FileDTO;
import com.cm.common.result.ErrorResult;
import com.cm.common.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;

/**
 * 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 AbstractController {

    @Autowired
    private IFileService fileService;
    @Autowired
    private FileProperties fileProperties;

    @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 SuccessResultData<String> uploadFile(@RequestHeader("token") String token, @RequestParam("file") MultipartFile file) throws SystemException {
        Map<String, Object> params = requestParams();
        return uploadSingle(token, 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 SuccessResultData<String> uploadImage(@RequestHeader("token") String token, @RequestParam("image") MultipartFile image) throws SystemException {
        Map<String, Object> params = requestParams();
        return uploadSingle(token, 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 SuccessResultData<String> uploadVideo(@RequestHeader("token") String token, @RequestParam("video") MultipartFile video) throws SystemException {
        if (fileProperties.getMediaMaxDuration() != null && fileProperties.getMediaMaxDuration().getApp() != null) {
            fileService.checkVideoDurationAllow(video, fileProperties.getMediaMaxDuration().getApp().getVideo());
        }
        Map<String, Object> params = requestParams();
        return uploadSingle(token, 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 SuccessResultData<String> uploadAudio(@RequestHeader("token") String token, @RequestParam("audio") MultipartFile audio) throws SystemException {
        if (fileProperties.getMediaMaxDuration() != null && fileProperties.getMediaMaxDuration().getApp() != null) {
            fileService.checkAudioDurationAllow(audio, fileProperties.getMediaMaxDuration().getApp().getAudio());
        }
        Map<String, Object> params = requestParams();
        return uploadSingle(token, 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("listfilebyids")
    public List<FileDTO> listFileByFileId(@RequestParam("ids") String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        return fileService.listFileByFileId(idList);
    }

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

}
