package com.eactive.eai.batch.ftp;

import java.io.IOException;
import java.util.HashMap;

import org.apache.commons.net.ftp.FTPFile;
import com.eactive.eai.common.util.Logger;

import com.eactive.eai.batch.doc.BatchDoc;

public class FTPUtil {

	public static FTPFile[] listFiles(String server, int port, String username, String password, String path, HashMap<Integer, String> extmap, boolean isSftp, Logger logger, String authType)
			throws IOException {
		if ( isSftp ){
			return SFTPAdaptor.listFiles(server, port, username, password, path, extmap, logger, authType);
		}else{
			return FTPAdaptor.listFiles(server, port, username, password, path, extmap, logger);
		}
			
	}
	// 20241008 sftp jsch 방식 list
	public static FTPFileObject[] listFilesJSCH(String server, int port, String username, String password, String path, boolean isSftp, Logger logger, String authType)
			throws IOException {
		if ( isSftp ){
			return SFTPAdaptor.listFilesJSCH(server, port, username, password, path, logger, authType);	
		}else{
			return FTPAdaptor.listFiles2(server, port, username, password, path, logger);
		}
	}

	public static long retrieve(BatchDoc batchDoc, String server, int port, String username, String password, String remote, String remoteFileName, String localPath,  
			int connTimeout, int setTimeout, boolean isSftp, int bandWidth, Logger logger, String authType) throws Exception {

		if ( isSftp ){
			return SFTPAdaptor.retrieveJSCH(batchDoc, server, port, username, password, remote, remoteFileName, localPath, connTimeout, setTimeout, bandWidth, logger, authType);
		}else{
			return FTPAdaptor.retrieve(batchDoc,server, port, username, password, remote+remoteFileName, localPath, bandWidth, logger, "");
		}
	}
	
	public static long retrieve(BatchDoc batchDoc, String server, int port, String username, String password, String remote, String remoteFileName, String localPath,  
			int connTimeout, int setTimeout, int bandWidth, Logger logger, String authType, String fileType) throws Exception {
		if ( "0".equals(authType)) {
			return FTPAdaptor.retrieve(batchDoc,server, port, username, password, remote+remoteFileName, localPath, bandWidth, logger, fileType);
		}else {
			return SFTPAdaptor.retrieveJSCH(batchDoc, server, port, username, password, remote, remoteFileName, localPath, connTimeout, setTimeout, bandWidth, logger, authType);
		}
	}
	
	public static long retrieve(String server, int port, String username, String password, String remote, String localPath, boolean isSftp, Logger logger, String authType) throws Exception {

		if ( isSftp ){
			return SFTPAdaptor.retrieve(server, port, username, password, remote, localPath, logger, authType);
		}else{
			return FTPAdaptor.retrieve(server, port, username, password, remote, localPath, logger);
		}
	}

	public static boolean store(BatchDoc batchDoc, String server, int port, String username, String password, String remote, String remoteFileName, String localPath,  
			int connTimeout, int setTimeout, boolean isSftp, int bandWidth, Logger logger, String authType) throws Exception {

		if ( isSftp ){
			return SFTPAdaptor.storeJSCH(batchDoc, server, port, username, password, remote, remoteFileName, localPath, connTimeout, setTimeout, bandWidth, logger, authType);
		}else{
			return FTPAdaptor.store(batchDoc,server, port, username, password, remote+remoteFileName, localPath, bandWidth, logger);
		}
	}
	
	/**
	 * 
	 * @param authType 1: ID 인증, 2: 키인증
	 */
    public static boolean store(String server, int port, String username, String password, String remote, String localPath, boolean isSftp, Logger logger, String authType) throws Exception {

		if ( isSftp ){
			return SFTPAdaptor.store(server, port, username, password, remote, localPath, logger, authType);
		}else{
			return FTPAdaptor.store(server, port, username, password, remote, localPath, logger);
		}

    }

    public static boolean rename(String server, int port, String username, String password, String srcname, String tarname, boolean isSftp, Logger logger, String authType) throws Exception {
		if ( isSftp ){
			return SFTPAdaptor.rename(server, port, username, password, srcname, tarname, logger, authType);
		}else{
			return FTPAdaptor.rename(server, port, username, password, srcname, tarname, logger);
		}
    }

    /**
     * KED backup을 위해서 만듬 
     */
    public static boolean renameWithMkdirs(String server, int port, String username, String password, String srcname, String tarname, String filename, boolean isSftp, Logger logger, String authType) throws Exception {
    	return SFTPAdaptor.renameWithMkdirs(server, port, username, password, srcname, tarname, filename, logger, authType);
    }

//    public static boolean delete(String server, int port, String username, String password, String srcname, boolean isSftp) throws Exception {
//		if ( isSftp ){
//			return SFTPAdaptor.delete(server, port, username, password, srcname);
//		}else{
//			return FTPAdaptor.delete(server, port, username, password, srcname);
//		}
//    }
}
