package com.fdkankan.fyun.oss;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.fdkankan.fyun.constant.FYunTypeEnum;
import com.fdkankan.fyun.face.AbstractFYunFileService;
import com.obs.services.ObsClient;
import com.obs.services.model.*;
import org.apache.http.HttpHeaders;
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 java.io.*;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

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

    @Autowired
    private ObsClient obsClient;

    @Override
    public String uploadFile(String bucket, byte[] data, String remoteFilePath) {
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, remoteFilePath, new ByteArrayInputStream(data));
            putObjectRequest.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
            obsClient.putObject(putObjectRequest);
        } 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 {
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, remoteFilePath, inputStream);
            putObjectRequest.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
            obsClient.putObject(putObjectRequest);
            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 (CollUtil.isNotEmpty(headers)) {
                String contentType = headers.get(HttpHeaders.CONTENT_TYPE);
                if(StrUtil.isNotEmpty(contentType)){
                    metadata.setContentType(contentType);
                }
                String contentEncoding = headers.get(HttpHeaders.CONTENT_ENCODING);
                if(StrUtil.isNotEmpty(contentEncoding)){
                    metadata.setContentEncoding(contentEncoding);
                }
            }
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, remoteFilePath, file);
            putObjectRequest.setMetadata(metadata);
            putObjectRequest.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
            obsClient.putObject(putObjectRequest);
            log.info("文件上传成功，path:{}", filePath);
        } catch (Exception e) {
            log.error("oss上传文件失败,filePath:"+filePath, e);
        }
        return null;
    }

    @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.OBS.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.OBS.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 {
            obsClient.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);
            listObjectsRequest.setPrefix(remoteFolderPath);
            listObjectsRequest.setMaxKeys(500);

            do {
                DeleteObjectsRequest request = new DeleteObjectsRequest(bucket);
                //获取下一页的起始点，它的下一项
                listObjectsRequest.setMarker(nextMaker);
                ObjectListing objectListing = obsClient.listObjects(listObjectsRequest);
                objectListing.getObjectSummaries().parallelStream().forEach(v->{
                    request.addKeyAndVersion(v.getObjectKey());
                });
                obsClient.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 = obsClient.listObjects(listObjectsRequest);
                List<String> collect = objectListing.getObjects().parallelStream()
                        .map(ObsObject::getObjectKey).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 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 -> {
                CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucketName, file, targetBucketName, file.replace(sourcePath, targetPath));
                copyObjectRequest.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
                obsClient.copyObject(copyObjectRequest);
            });
        } 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) {
        ObsObject ossObject = null;
        try {
            ossObject = obsClient.getObject(bucketName, remoteFilePath);
        }catch (Exception e){
            return null;
        }
        try (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 obsClient.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);
            obsClient.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);
        return null;
    }

    @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 = obsClient.listObjects(listObjectsRequest);
                List<String> collect = objectListing.getObjects().parallelStream()
                        .map(ObsObject::getObjectKey).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 =  obsClient.getObjectMetadata(bucket, filePath);
        return this.isRestoreCompleted(objectMetadata);
    }

    private boolean isRestoreCompleted(ObjectMetadata objectMetadata){
        String restoreStr = (String)objectMetadata.getValue("x-obs-restore");
        boolean restore = Boolean.valueOf(restoreStr);
        if(!restore){
            return true;
        }
        return false;
    }

    @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 =  obsClient.getObjectMetadata(bucket, objectName);
            // 校验Object是否为归档类型Object。
            StorageClassEnum storageClass = objectMetadata.getObjectStorageClass();
            if (storageClass == StorageClassEnum.COLD) {
                // 解冻Object。
                RestoreObjectRequest request = new RestoreObjectRequest(bucket, objectName, 1);
                obsClient.restoreObject(request);
            }
        }
    }

    @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 = obsClient.getObjectMetadata(bucket, objectName);
            if(this.isRestoreCompleted(objectMetadata)){
                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 = obsClient.getObjectMetadata(bucket, objectName);

        // 校验Object是否为归档类型Object。
        StorageClassEnum storageClass = objectMetadata.getObjectStorageClass();
        if (storageClass == StorageClassEnum.COLD) {
            // 设置解冻冷归档Object的优先级。
            // RestoreTier.RESTORE_TIER_EXPEDITED 表示1小时内完成解冻。
            // RestoreTier.RESTORE_TIER_STANDARD 表示2~5小时内完成解冻。
            // RestoreTier.RESTORE_TIER_BULK 表示5~12小时内完成解冻。
            RestoreTierEnum restoreTier = null;
            switch (priority){
                case 1 :
                    restoreTier = RestoreTierEnum.EXPEDITED;
                    break;
                case 2 :
                    restoreTier = RestoreTierEnum.STANDARD;
                    break;
                default:
                    restoreTier = RestoreTierEnum.BULK;
            }
            // 配置解冻参数，以设置5小时内解冻完成，解冻状态保持2天为例。
            // 第一个参数表示保持解冻状态的天数，默认是1天，此参数适用于解冻Archive（归档）与ColdArchive（冷归档）类型Object。
            // 第二个参数jobParameters表示解冻优先级，只适用于解冻ColdArchive类型Object。
            //开始解冻
            // 解冻Object。
            RestoreObjectRequest request = new RestoreObjectRequest(bucket, objectName, 1);
            request.setRestoreTier(restoreTier);
            obsClient.restoreObject(request);
//            // 等待解冻完成。
//            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 = obsClient.listObjects(listObjectsRequest);
//            Long space = objectListing.getObjectSummaries().parallelStream()
//                    .mapToLong(S3Object::getMetadat).sum();
            List<S3Object> objectSummaries = objectListing.getObjectSummaries();
            for (S3Object objectSummary : objectSummaries) {
                Long contentLength = objectSummary.getMetadata().getContentLength();
                total += contentLength;
            }
            nextMaker = objectListing.getNextMarker();
            //全部执行完后，为false
            flag = objectListing.isTruncated();
        } while (flag);
        return total;
    }

}
