package com.fdkankan.fyun.face;

import com.fdkankan.fyun.config.FYunFileConfig;
import com.fdkankan.fyun.constant.FYunConstants;
import com.fdkankan.fyun.model.StreamGobbler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.Map;

@Component
public abstract class AbstractFYunFileService implements FYunFileServiceInterface {

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

    @Autowired
    public FYunFileConfig fYunFileConfig;
    @Autowired
    public FYunConstants fYunConstants;

    public String getFyunType(){
        return fYunFileConfig.getFyunType();
    }

    @Override
    public String uploadFile(byte[] data, String remoteFilePath) {
        uploadFile(fYunFileConfig.getBucket(), data, remoteFilePath);
        return fYunFileConfig.getHost().concat(remoteFilePath);
    }

    @Override
    public String uploadFile(String filePath, String remoteFilePath) {
        uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath);
        return fYunFileConfig.getHost().concat(remoteFilePath);
    }

    @Override
    public String uploadFile(String filePath, String remoteFilePath, Map<String, String> headers) {
        uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath, headers);
        return fYunFileConfig.getHost().concat(remoteFilePath);
    }

    @Override
    public String uploadFile(InputStream inputStream, String remoteFilePath) {
        uploadFile(fYunFileConfig.getBucket(), inputStream, remoteFilePath);
        return fYunFileConfig.getHost().concat(remoteFilePath);
    }

    @Override
    public String uploadFileByCommand(String filePath, String remoteFilePath) {
        uploadFileByCommand(fYunFileConfig.getBucket(), filePath, remoteFilePath);
        return fYunFileConfig.getHost().concat(remoteFilePath);
    }

    @Override
    public void downloadFileByCommand(String filePath, String remoteFilePath) {
        downloadFileByCommand(fYunFileConfig.getBucket(),filePath,remoteFilePath);
    }

    @Override
    public void deleteFile(String remoteFilePath) throws IOException {
        deleteFile(fYunFileConfig.getBucket(), remoteFilePath);
    }

    @Override
    public void deleteFolder(String remoteFolderPath) {
        deleteFolder(fYunFileConfig.getBucket(), remoteFolderPath);
    }

    @Override
    public void uploadMulFiles(Map<String, String> filepaths) {
        uploadMulFiles(fYunFileConfig.getBucket(), filepaths);
    }

    @Override
    public List<String> listRemoteFiles(String sourcePath) {
        return listRemoteFiles(fYunFileConfig.getBucket(), sourcePath);
    }

    public void copyFileInBucket(String sourcePath, String targetPath) {
        copyFileBetweenBucket(fYunFileConfig.getBucket(), sourcePath, fYunFileConfig.getBucket(), targetPath);
    }

    @Override
    public void copyFileToArchive(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
        copyFileToArchive(sourceBucketName, sourcePath, targetBucketName, targetPath);
    }

    public void copyFileInBucket(String bucket, String sourcePath, String targetPath) {
        copyFileBetweenBucket(bucket, sourcePath, bucket, targetPath);
    }

    @Override
    public void copyFileBetweenBucket(String sourcePath, String targetBucketName, String targetPath) {
        copyFileBetweenBucket(fYunFileConfig.getBucket(), sourcePath, targetBucketName, targetPath);
    }

    @Override
    public void copyFilesBetweenBucket(String targetBucketName, Map<String, String> pathMap) {
        copyFilesBetweenBucket(fYunFileConfig.getBucket(), targetBucketName, pathMap);
    }

    @Override
    public String getFileContent(String remoteFilePath) {
        return getFileContent(fYunFileConfig.getBucket(), remoteFilePath);
    }

    @Override
    public boolean fileExist(String objectName) {
        return fileExist(fYunFileConfig.getBucket(), objectName);
    }

    @Override
    public void downloadFile(String remoteFilePath, String localPath) {
        downloadFile(fYunFileConfig.getBucket(), remoteFilePath, localPath);
    }

    public void callshell(String command){
        try {
            Long start = System.currentTimeMillis();
            Process process = Runtime.getRuntime().exec(command);
            StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");
            errorGobbler.start();
            StreamGobbler outGobbler = new StreamGobbler(process.getInputStream(), "STDOUT");
            outGobbler.start();
            process.waitFor();
            log.info("脚本{}执行完毕,用时：{}ms",command,System.currentTimeMillis()-start);
        } catch (Exception e) {
            log.error("调用脚本文件上传下载失败, command : " + command, e);
        }
    }

    @Override
    public URL getPresignedUrl(String url) {
        return getPresignedUrl(fYunFileConfig.getBucket(), url);
    }


    @Override
    public long getSubFileNums(String url) {
        return getSubFileNums(fYunFileConfig.getBucket(), url);
    }

}
