package kr.co.icontrols.ilp300_700.ftp;

import android.util.Log;

import org.apache.commons.net.ftp.FTPClient;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class HDCFTPManager {

    String TAG = "HDCFTPManager";

    public String ENCODING_TYPE_EUC_KR = "euc-kr";
    public String ENCODING_TYPE_UTF_8 = "utf-8";
    public int RECEIVE_BUFFERSIZE = 1024 * 512;

    boolean bLOG = true;   // true, false
    boolean bLOG_DEBUG = false;   // true, false
    boolean bLOG_INFO = true;   // true, false
    FTPClient mFTPClient;

    public HDCFTPManager() {
        try {
            Log.d(TAG, "[HDCFTPManager] ===== START =====");
            mFTPClient = new FTPClient();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] HDCFTPManager()");
            e.printStackTrace();
        }
    }

    private void LOG(String log) {
        if (bLOG) Log.v(TAG, log);
    }

    private void LOG_DEBUG(String log) {
        if (bLOG_DEBUG) Log.d(TAG, log);
    }

    private void LOG_INFO(String log) {
        if (bLOG_INFO) Log.i(TAG, log);
    }

    public boolean connectFTP(String TargetIP) {
        try {
            mFTPClient.connect(TargetIP);
            LOG_INFO("[connectFTP] Connected to [" + TargetIP + "]");
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] connectFTP(TargetIP)");
            e.printStackTrace();
            return false;
        }
    }

    public boolean loginFTP(String ID, String PW) {
        try {
            boolean bResult = false;
            bResult = mFTPClient.login(ID, PW);
            LOG_INFO("[loginFTP] logged FTP Server in : [" + bResult + "]");
            return bResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] loginFTP(ID, PW)");
            e.printStackTrace();
            return false;
        }
    }

    public boolean logoutFTP() {
        try {
            boolean bResult = false;
            bResult = mFTPClient.logout();
            LOG_INFO("[logoutFTP] bResult [" + bResult + "]");
            return bResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] logoutFTP()");
            e.printStackTrace();
            return false;
        }
    }

    public boolean disconnectFTP() {
        try {
            mFTPClient.disconnect();
            LOG_INFO("[disconnectFTP] Disconnected FTP");
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] disconnectFTP()");
            e.printStackTrace();
            return false;
        }
    }

    public boolean downloadFile(String remoteFilepath, String localFilePath) {
        try {
            LOG_INFO("[downloadFile] remoteFilepath [" + remoteFilepath + "], localFilePath [" + localFilePath + "]");
            boolean bResult = false;
            File mFile = new File(localFilePath);
            if (mFile.exists()) {
                LOG_INFO("[downloadFile] [" + localFilePath + "] is exist!!");
                if (mFile.delete()) {
                    LOG_INFO("[downloadFile] [" + localFilePath + "] is deleted!!");
                } else {
                    LOG_INFO("[downloadFile] Fail to delete [" + localFilePath + "]");
                    return false;
                }
            }
            FileOutputStream FileOutStm = new FileOutputStream(mFile);
            BufferedOutputStream BufferedOutStm = new BufferedOutputStream(FileOutStm);
            bResult = mFTPClient.retrieveFile(remoteFilepath, BufferedOutStm);
            BufferedOutStm.flush();
            FileOutStm.flush();
            BufferedOutStm.close();
            FileOutStm.close();
            LOG_INFO("[downloadFile] bResult [" + bResult + "]");

            return bResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] downloadFile(remoteFilepath, localFilePath)");
            e.printStackTrace();
            deleteFile(localFilePath);
            return false;
        }
    }

    public boolean uploadFile(String remoteFilepath, String localFilePath) {
        try {
            boolean bResult = false;
            File mFile = new File(localFilePath);
            if (!mFile.exists()) {
                LOG_INFO("[uploadFile] No FILE!! [" + localFilePath + "]");
                return false;
            }
            FileInputStream FileInStm = new FileInputStream(mFile);
            bResult = mFTPClient.storeFile(remoteFilepath, FileInStm);
            LOG_INFO("[uploadFile] bResult [" + bResult + "]");

            return bResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] uploadFile(remoteFilepath, localFilePath)");
            e.printStackTrace();
            return false;
        }
    }

    public boolean isFileexist(String Filepath) {
        try {
            boolean bResult = false;
            File mFile = new File(Filepath);
            bResult = mFile.exists();
            LOG_INFO("[isFileexist] bResult [" + bResult + "]");
            return bResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] isFileexist(Filepath)");
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteFile(String Filepath) {
        try {
            boolean bResult = false;
            File mFile = new File(Filepath);
            if (mFile.exists()) {
                bResult = mFile.delete();
            } else {
                LOG_INFO("[deleteFile] There is no file = " + Filepath);
            }
            LOG_INFO("[deleteFile] bResult [" + bResult + "]");
            return bResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] deleteFile(Filepath)");
            e.printStackTrace();
            return false;
        }
    }

    public boolean setReceieveDataSocketBufferSize(int BufferSize) {
        try {
            mFTPClient.setReceieveDataSocketBufferSize(BufferSize);
            LOG_INFO("[setReceieveDataSocketBufferSize] BufferSize [" + BufferSize + "]");
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setReceieveDataSocketBufferSize(BufferSize)");
            e.printStackTrace();
            return false;
        }
    }

    public boolean setReceiveBufferSize(int BufferSize) {
        try {
            mFTPClient.setReceiveBufferSize(BufferSize);
            LOG_INFO("[setReceiveBufferSize] BufferSize [" + BufferSize + "]");
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setReceiveBufferSize(BufferSize)");
            e.printStackTrace();
            return false;
        }
    }

    public boolean setCtrlEncoding(String Encode) {
        try {
            if (mFTPClient.isConnected()) {
                mFTPClient.setControlEncoding(Encode);
                LOG_INFO("[setCtrlEncoding] setControlEncoding [" + Encode + "]");
                return true;
            } else {
                LOG_INFO("[setFTPEnvironment] Not connected!!");
                return false;
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setCtrlEncoding(Encode)");
            e.printStackTrace();
            return false;
        }
    }

    public boolean setFileType(int FileType) {
        try {
            boolean bResult = false;
            bResult = mFTPClient.setFileType(FileType);
            LOG_INFO("[setFileType] bResult [" + bResult + "]");
            return bResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setFileType(FileType)");
            e.printStackTrace();
            return false;
        }
    }

    public boolean setPassiveMode() {
        try {
            mFTPClient.enterLocalPassiveMode();
            LOG_INFO("[setPassiveMode] Set LocalPassiveMode!!");
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setPassiveMode()");
            e.printStackTrace();
            return false;
        }
    }

    public boolean setActiveMode() {
        try {
            mFTPClient.enterLocalActiveMode();
            LOG_INFO("[setPassiveMode] Set LocalActiveMode!!");
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setActiveMode()");
            e.printStackTrace();
            return false;
        }
    }

    public boolean setFileTransferMode(int Mode) {
        try {
            boolean bResult = false;
            bResult = mFTPClient.setFileTransferMode(Mode);
            LOG_INFO("[setFileTransferMode] bResult [" + bResult + "]");
            return bResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setFileTransferMode(Mode)");
            e.printStackTrace();
            return false;
        }
    }

    public boolean setDefaultTimeout(int mSec) {
        try {
            mFTPClient.setDefaultTimeout(mSec);
            LOG_INFO("[setDefaultTimeout] Set Timeout [" + mSec + "]");
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setDefaultTimeout(Sec)");
            e.printStackTrace();
            return false;
        }
    }

    public boolean setConnectTimeout(int mSec) {
        try {
            mFTPClient.setConnectTimeout(mSec);
            LOG_INFO("[setConnectTimeout] Set Timeout [" + mSec + "]");
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setConnectTimeout(Sec)");
            e.printStackTrace();
            return false;
        }
    }

    public boolean setSoTimeout(int mSec) {
        try {
            mFTPClient.setSoTimeout(mSec);
            LOG_INFO("[setSoTimeout] Set Timeout [" + mSec + "]");
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setSoTimeout(Sec)");
            e.printStackTrace();
            return false;
        }
    }

    public boolean setDataTimeout(int mSec) {
        try {
            mFTPClient.setDataTimeout(mSec);
            LOG_INFO("[setDataTimeout] Set Timeout [" + mSec + "]");
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setDataTimeout(Sec)");
            e.printStackTrace();
            return false;
        }
    }


}