package com.yutou.nas.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.StorageClass;
import com.yutou.nas.interfaces.DownloadInterface;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;

public class OSSManager {
    public static final String TYPE_MUSIC = "oss-name-music";
    public static final String TYPE_PHOTO = "oss-name-photo";
    public static final String TYPE_DEPOT = "oss-name-depot";

    private static OSS getOssClient() {
        return new OSSClientBuilder().build(ConfigTools.load(ConfigTools.CONFIG, "oss-url", String.class),
                ConfigTools.load(ConfigTools.CONFIG, "oss-id", String.class),
                ConfigTools.load(ConfigTools.CONFIG, "oss-key", String.class));
    }

    private static void closeClient(OSS oss) {
        oss.shutdown();
    }


    public static void upload(String type, String path, File... files) {
        boolean music=false,photo=false,depot=false;
        if (type.equals(TYPE_MUSIC) && !ConfigTools.load(ConfigTools.CONFIG, "oss-upload-music", boolean.class, false)) {
            music=true;
        }
        if (type.equals(TYPE_PHOTO) && !ConfigTools.load(ConfigTools.CONFIG, "oss-upload-photo", boolean.class, false)) {
            photo=true;
        }
        if (type.equals(TYPE_DEPOT) && !ConfigTools.load(ConfigTools.CONFIG, "oss-upload-depot", boolean.class, false)) {
            depot=true;
        }
        if(music||photo||depot){
            System.out.println("music = " + music);
            System.out.println("photo = " + photo);
            System.out.println("depot = " + depot);
            System.out.println("type = " + type + ", path = " + path + ", files = " + Arrays.deepToString(files));
            System.out.println("------------------------------");
            return;
        }
        new Thread(() -> {
            OSS client = getOssClient();
            for (File file : files) {
                if (file.isDirectory()) {
                    continue;
                }
                String uploadPath = file.getAbsolutePath().replace(path, "").replace(File.separator, "/");
                if (uploadPath.startsWith("/")) {
                    uploadPath = uploadPath.substring(1);
                }
                if(isExist(ConfigTools.load(ConfigTools.CONFIG,type,String.class),uploadPath)){
                    System.out.println("文件已存在:"+file.getName());
                    return;
                }

                //client.putObject(ConfigTools.load(ConfigTools.CONFIG,type,String.class), uploadPath, file);
                int limitSpeed = 100 * 1024 * 8;
                PutObjectRequest request=new PutObjectRequest(ConfigTools.load(ConfigTools.CONFIG,type,String.class),uploadPath,file);
                request.setTrafficLimit(limitSpeed);
                client.putObject(request);
                System.out.println(file.getName()+"已上传");
            }
            closeClient(client);
        }).start();

    }

    public static void download(String type, String path, DownloadInterface downloadInterface) {
        new Thread(() -> {
            OSS oss = getOssClient();
            ObjectMetadata metadata = oss.getObjectMetadata(type, path);
            if (metadata.getObjectStorageClass() == StorageClass.Archive) {
                oss.restoreObject(type, path);
                do {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    metadata = oss.getObjectMetadata(type, path);
                } while (!metadata.isRestoreCompleted());
            }
            OSSObject obj = oss.getObject(type, path);
            File file = StreamTools.streamSave(obj.getObjectContent());
            if (file != null) {
                downloadInterface.onDownload(file);
            } else {
                downloadInterface.onError(new FileNotFoundException("没有该文件"));
            }
            closeClient(oss);
        }).start();
    }
    private static boolean isExist(String type,String path){
        OSS oss=getOssClient();
        boolean flag=oss.doesObjectExist(type,path,true);
        closeClient(oss);
        return flag;
    }
}
