package io.renren.modules.kubesphere.service;

import io.minio.errors.*;
import io.renren.common.utils.FTPUtils;
import io.renren.common.utils.JavaShellUtil;
import io.renren.modules.minio.pojo.MinioFile;
import io.renren.modules.minio.utils.MinIoUtils;
import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @Description
 * @Author chenrj.xdu@gmail.com
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2021/10/11
 */
@Service
public class KubeSphereAlgorithmService {

    /**
     * 算法获取
     *
     * @return
     */
    public String algorithmDownload(String algorithmName) {
        String bucketName = "algmodel";
//        String destination = "/home/chenrj/gitrep";
        String destination = "/home/mkcloud/algorithm";
        File folder = null;
        try {
            List<MinioFile> fileList = MinIoUtils.listFiles(bucketName, algorithmName);
            folder = new File(destination + "/" + algorithmName);
            if (!folder.exists() && !folder.isDirectory()) {
                folder.mkdirs();
                System.out.println("创建文件夹");
            } else {
                System.out.println("文件夹已存在");
            }
            for (MinioFile file : fileList) {
                String fileName = file.getFilename();
                System.out.println(fileName);
                File localFile = new File(folder.getAbsolutePath() + "/" + fileName);
                if (!localFile.exists()) {
                    try {
                        localFile.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println("文件已创建");
                } else {
                    System.out.println("文件已存在");
                }
                //从minio中获得目标文件的输入流
                InputStream inputStream = MinIoUtils.getFileInputStream(bucketName, algorithmName + "/" + fileName);
                MinIoUtils.writeToLocal(folder.getAbsolutePath() + "/" + fileName, inputStream);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (InvalidBucketNameException e) {
            e.printStackTrace();
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        }
        return folder.getAbsolutePath();
    }

    /**
     * 算法获取 直接到服务器
     * @param algorithmName
     * @return
     */
    public void algorithmDownloadToServer(String algorithmName) {
        String bucketName = "algmodel";
//        String destination = "/home/chenrj/gitrep";
        String destination = "/home/mkcloud/algorithm";
        File folder = null;
        try {
            List<MinioFile> fileList = MinIoUtils.listFiles(bucketName, algorithmName);
            FTPUtils.mkdir(algorithmName);
            for (MinioFile file : fileList) {
                String fileName = file.getFilename();
                System.out.println(fileName);

                //从minio中获得目标文件的输入流
                InputStream inputStream = MinIoUtils.getFileInputStream(bucketName, algorithmName + "/" + fileName);
                FTPUtils.uploadFile(algorithmName,fileName,inputStream);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (InvalidBucketNameException e) {
            e.printStackTrace();
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        }

    }

    /**
     * 算法打包
     *
     * @param command
     * @return
     */
    public File algorithmPack(String command, String algorithmPath, String algorithmName) {
        System.out.println("开始打包~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        String res = JavaShellUtil.execute(command);
        if (res == null) return null;
        String formatAlgorithmName = algorithmName.toLowerCase();
        File file = new File(algorithmPath + "/dist/" + formatAlgorithmName);
        return file;
    }




    /**
     * File 转换 MultipartFile
     *
     * @param packFile
     * @return
     */
    public MultipartFile fileTransMultipartFile(File packFile) {
        File file = packFile;
        FileInputStream fileInputStream = null;
        MultipartFile multipartFile = null;
        try {
            fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            multipartFile = new MockMultipartFile(file.getName(), file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return multipartFile;
    }

}
