package com.fzm.mall.server.admin.system.controller;

import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import com.fzm.mall.server.admin.util.OSSUtil;
import com.fzm.mall.server.admin.util.SerialIdsUtil;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

@Api(tags = "系统-资源上传接口")
@ApiSupport(order = 11)
@Slf4j
@RestController
@RequestMapping("/upload")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UploadController {

    private final ResponseFactory resFac;

    private static final ArrayList<String> imgTypeArr = new ArrayList<String>(Arrays.asList(".jpg", ".jpeg", ".png", ".gif", ".bmp", ".xlsx"));
    private static final ArrayList<String> videoTypeArr = new ArrayList<String>(Arrays.asList(".mp4", ".mkv"));
    private static final ArrayList<String> apkTypeArr = new ArrayList<String>(Arrays.asList(".apk", ".xlsx"));
    private static final ArrayList<String> nftTypeArr = new ArrayList<String>(Arrays.asList(
            ".jpg", ".jpeg", ".gif", ".bmp", ".png",
            ".mp4", ".wmv",
            ".mp3", ".wav",
            ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt"));

    @PostMapping("/common/img")
    public ResponseVO uploadImg(@RequestParam(value = "file") MultipartFile file) {
        String fileType = getFileType(file.getOriginalFilename());
        if (!imgTypeArr.contains(fileType)) {
            return resFac.getResponse(MallResponseError.UPLOAD_FORMAT_ERROR);
        }
        String url = uploadFile(file, "img");
        return resFac.getSuccessResponseWithData(url);
    }

    @PostMapping("/common/video")
    public ResponseVO<String> uploadVideo(@RequestBody MultipartFile file) {
        String fileType = getFileType(file.getOriginalFilename());
        if (!videoTypeArr.contains(fileType)) {
            return resFac.getResponse(MallResponseError.UPLOAD_FORMAT_ERROR);
        }
        String url = uploadFile(file, "video");
        return resFac.getSuccessResponseWithData(url);
    }

    @PostMapping("/common/apk")
    public ResponseVO uploadApk(@RequestParam(value = "file") MultipartFile file) {
        String fileType = getFileType(file.getOriginalFilename());
        if (!apkTypeArr.contains(fileType)) {
            return resFac.getResponse(MallResponseError.UPLOAD_FORMAT_ERROR);
        }
        String url = uploadFile(file, "apk");
        return resFac.getSuccessResponseWithData(url);
    }

    @PostMapping("/common/nft")
    public ResponseVO uploadNft(@RequestParam(value = "file") MultipartFile file) {
        String fileType = getFileType(file.getOriginalFilename());
        if (!nftTypeArr.contains(fileType)) {
            return resFac.getResponse(MallResponseError.UPLOAD_FORMAT_ERROR);
        }
        String url = uploadFile(file, "fileType");
        return resFac.getSuccessResponseWithData(url);
    }

    private String uploadFile(MultipartFile file, String type) {
        String fileType = getFileType(file.getOriginalFilename());
        byte[] bytes = null;
        try {
            bytes = file.getBytes();
        } catch (IOException e) {
            log.error("upload error:", e);
            e.printStackTrace();
        }
        return OSSUtil.uploadByte(bytes, "manage/" + type + "/" + SerialIdsUtil.createDateStr() + "/", SerialIdsUtil.createFileName(fileType));
    }

    private String getFileType(String fileName) {
        if (fileName != null && fileName.indexOf(".") >= 0) {
            return fileName.substring(fileName.lastIndexOf("."), fileName.length());
        }
        return "";
    }

}
