package com.nokia.common.jsch;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.ChannelSftp.LsEntry;

public class SftpUtil {

    private JSch jSch = new JSch();
    private Session session = null;
    private ChannelSftp channelSftp = null;

    private String host;
    private int port = 22;
    private String user;
    private String password;

    /**
     * sftp工具，从sftp服务器下载文件到本地
     * 
     * @param src 源路径，必须是一个文件路径
     * @param dst 目的路径，也应该是一个文件路径
     * @throws IOException
     * @throws JSchException
     * @throws SftpException
     */
    public void get(String src, String dst) throws IOException, JSchException, SftpException {
        try (OutputStream outputStream = Files.newOutputStream(Paths.get(dst));) {
            channelSftp.get(src, dst);
        }
    }

    /**
     * 获取sftp指定目录下文件名列表
     * 
     * @param path
     * @return
     * @throws SftpException
     */
    @SuppressWarnings("rawtypes")
    public List<String> ls(String path) throws SftpException {
        List<String> files = new ArrayList<>();
        Vector ls = channelSftp.ls(path);
        for (Object object : ls) {
            String fileName = ((LsEntry) object).getFilename();
            if (".".equals(fileName) || "..".equals(fileName)) {
                continue;
            }
            files.add(fileName);
        }
        return files;
    }

    /**
     * 建立连接通道
     * 
     * @throws JSchException
     */
    public void connect() throws JSchException {
        // 仅在第一次使用时初始化
        if (channelSftp == null) {
            sessionConnect();
            channelSftp = (ChannelSftp) session.openChannel("sftp");
        }
        if (!channelSftp.isConnected()) {
            channelSftp.connect();
        }
    }

    /**
     * 断开连接
     */
    public void disconnect() {
        if (channelSftp != null) {
            channelSftp.disconnect();
        }
        if (session != null) {
            session.disconnect();
        }
        channelSftp = null;
        session = null;
        jSch = null;
    }

    /**
     * 建立会话
     * 
     * @throws JSchException
     */
    private void sessionConnect() throws JSchException {
        // 仅在第一次使用时初始化
        if (session == null) {
            this.session = jSch.getSession(user, host, port);
            this.session.setPassword(password);
            this.session.setUserInfo(new UserInfoImpl());
            this.session.setConfig("StrictHostKeyChecking", "no");
        }
        if (!session.isConnected()) {
            session.connect();
        }
    }

    public SftpUtil setHost(String host) {
        this.host = host;
        return this;
    }

    public SftpUtil setPort(int port) {
        this.port = port;
        return this;
    }

    public SftpUtil setUser(String user) {
        this.user = user;
        return this;
    }

    public SftpUtil setPassword(String password) {
        this.password = password;
        return this;
    }
}
