package com.fdkankan.fyun.oss;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.aliyun.oss.model.*;
import com.fdkankan.common.util.FileMd5Util;
import com.fdkankan.fyun.constant.FYunTypeEnum;
import com.fdkankan.fyun.face.AbstractFYunFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.io.*;
import java.math.BigDecimal;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

@Component
@ConditionalOnProperty(name = "fyun.type", havingValue = "oss")
public class OssFileService extends AbstractFYunFileService {

    private Logger log = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    private OSS ossClient;

    @Override
    public String uploadFile(String bucket, byte[] data, String remoteFilePath) {
        try {
            ossClient.putObject(bucket, remoteFilePath, new ByteArrayInputStream(data));
        } catch (Exception e) {
            log.error("oss上传文件失败,remoteFilePath:" + remoteFilePath, e);
        }
        return null;
    }

    @Override
    public String uploadFile(String bucket, String filePath, String remoteFilePath) {
        return uploadFile(bucket, filePath, remoteFilePath, null);
    }

    @Override
    public String uploadFile(String bucket, InputStream inputStream, String remoteFilePath) {
        try {
            ossClient.putObject(bucket, remoteFilePath, inputStream);
            log.info("文件流上传成功，目标路径：remoteFilePath:{}", remoteFilePath);
        } catch (Exception e) {
            log.error("oss上传文件失败,remoteFilePath:"+remoteFilePath, e);
        }
        return null;
    }

