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

import com.fzm.mall.server.front.base.ResponseVO;
import com.fzm.mall.server.front.constant.MallResponseEnum;
import com.fzm.mall.server.front.util.OSSUtil;
import com.fzm.mall.server.front.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;
import java.util.Locale;

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


    private static final ArrayList<String> imgTypeArr = new ArrayList<String>(Arrays.asList(".jpg", ".jpeg", ".png", ".gif", ".bmp"));
    private static final ArrayList<String> videoTypeArr = new ArrayList<String>(Arrays.asList(".mp4", ".mkv",".mov"));
    private static final ArrayList<String> apkTypeArr = new ArrayList<String>(Arrays.asList(".apk"));

    @PostMapping("/common/img")
    public ResponseVO<String> uploadImg(@RequestParam(value = "file") MultipartFile file) {
        String fileType = getFileType(file.getOriginalFilename());
        if (!imgTypeArr.contains(fileType.toLowerCase())) {
            return new ResponseVO(MallResponseEnum.UPLOAD_FORMAT_ERROR);
        }
        String url = uploadFile(file, "img");
        return new ResponseVO(MallResponseEnum.SUCCESS,url);
    }

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

    @PostMapping("/common/apk")
    public ResponseVO uploadApk(@RequestParam(value = "file") MultipartFile file) {
        String fileType = getFileType(file.getOriginalFilename());
        if (!apkTypeArr.contains(fileType.toLowerCase())) {
            return new ResponseVO(MallResponseEnum.UPLOAD_FORMAT_ERROR);
        }
        String url = uploadFile(file, "apk");
        return new ResponseVO(MallResponseEnum.SUCCESS,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, "", SerialIdsUtil.createFileName(fileType));
    }

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

}
