package com.upload.controller;

import cn.hutool.core.util.ArrayUtil;
import com.upload.service.UploadService;
import com.upload.config.*;
import com.upload.util.WatermarkUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.FutureTask;

/**
 * 图片上传接口
 */
@Slf4j
@AllArgsConstructor
@RestController
public class UploadController {
    UploadService uploadService;
    SystemConfiguration systemConfiguration;

    /**
     * 批量上传多个文件
     *
     * @param multipartFiles 文件列表
     * @return RestResponse
     */
    @PostMapping("uploads")
    public RestResponse<?> uploads(@RequestParam("files") MultipartFile[] multipartFiles, @RequestParam(required = false) String workerName, @RequestParam(required = false) String registrationCode) throws IOException {
        Validate.notTrue(ArrayUtil.isEmpty(multipartFiles), ValuePool.UPLOAD_DATA_MISSING);
        Map<String, byte[]> fileMap = new LinkedHashMap<>();
        //1 先解析文件格式
        for (MultipartFile multipartFile : multipartFiles) {
            String fileName = uploadService.handleFile(multipartFile);
            byte[] fileByte;
            if (registrationCode == null || workerName == null) {
                fileByte = multipartFile.getBytes();
            } else {
                fileByte = WatermarkUtil.pressImageAndWorld(multipartFile, ValuePool.MARK_IMAGE_PATH, workerName, registrationCode);
            }
            fileMap.put(fileName, fileByte);
        }
        //2 批量上传
        return AliyunOSS.me().putObjects(fileMap);
    }

    /**
     * 上传单个文件
     *
     * @param multipartFile 上传的文件
     * @return RestResponse
     */
    @PostMapping("upload")
    public RestResponse<?> upload(@RequestParam("file") MultipartFile multipartFile, @RequestParam(required = false) String workerName, @RequestParam(required = false) String registrationCode) throws IOException {
        Validate.notTrue(Objects.isNull(multipartFile) || multipartFile.isEmpty(), ValuePool.UPLOAD_DATA_MISSING);
        String fileName = uploadService.handleFile(multipartFile);
        byte[] fileByte;
        if (registrationCode == null || workerName == null) {
            fileByte = multipartFile.getBytes();
        } else {
            fileByte = WatermarkUtil.pressImageAndWorld(multipartFile, ValuePool.MARK_IMAGE_PATH, workerName, registrationCode);
        }
        try {
            FutureTask<String> futureTask = new FutureTask<>(() ->
                    systemConfiguration.build().putObject(fileName, fileByte));
            new Thread(futureTask).start();
            if (!futureTask.isDone())
                log.info("task has not finished!");
            String url = futureTask.get();
            log.info("upload file complete, file URL: {}", url);
            return RestResponse.success(url);
        } catch (Exception e) {
            log.error("upload file occur exception", e);
            return RestResponse.fail(ValuePool.UPLOAD_FAIL);
        }
    }
}