    @Override
    public String uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) {
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                log.warn("要上传的文件不存在,filePath" + filePath);
                return null;
            }
            ObjectMetadata metadata = new ObjectMetadata();
            if (filePath.contains(".jpg")) {
                metadata.setContentType("image/jpeg");
            }
            if (filePath.contains(".mp4")) {
                metadata.setContentType("video/mp4");
            }
            if (filePath.contains(".mp3")) {
                metadata.setContentType("audio/mp3");
            }
            if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(headers)) {
                for (Map.Entry<String, String> header : headers.entrySet()) {
                    metadata.setHeader(header.getKey(), header.getValue());
                }
            }
            PutObjectResult putObjectResult = ossClient.putObject(bucket, remoteFilePath, file, metadata);
            log.info("文件上传成功，path:{}", filePath);
        } catch (Exception e) {
            log.error("oss上传文件失败,filePath:"+filePath, e);
        }
        return null;
    }

    public static void main(String[] args) {
        File file = new File("D:\\Downloads\\SG-t-GjwsdgpLvDb\\wwwroot\\scene_view_data\\SG-t-GjwsdgpLvDb\\images\\pan\\high\\0.jpg");
        String fileMD5 = FileMd5Util.getFileMD5(file);
        System.out.println("本地文件md5：" + fileMD5);
        ObjectMetadata metadata = new ObjectMetadata();
        Map<String, String> tags = new HashMap<>();
        tags.put("tag-a", file.getName());
        metadata.setObjectTagging(tags);
        OSS ossClient = new OSSClientBuilder().build("http://oss-cn-shenzhen.aliyuncs.com", "LTAI5tJwboCj3r4vUNkSmbyX", "meDy7VYAWbg8kZCKsoUZcIYQxigWOy");
        PutObjectRequest request = new PutObjectRequest("4dkankan", "testdata/0.jpg", file, metadata);
        request.setVersionId("123444");
        PutObjectResult putObjectResult = ossClient.putObject(request);
        String requestId = putObjectResult.getRequestId();
        System.out.println("requestId:" + requestId);
        System.out.println(System.currentTimeMillis());
        boolean exist = ossClient.doesObjectExist("4dkankan", "testdata/0.jpg");
        System.out.println(System.currentTimeMillis());
        System.out.println("文件是否存在：" + exist);
        if(!exist){
            System.out.println("文件不存在");
            // TODO: 2025/3/31 失败处理
        }

        putObjectResult.getVersionId();
        System.out.println(putObjectResult.getVersionId());

//        System.out.println(requestId1.equals(requestId2));
    }

    @Override
    public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) {
        try {
            String optType = new File(filePath).isDirectory() ? "folder" : "file";
            String command = String.format(fYunConstants.UPLOAD_SH, bucket, filePath, remoteFilePath, FYunTypeEnum.OSS.code(), optType);
            log.info("开始上传文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
            callshell(command);
            log.info("上传文件完毕, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
        } catch (Exception e) {
            log.error(String.format("上传文件失败, ossPath:%s, srcPath:%s", remoteFilePath, filePath), e);
        }
        return null;
    }

    @Override
    public void downloadFileByCommand(String bucket, String filePath, String remoteFilePath) {
        try {
            String optType = remoteFilePath.contains(".") ? "file" : "folder";
            String command = String.format(fYunConstants.DOWNLOAD_SH, bucket, remoteFilePath, filePath, FYunTypeEnum.OSS.code(), optType);
            log.info("开始下载文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
            callshell(command);
            log.info("下载文件完毕, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
        } catch (Exception e) {
            log.error(String.format("下载文件失败, ossPath:%s, srcPath:%s", remoteFilePath, filePath), e);
        }
    }

    @Override
    public void downloadByCommand(String bucket,  String filePath, String remoteFilePath, boolean isDir) {
        try {
            String optType = isDir ? "folder" : "file";
            String command = String.format(fYunConstants.DOWNLOAD_SH, bucket, remoteFilePath, filePath, FYunTypeEnum.OSS.code(), optType);
            log.info("开始下载文件, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
            callshell(command);
            log.info("下载文件完毕, ossPath:{}, srcPath:{}", remoteFilePath, filePath);
        } catch (Exception e) {
            log.error(String.format("下载文件失败, ossPath:%s, srcPath:%s", remoteFilePath, filePath), e);
        }
    }

    @Override
    public void deleteFile(String bucket, String remoteFilePath) throws IOException {
        try {
            ossClient.deleteObject(bucket, remoteFilePath);
        } catch (Exception e) {
            log.error("OSS删除文件失败,key:" + remoteFilePath, e);
        }
    }

    @Override
    public void deleteFolder(String bucket, String remoteFolderPath) {
        try {

            if (!remoteFolderPath.endsWith("/")) {
                remoteFolderPath = remoteFolderPath + "/";
            }
            log.info("开始删除文件夹：{}", remoteFolderPath);
            boolean flag = true;
            String nextMaker = null;
            ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket).withPrefix(remoteFolderPath).withMaxKeys(1000);

            DeleteObjectsRequest request = new DeleteObjectsRequest(bucket);
            do {
                //获取下一页的起始点，它的下一项
                listObjectsRequest.setMarker(nextMaker);
                ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
                List<String> keys = objectListing.getObjectSummaries().parallelStream()
                        .map(OSSObjectSummary::getKey).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(keys)) {
                    request.setKeys(keys);
                    ossClient.deleteObjects(request);
                }
                nextMaker = objectListing.getNextMarker();
                //全部执行完后，为false
                flag = objectListing.isTruncated();
            } while (flag);
        } catch (Exception e) {
            log.error("OSS删除文件失败,key:" + remoteFolderPath, e);
        }
    }

    @Override
    public void uploadMulFiles(String bucket, Map<String, String> filepaths) {
        try {
            for (Map.Entry<String, String> entry : filepaths.entrySet()) {
                uploadFile(bucket, entry.getKey(), entry.getValue(), null);
            }
        } catch (Exception e) {
            log.error("OSS批量上传文件失败！");
        }
    }

    @Override
    public List<String> listRemoteFiles(String bucket, String sourcePath) {
        List<String> keyList = new ArrayList<>();
        try {
            boolean flag = true;
            String nextMaker = null;
            ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket);
            //指定下一级文件
            listObjectsRequest.setPrefix(sourcePath);
            //设置分页的页容量
            listObjectsRequest.setMaxKeys(200);
            do {
                //获取下一页的起始点，它的下一项
                listObjectsRequest.setMarker(nextMaker);
                ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
                List<String> collect = objectListing.getObjectSummaries().parallelStream()
                        .map(OSSObjectSummary::getKey).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(collect)) {
                    keyList.addAll(collect);
                }
                nextMaker = objectListing.getNextMarker();
                //全部执行完后，为false
                flag = objectListing.isTruncated();
            } while (flag);
        } catch (Exception e) {
            log.error("获取文件列表失败，path:" + sourcePath, e);
        }
        return keyList;
    }

    @Override
    public void copyFileBetweenBucketParallel(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
        try {
            List<String> files = listRemoteFiles(sourceBucketName, sourcePath);
            if (ObjectUtils.isEmpty(files)) {
                return;
            }
            files.parallelStream().forEach(file -> {
                ossClient.copyObject(sourceBucketName, file, targetBucketName, file.replace(sourcePath, targetPath));
            });
        } catch (Exception e) {
            log.error("列举文件目录失败,key:" + sourcePath, e);
        }
    }

    @Override
    public void copyFileBetweenBucket(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
        try {
            List<String> files = listRemoteFiles(sourceBucketName, sourcePath);
            if (ObjectUtils.isEmpty(files)) {
                return;
            }
            files.stream().forEach(file -> {
                ossClient.copyObject(sourceBucketName, file, targetBucketName, file.replace(sourcePath, targetPath));
            });
        } catch (Exception e) {
            log.error("列举文件目录失败,key:" + sourcePath, e);
        }
    }

    @Override
    public void copyFilesBetweenBucket(String sourceBucketName, String targetBucketName, Map<String, String> pathMap) {
        if (ObjectUtils.isEmpty(pathMap)) {
            return;
        }
        try {
            for (Map.Entry<String, String> entry : pathMap.entrySet()) {
                copyFileBetweenBucket(sourceBucketName, entry.getKey(), targetBucketName, entry.getValue());
            }
        } catch (Exception e) {
            log.error(String.format("批量复制文件失败, sourceBucketName:%s, targetBucketName:%s", sourceBucketName, targetBucketName), e);
        }
    }

    @Override
    public String getFileContent(String bucketName, String remoteFilePath) {
        try (OSSObject ossObject = ossClient.getObject(bucketName, remoteFilePath)){
            InputStream objectContent = ossObject.getObjectContent();
            StringBuilder contentJson = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(objectContent))) {
                while (true) {
                    String line = reader.readLine();
                    if (line == null) break;
                    contentJson.append(line);
                }
            } catch (IOException e) {
                throw e;
            }
            return contentJson.toString();
        } catch (Exception e) {
            log.error("获取文件内容失败：key:"+remoteFilePath, e);
        }
        return null;
    }

    @Override
    public boolean fileExist(String bucket, String objectName) {
        try {
            return ossClient.doesObjectExist(bucket, objectName);
        } catch (Exception e) {
            log.error("判断文件是否存在失败,key:"+objectName, e);
        }
        return false;
    }

    @Override
    public void downloadFile(String bucket, String remoteFilePath, String localPath) {
        try {
            File localFile = new File(localPath);
            if (!localFile.getParentFile().exists()) {
                localFile.getParentFile().mkdirs();
            }
            if(localFile.isDirectory()){
                String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/")+1);
                log.info("未配置文件名，使用默认文件名：{}",fileName);
                localPath = localPath.concat(File.separator).concat(fileName);
            }
            DownloadFileRequest request = new DownloadFileRequest(bucket, remoteFilePath);
            request.setDownloadFile(localPath);
            // 默认5个任务并发下载
            request.setTaskNum(5);
            // 启动断点续传
            request.setEnableCheckpoint(true);
            ossClient.downloadFile(request);
        } catch (Throwable throwable) {
            log.error("文件下载失败,key:"+remoteFilePath, throwable);
        }
    }

    @Override
    public URL getPresignedUrl(String bucket, String url) {
        java.util.Date expiration = new java.util.Date();
        long expTimeMillis = expiration.getTime();
        expTimeMillis += 1000 * 60 * 60 * 8;
        expiration.setTime(expTimeMillis);
        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket, url);
        generatePresignedUrlRequest.setMethod(HttpMethod.PUT);
        generatePresignedUrlRequest.setExpiration(expiration);
        return ossClient.generatePresignedUrl(generatePresignedUrlRequest);
    }

    @Override
    public long getSubFileNums(String bucket, String url) {
        long totalSubFileNum = 0;
        try {
            boolean flag = true;
            String nextMaker = null;
            ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket);
            //指定下一级文件
            listObjectsRequest.setPrefix(url);
            //设置分页的页容量
            listObjectsRequest.setMaxKeys(200);
            do {
                //获取下一页的起始点，它的下一项
                listObjectsRequest.setMarker(nextMaker);
                ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
                List<String> collect = objectListing.getObjectSummaries().parallelStream()
                        .map(OSSObjectSummary::getKey).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(collect)) {
                    totalSubFileNum = totalSubFileNum + collect.size();
                }
                nextMaker = objectListing.getNextMarker();
                //全部执行完后，为false
                flag = objectListing.isTruncated();
            } while (flag);
        } catch (Exception e) {
            log.error("获取文件数量失败，path:" + url, e);
        }
        return totalSubFileNum;
    }

    @Override
    public Boolean checkStore(String bucket,String filePath){
        ObjectMetadata objectMetadata =  ossClient.getObjectMetadata(bucket, filePath);
        return !objectMetadata.isRestoreCompleted();
    }

    @Override
    public void restoreFolder(String bucket,String folderName){
        ObjectMetadata objectMetadata ;

        List<String> objectList = this.listRemoteFiles(bucket, folderName);
        if(CollUtil.isEmpty(objectList)){
            return;
        }
        for (String objectName : objectList) {
            objectMetadata =  ossClient.getObjectMetadata(bucket, objectName);
            // 校验Object是否为归档类型Object。
            StorageClass storageClass = objectMetadata.getObjectStorageClass();
            if (storageClass == StorageClass.ColdArchive) {
                // 解冻Object。
                ossClient.restoreObject(bucket, objectName);
            }
        }
    }

    @Override
    public Integer getRestoreFolderProcess(String bucket,String folderName){
        ObjectMetadata objectMetadata ;

        List<String> objectList = this.listRemoteFiles(bucket, folderName);
        if(CollUtil.isEmpty(objectList)){
            return 100;
        }
        List<String> restoreFileList = new ArrayList<>();
        for (String objectName : objectList) {
            objectMetadata = ossClient.getObjectMetadata(bucket, objectName);
            if(objectMetadata.isRestoreCompleted()){
                restoreFileList.add(objectName);
            }
        }
        if(objectList.size() <= restoreFileList.size() ){
            return 100;
        }
        BigDecimal rite = new BigDecimal(restoreFileList.size()).divide(new BigDecimal(objectList.size()),2,BigDecimal.ROUND_HALF_UP);
        BigDecimal multiply = rite.multiply(new BigDecimal(100));
        return multiply.intValue();
    }

    @Override
    public void restoreFolder(String bucket, String folderName, Integer priority) {

        List<String> objectList = this.listRemoteFiles(bucket, folderName);
        if(CollUtil.isEmpty(objectList)){
            return;
        }
        objectList.parallelStream().forEach(objectName -> {
            this.restoreFile(bucket, objectName, priority);
        });
    }

    @Override
    public void restoreFile(String bucket, String objectName, Integer priority){
        ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucket, objectName);

        // 校验Object是否为归档类型Object。
        StorageClass storageClass = objectMetadata.getObjectStorageClass();
        if (storageClass == StorageClass.ColdArchive) {
            // 设置解冻冷归档Object的优先级。
            // RestoreTier.RESTORE_TIER_EXPEDITED 表示1小时内完成解冻。
            // RestoreTier.RESTORE_TIER_STANDARD 表示2~5小时内完成解冻。
            // RestoreTier.RESTORE_TIER_BULK 表示5~12小时内完成解冻。
            RestoreTier restoreTier = null;
            switch (priority){
                case 1 :
                    restoreTier = RestoreTier.RESTORE_TIER_EXPEDITED;
                    break;
                case 2 :
                    restoreTier = RestoreTier.RESTORE_TIER_STANDARD;
                    break;
                default:
                    restoreTier = RestoreTier.RESTORE_TIER_BULK;
            }
            RestoreJobParameters jobParameters = new RestoreJobParameters(restoreTier);
            // 配置解冻参数，以设置5小时内解冻完成，解冻状态保持2天为例。
            // 第一个参数表示保持解冻状态的天数，默认是1天，此参数适用于解冻Archive（归档）与ColdArchive（冷归档）类型Object。
            // 第二个参数jobParameters表示解冻优先级，只适用于解冻ColdArchive类型Object。
            RestoreConfiguration configuration = new RestoreConfiguration(1, jobParameters);
            //开始解冻
            ossClient.restoreObject(bucket, objectName, configuration);
//            // 等待解冻完成。
//            do {
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                objectMetadata = ossClient.getObjectMetadata(bucket, objectName);
//            } while (!objectMetadata.isRestoreCompleted());
        }
    }

    @Override
    public Long getSpace(String bucket, String key) {

        Long total = 0L;
        boolean flag = true;
        String nextMaker = null;
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket);
        //指定下一级文件
        listObjectsRequest.setPrefix(key);
        //设置分页的页容量
        listObjectsRequest.setMaxKeys(200);
        do {
            //获取下一页的起始点，它的下一项
            listObjectsRequest.setMarker(nextMaker);
            ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
            Long space = objectListing.getObjectSummaries().parallelStream()
                    .mapToLong(OSSObjectSummary::getSize).sum();
            total += space;
            nextMaker = objectListing.getNextMarker();
            //全部执行完后，为false
            flag = objectListing.isTruncated();
        } while (flag);
        return total;
    }

}
