package easydo.technology.service;

import easydo.technology.utils.LocalDateUtil;
import io.minio.*;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.MinioException;
import io.minio.http.Method;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;

@Service
public class MinioService {

    @Resource
    private MinioClient minioClient;

    @Value("${minio.endpoint}")
    private String endpoint;

    /**
     * 检查存储桶是否存在
     */
    public boolean bucketExists(String bucketName) throws Exception {
        return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
    }

    /**
     * 创建存储桶
     */
    public void makeBucket(String bucketName) throws Exception {
        if (!bucketExists(bucketName)) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }

    /**
     * 上传文件
     */
    public String uploadFile(String bucketName, InputStream inputStream, String objectName) throws Exception {
        // 如果存储桶不存在则创建
        makeBucket(bucketName);

        String dir = LocalDateUtil.localDateTimeFormat(LocalDate.now(), "yyyy-MM-dd");
        String object = dir + "/" + objectName;
        minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(object)
                        //分块上传10m一个区块
                        .stream(inputStream, -1, 10485760)
                        .contentType("application/octet-stream")
                        .build());
        return "/" + bucketName + "/" + object;
    }

    /**
     * 上传文件
     */
    public String uploadFile(String bucketName, InputStream inputStream, String objectName, String dir) throws Exception {
        // 如果存储桶不存在则创建
        makeBucket(bucketName);

        String object = dir + "/" + objectName;
        minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(object)
                        //分块上传10m一个区块
                        .stream(inputStream, -1, 10485760)
                        .contentType("application/octet-stream")
                        .build());
        return "/" + bucketName + "/" + object;
    }


    /**
     * 获取文件URL
     */
    public String getFileUrl(String bucketName, String objectName) throws Exception {
        return minioClient.getPresignedObjectUrl(
                GetPresignedObjectUrlArgs.builder()
                        .method(Method.GET)
                        .bucket(bucketName)
                        .object(objectName)
                        .build());
    }

    /**
     * 下载文件
     */
    public InputStream downloadFile(String bucketName, String objectName) throws Exception {
        return minioClient.getObject(
                GetObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .build());
    }

    /**
     * 删除文件
     */
    public void removeFile(String bucketName, String objectName) throws Exception {
        minioClient.removeObject(
                RemoveObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .build());
    }

    public boolean isObjectExist(String bucketName, String objectName) {
        try {
            minioClient.statObject(
                    StatObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .build()
            );
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
