package com.eactive.eai.rms.onl.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import com.eactive.eai.rms.onl.common.exception.BizException;

public class FtpUtil {
	private static final int FTP_TIME_OUT = 10000;
	
	private String ftpIp;
//	private String ftpPort;
	private String ftpUser;
	private String ftpPass;
	
	public FtpUtil(String ip, String user, String pass) {
		this.ftpIp = ip;
		this.ftpUser = user;
		this.ftpPass = pass;
	}
	
	public FtpUtil(String ip, String port, String user, String pass) {
		this.ftpIp = ip;
//		this.ftpPort = port;
		this.ftpUser = user;
		this.ftpPass = pass;
	}

	public static FTPClient ftpConnection(String ip, String user, String pass) throws BizException, IOException {
		return ftpConnection(ip, FTPClient.DEFAULT_PORT, user, pass);
	}
	
	public static FTPClient ftpConnection(String ip, int port, String user, String pass) throws BizException, IOException {
		FTPClient ftp = new FTPClient();
		ftp.connect(ip, port);
		ftp.setDataTimeout(FTP_TIME_OUT);
		// 정상적인지 확인
		int reply = ftp.getReplyCode();
		if (!FTPReply.isPositiveCompletion(reply)) {
			throw new BizException("FTP 비정상적으로 연결되어 종료됩니다. - reply: "+ reply);
		}
		// 로그인
		if (!ftp.login(user, pass)) {
			ftp.logout();
			throw new BizException("FTP 로그인이 실패하였습니다.");
		}
		return ftp;
	}
	
	public static void ftpClose(FTPClient client) {
		if (client != null) {
			try {
				client.logout();
			} catch (IOException e) {}
			try {
				client.disconnect();
			} catch (IOException e) {}
		}
	}
	
	public String[] ftpGetFileNameList(String serverDir) throws BizException, Exception {
		String[] ret = null;
		
		FTPClient client = null;
		try {
			client = ftpConnection(ftpIp, ftpUser, ftpPass);
			client.setFileType(FTP.BINARY_FILE_TYPE);
			client.enterLocalPassiveMode();
			if (!client.changeWorkingDirectory(serverDir)) {
				throw new BizException("폴더가 존재하지 않습니다. - "+ serverDir);
			}
			FTPFile[] fileList = client.listFiles();
			ret = Arrays.stream(fileList).filter(FTPFile::isFile).map(FTPFile::getName).toArray(String[]::new);
		} catch (BizException be) {
			throw be;
		} catch (Exception e) {
			throw e;
		} finally {
			ftpClose(client);
		}
		
		return ret;
	}
	
	public boolean ftpDeleteBinaryFile(String serverDir, String fileName) throws BizException, Exception {
		boolean ret = false;
		FTPClient client = null;
		try {
			client = ftpConnection(ftpIp, ftpUser, ftpPass);
			client.setFileType(FTP.BINARY_FILE_TYPE);
			client.enterLocalPassiveMode();
			ret = client.deleteFile(serverDir + fileName);
		} catch (BizException be) {
			throw be;
		} catch (Exception e) {
			throw e;
		} finally {
			ftpClose(client);
		}
		return ret;
	}
	
	public boolean ftpPutBinaryFile(String serverPath, String clientPath) throws BizException, Exception {
		boolean ret = false;
		
		FTPClient client = null;
		FileInputStream in = null;
		try {
			client = ftpConnection(ftpIp, ftpUser, ftpPass);
			client.setFileType(FTP.BINARY_FILE_TYPE);
			client.enterLocalPassiveMode();
			
			in = new FileInputStream(new File(clientPath));
			ret = client.storeFile(serverPath, in);
			if (ret) {
				client.sendCommand("chmod 755 "+ serverPath);
			}
		} catch (BizException be) {
			throw be;
		} catch (Exception e) {
			throw e;
		} finally {
			if (in != null) {
				in.close();
			}
			ftpClose(client);
		}
		return ret;
	}
}
