package com.persagy.dmp.file.controller;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSONObject;
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 com.persagy.dmp.file.utils.FileStorageHelper;
import io.minio.*;
import lombok.SneakyThrows;
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.FileInputStream;
import java.io.IOException;

/**
 * 文件管理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";
    }

    @SneakyThrows
    public static void main(String[] args) {
        //byte[] streamBytes = FileStorageHelper.getStreamBytes(new FileInputStream(new File("D:\\test\\temp\\20211209_Eq440305000274029fde4b7f459a872d5e15646c285c.png")));
        byte[] streamBytes = FileStorageHelper.getStreamBytes(new FileInputStream(new File("D:\\test\\temp\\上海国际航空服务中心龙华二期_塔楼_F18_20211206.rvt")));
        System.out.println(DigestUtil.md5Hex(streamBytes));
        // 4ee40f88485bcbe9282592d6d97ce6ff
        //EFDDF79F60624752814D8F2A36CD3B9D
//        MinioClient minioClient=MinioClient.builder()
//                .endpoint("http://192.168.100.102",31335,false)
//                .credentials("persagy", "persagy@2021")
//                .region("GM+8")
//                .build();
        // Get information of an object.
//        StatObjectResponse statObject = minioClient
//                .statObject(StatObjectArgs.builder()
//                        .bucket("dmp-rwd")
//                        .object("20211209/Eq440305000274029fde4b7f459a872d5e15646c285c.png")
//                        .build());
//        StatObjectResponse statObject = minioClient
//                .statObject(StatObjectArgs.builder()
//                        .bucket("dmp-rwd")
//                        .object("20211209/Eq440305000274029fde4b7f459a872d5e15646c285c.png")
//                        .region("GM+8")
//                        .build());
//        GetObjectResponse getObjectResponse = minioClient.getObject(GetObjectArgs.builder()
//                .bucket("dmp-rwd")
//                .object("20211209/Eq440305000274029fde4b7f459a872d5e15646c285c.png")
//                .region("GM+8")
//                .build());
//        System.out.println(getObjectResponse.read());
        // System.out.println(JSONObject.toJSONString(statObject));

    }

}
