package com.inspect.base.core.sftp;

import java.io.IOException;
import java.io.InputStream;

import com.inspect.base.core.utils.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ftpClient {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private static SftpClient sftpClient;

    public static InputStream login(String host, int port, String username, String password, String fileName, String path) throws Exception {
        InputStream inputStream = null;
        FTPClient ftp = new FTPClient();
        ftp.setControlEncoding("UTF-8");
        ftp.connect(host, port);
        boolean success = ftp.login(username, password);
        if (success) {
            inputStream = downLoad(ftp, fileName, path);
            logout(ftp);
        }

        return inputStream;
    }

    public static void logout(FTPClient ftps) throws IOException {
        if (ftps != null && ftps.isConnected()) {
            ftps.disconnect();
        }

    }

    public static boolean existFile(FTPClient ftp, String path, String fileName) throws IOException {
        boolean flag = false;
        String[] str = path.split(StringUtils.SLASH);

        for (String s : str) {
            ftp.changeWorkingDirectory(s);
        }

        FTPFile[] files = ftp.listFiles();
        if (files != null) {
            for (FTPFile file : files) {
                if (file.getName().equals(fileName)) {
                    System.out.println("File exists." + fileName);
                    flag = true;
                    break;
                }
            }
        }

        return flag;
    }

    public static void mkdir(FTPClient ftps, String path) throws IOException {
        String[] split = path.split(StringUtils.SLASH);
        ftps.changeWorkingDirectory(StringUtils.SLASH);
        for (String str : split) {
            if (ftps.changeWorkingDirectory(str)) {
                ftps.changeWorkingDirectory(str);
            } else {
                ftps.makeDirectory(str);
                ftps.changeWorkingDirectory(str);
            }
        }

    }

    public static InputStream downLoad(FTPClient ftp, String fileName, String path) {
        InputStream inputStream = null;

        Object object;
        try {
            ftp.enterLocalPassiveMode();
            ftp.setFileType(2);
            boolean bl = existFile(ftp, path, fileName);
            if (bl) {
                inputStream = ftp.retrieveFileStream(path);
                ftp.logout();
                return inputStream;
            }

            object = null;
        } catch (Exception e) {
            e.printStackTrace();
            return inputStream;
        } finally {
            try {
                if (ftp.isConnected()) {
                    ftp.disconnect();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return (InputStream) object;
    }

    public static void main(String[] args) throws Exception {
        String host = "10.10.18.92";
        int port = 21;
        String username = "xg";
        String password = "Xg@123456";
        String fileName = "极1低,极1高,极2高,极2低巡检任务20240226150000.xls";
        String path = "//极1低,极1高,极2高,极2低巡检任务20240226150000/";
        InputStream str = login(host, port, username, password, fileName, path);
        System.out.println(str == null);
    }
}
