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

import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.file.service.IFileService;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.properties.FileProperties;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * @ClassName: FileController
 * @Description: 文件
 * @Author: WangGeng
 * @Date: 2019/3/11 2:45 PM
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "文件管理接口")
@Controller
@RequestMapping(ISystemConstant.ROUTE_PREFIX + "/file")
public class FileRouteController extends DefaultBaseController {

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

    /**
     * 文件页面
     *
     * @return
     */
    @GetMapping("listinfo")
    public ModelAndView listFileInfo() {
        ModelAndView mv = new ModelAndView("file/list-info");
        return mv;
    }

    @ApiOperation(value = "打开文件上传", notes = "打开文件上传接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uploadType", value = "文件类型,1:文件,2:图片,3:视频,4:音频", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("uploadfile/{uploadType}")
    public ModelAndView uploadFile(@PathVariable("uploadType") String uploadType) {
        ModelAndView mv = new ModelAndView("file/file-upload");
        return getUploadFileModelAndView(mv, uploadType);
    }

    private ModelAndView getUploadFileModelAndView(ModelAndView mv, String uploadType) {
        if (!IFileService.UPLOAD_FILE_TYPE.equals(uploadType) &&
                !IFileService.UPLOAD_IMAGE_TYPE.equals(uploadType) &&
                !IFileService.UPLOAD_VIDEO_TYPE.equals(uploadType) &&
                !IFileService.UPLOAD_AUDIO_TYPE.equals(uploadType)) {
            throw new ParamsException("参数错误");
        }
        if (IFileService.UPLOAD_FILE_TYPE.equals(uploadType)) {
            mv.addObject("uploadPath", "uploadfile");
            mv.addObject("updateInputName", "file");
            mv.addObject("allowFiles", fileProperties.getFileTypes());
        }
        if (IFileService.UPLOAD_IMAGE_TYPE.equals(uploadType)) {
            mv.addObject("uploadPath", "uploadimage");
            mv.addObject("updateInputName", "image");
            mv.addObject("allowFiles", fileProperties.getImageTypes());
        }
        if (IFileService.UPLOAD_VIDEO_TYPE.equals(uploadType)) {
            mv.addObject("uploadPath", "uploadvideo");
            mv.addObject("updateInputName", "video");
            mv.addObject("allowFiles", fileProperties.getVideoTypes());
        }
        if (IFileService.UPLOAD_AUDIO_TYPE.equals(uploadType)) {
            mv.addObject("uploadPath", "uploadaudio");
            mv.addObject("updateInputName", "audio");
            mv.addObject("allowFiles", fileProperties.getAudioTypes());
        }
        mv.addObject("uploadType", uploadType);
        mv.addObject("maxFileCount", fileProperties.getMaxFileCount());
        return mv;
    }

    /**
     * 上传图片
     *
     * @return
     */
    @GetMapping("uploadimage")
    public ModelAndView uploadImage() {
        Map<String, Object> params = requestParams();
        ModelAndView mv = new ModelAndView("file/image-upload");
        return getUploadImageModelAndView(mv, params);
    }

    private ModelAndView getUploadImageModelAndView(ModelAndView mv, Map<String, Object> params) {
        if (!StringUtils.isBlank(params.get("fileId") == null ? null : params.get("fileId").toString())) {
            mv.addObject("fileId", params.get("fileId"));
        } else {
            mv.addObject("fileId", "");
        }
        return mv;
    }

    @ApiOperation(value = "下载文件", notes = "下载文件接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isOpen", value = "是否打开,true和false", paramType = "path"),
            @ApiImplicitParam(name = "fileId", value = "文件ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("download/{isOpen}/{fileId}")
    public void downLoad(HttpServletRequest request, HttpServletResponse response, @PathVariable("isOpen") Boolean isOpen, @PathVariable("fileId") String fileId) {
        if (isOpen == null) {
            throw new ParamsException("参数错误");
        }
        if (fileId.indexOf(".") > 0) {
            fileId = fileId.split("\\.")[0];
        }
        Map<String, Object> params = getParams();
        params.put("fileId", fileId);
        params.put("isOpen", isOpen);
        fileService.downLoadFile(request, response, params);
    }

    /**
     * 显示文件
     *
     * @param type
     * @param fileId
     * @return
     */
    @GetMapping("show/{type}/{fileId}")
    public ModelAndView show(@PathVariable("type") String type, @PathVariable("fileId") String fileId) {
        ModelAndView mv = new ModelAndView("file/show");
        mv.addObject("type", type);
        mv.addObject("fileId", fileId);
        return mv;
    }

}
