package com.persagy.dmp.file.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.dmp.common.lang.PsDateTime;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ResultHelper;
import com.persagy.dmp.file.client.FileClientFacade;
import com.persagy.dmp.file.model.FileInfo;
import com.persagy.dmp.file.model.FileInfoCreator;
import com.persagy.dmp.file.model.FileRequestData;
import com.persagy.dmp.file.model.UploadMes;
import io.minio.MinioClient;
import io.minio.PostPolicy;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.Map;

/**
 * 文件管理Controller
 * @author Charlie Yu
 * @date 2021-05-15
 */
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private MinioClient minioClient;

    @PostMapping(value = "/uploadFile")
    @CrossOrigin
    public CommonResult<FileInfo> uploadFile(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        String fileName = multipartFile.getResource().getFilename();
        FileInfo fileInfo = FileInfoCreator.of(AppContext.getContext().getGroupCode(),
                IdWorker.getIdStr(), "ADM", fileName);
        fileInfo.setCreationTime(new PsDateTime());
        //String fileId = FileStorageHelper.uploadImage(fileInfo, multipartFile.getInputStream());
        FileInfo dest = new FileInfo();
        //dest.setId(fileId);
        return ResultHelper.single(dest);
    }
    @PostMapping(value = "/replaceFile")
    @CrossOrigin
    public CommonResult<FileInfo> replaceFile(@RequestParam("fileId") String fileId,
                                             @RequestParam("file") MultipartFile multipartFile) throws IOException {
        String fileName = multipartFile.getResource().getFilename();
        FileInfo fileInfo = FileInfoCreator.of(AppContext.getContext().getGroupCode(),
                IdWorker.getIdStr(), "ADM", fileName);
        fileInfo.setCreationTime(new PsDateTime());
        //FileStorageHelper.replaceFile(fileId,fileName, multipartFile.getInputStream());
        FileInfo dest = new FileInfo();
        dest.setId(fileId);
        return ResultHelper.single(dest);
    }
    @PostMapping(value = "/deleteFile")
    @CrossOrigin
    public CommonResult<FileInfo> deleteFile(@RequestParam("fileId") String fileId) throws IOException {
        //FileStorageHelper.deleteFile(fileId);
        FileInfo dest = new FileInfo();
        dest.setId(fileId);
        return ResultHelper.single(dest);
    }
    @PostMapping(value = "/downloadUrl")
    @CrossOrigin
    public CommonResult<FileInfo> downloadUrl(@RequestParam("fileId") String fileId) throws IOException {
        return ResultHelper.single(new FileInfo());//FileStorageHelper.downloadUrl(fileId));
    }
    @PostMapping(value = "/testInitChunkUpload")
    @CrossOrigin
    public CommonResult<UploadMes> testInitChunkUpload(@RequestBody FileRequestData requestData) throws IOException {
        return FileClientFacade.initChunkUploadPrototype(AppContext.getContext().getGroupCode(),
                AppContext.getContext().getProjectId(),
                AppContext.getContext().getAppId(),
                AppContext.getContext().getAccountId(),requestData);
    }
    @PostMapping(value = "/testInitSingleUpload")
    @CrossOrigin
    public CommonResult<UploadMes> testInitSingleUpload(@RequestBody FileRequestData requestData) throws IOException {
        return FileClientFacade.initSingleUploadPrototype(AppContext.getContext().getGroupCode(),
                AppContext.getContext().getProjectId(),
                AppContext.getContext().getAppId(),
                AppContext.getContext().getAccountId(),requestData);
    }

    @PostMapping(value = "/testInitMultiUploads")
    @CrossOrigin
    public String testInitMultiUploads(@RequestBody FileRequestData requestData) throws Exception {
        return "success";
    }

}
