package com.zhongshu.card.server.core.controller.upLoadFile;

import com.github.microservice.auth.security.annotations.ResourceAuth;
import com.github.microservice.auth.security.type.AuthType;
import com.github.microservice.net.ResultContent;
import com.github.microservice.netdisk.client.model.file.FileItemModel;
import com.github.microservice.netdisk.client.model.file.FileStoreModel;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.zhongshu.card.client.model.base.OidParam;
import com.zhongshu.card.server.core.model.oss.FileUploadFinishParam;
import com.zhongshu.card.server.core.model.upLoadFile.RemoteBusFile;
import com.zhongshu.card.server.core.model.upLoadFile.RemoteGetTokenFileParam;
import com.zhongshu.card.server.core.model.upLoadFile.StsToken;
import com.zhongshu.card.server.core.service.oss.NetDiskOssBusService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Cleanup;
import lombok.SneakyThrows;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.http.MediaType;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * @author TRX
 * @date 2024/4/10
 */
@Validated
@RestController
@RequestMapping("oss")
@Tag(name = "上传文件OSS")
public class OssController {

    @Autowired
    NetDiskOssBusService netDiskOssService;

    @ResourceAuth(value = "user", type = AuthType.User)
    @RequestMapping(value = "uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, method = RequestMethod.POST)
    @Operation(summary = "网盘直接上传文件", description = "网盘直接上传文件")
    public ResultContent<String> uploadFile(HttpServletRequest request, HttpServletResponse response, @RequestPart("file") MultipartFile file, OidParam param) throws IOException {
        if (file == null || file.isEmpty()) {
            return ResultContent.buildFail("文件不能为空");
        }
        return netDiskOssService.uploadMinioFile(file, param);
    }

    @RequestMapping(value = "/dl/{filename}", method = RequestMethod.GET)
    @SneakyThrows
    public void download(HttpServletResponse response, @PathVariable("filename") String filename) {
        netDiskOssService.download(response, filename);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到上传文件Token", description = "得到上传文件Token")
    @RequestMapping(value = "getUploadToken", method = {RequestMethod.POST})
    public ResultContent<StsToken> getUploadToken(@RequestBody RemoteBusFile remoteFile) {
        return netDiskOssService.getUploadToken(remoteFile);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到上传文件Token", description = "得到上传文件Token")
    @RequestMapping(value = "getUploadFileToken", method = {RequestMethod.POST})
    public ResultContent<com.github.microservice.netdisk.client.model.token.StsToken> getUploadToken(@RequestBody @Valid RemoteGetTokenFileParam param) {
        return netDiskOssService.getUploadFileToken(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "上传完成", description = "上传完成")
    @RequestMapping(value = "uploadFinish", method = {RequestMethod.POST})
    public ResultContent<FileStoreModel> uploadFinish(@RequestBody @Valid FileItemModel param) {
        return netDiskOssService.uploadFinish(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "上传完成（多个）", description = "上传完成（多个）")
    @RequestMapping(value = "uploadFinishMuli", method = {RequestMethod.POST})
    public ResultContent<List<FileStoreModel>> uploadFinishMuli(@RequestBody @Valid FileUploadFinishParam param) {
        if (ObjectUtils.isEmpty(param.getFileItems())) {
            return ResultContent.buildFail("fileItems 不能为空");
        }
        FileItemModel[] fileItems = param.getFileItems().toArray(new FileItemModel[0]);
        return netDiskOssService.uploadFinishMuli(fileItems);
    }
}
