package kr.co.icontrols.ilp300_700.global;

import android.content.Context;
import android.util.Log;

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

import kr.co.icontrols.ilp300_700.ftp.HDCFTPManager;
import kr.co.icontrols.ilp300_700.global.Configure.ConfigValue;
import kr.co.icontrols.ilp300_700.global.Configure.SetValue;
import kr.co.icontrols.ilp300_700.global.DataClasses.FTPUploadInfo;
import kr.co.icontrols.ilp300_700.imap.Notices;

public class FTPUpload {

    static String TAG = "FTPUpload";

    public static FTPUploadInfo mFTPUploadInfo;

    static boolean bLOG = true;   // true, false
    static boolean bLOG_DEBUG = false;   // true, false
    static boolean bLOG_INFO = true;   // true, false
    static Context mContext;
    static HDCFTPManager mFTPManager;

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

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

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

    public static boolean disconnectFTP() {
        try {
            if (mFTPManager.logoutFTP()) {
                if (mFTPManager.disconnectFTP()) {
                    LOG_INFO("[disconnectFTP] disconnectFTP OK!!");
                    return true;
                }
            }
            LOG_INFO("[disconnectFTP] disconnectFTP FAIL!!");
            return false;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] disconnectFTP()");
            e.printStackTrace();
            return false;
        }
    }

    public static class ProcFTPUploadThread extends Thread {

        public ProcFTPUploadThread(Context context, FTPUploadInfo Info) {
            try {
                LOG_INFO("[ProcFTPUploadThread] =====START =====");
                mContext = context;
                mFTPUploadInfo = Info;
                mFTPUploadInfo.printValue();
            } catch (Exception e) {
                Log.e(TAG, "[Exception] ProcFTPUploadThread(Context context, FTPUploadInfo Info)");
                e.printStackTrace();
            }
        }

        public void run() {
            try {

                mFTPManager = new HDCFTPManager();
                if (!mFTPManager.connectFTP(mFTPUploadInfo.strTargetIP)) {
                    Log.w(TAG, "[ProcFTPUploadThread.run()] FAIL!! -> connectFTP(" + mFTPUploadInfo.strTargetIP + ")");
                    Notices.send_FTPUploadResult("fail_connect", mFTPUploadInfo.strFilePath, mFTPUploadInfo.strUploadPath, mFTPUploadInfo.strMessage, mFTPUploadInfo.strTargetIP);
                    Global.cancelFileUpload();
                    return;
                }

                if (!mFTPManager.loginFTP(mFTPUploadInfo.strUserID, mFTPUploadInfo.strPassword)) {
                    Log.w(TAG, "[ProcFTPUploadThread.run()] FAIL!! -> loginFTP(" + mFTPUploadInfo.strUserID + "," + mFTPUploadInfo.strPassword + ")");
                    Notices.send_FTPUploadResult("fail_login", mFTPUploadInfo.strFilePath, mFTPUploadInfo.strUploadPath, mFTPUploadInfo.strMessage, mFTPUploadInfo.strTargetIP);
                    Global.cancelFileUpload();
                    return;
                }

                if (ConfigValue.eMainServerType == SetValue.Server_Separated) {
                    if (!mFTPManager.setPassiveMode()) {
                        Log.w(TAG, "[ProcFTPUploadThread.run()] FAIL!! -> setPassiveMode()");
                        disconnectFTP();
                        Notices.send_FTPUploadResult("fail_passivemode", mFTPUploadInfo.strFilePath, mFTPUploadInfo.strUploadPath, mFTPUploadInfo.strMessage, mFTPUploadInfo.strTargetIP);
                        Global.cancelFileUpload();
                        return;
                    }
                }

                if (!mFTPManager.setCtrlEncoding(mFTPManager.ENCODING_TYPE_EUC_KR)) {
                    Log.w(TAG, "[ProcFTPUploadThread.run()] FAIL!! -> setCtrlEncoding(" + mFTPManager.ENCODING_TYPE_EUC_KR + ")");
                    disconnectFTP();
                    Notices.send_FTPUploadResult("fail_ctrlencoding", mFTPUploadInfo.strFilePath, mFTPUploadInfo.strUploadPath, mFTPUploadInfo.strMessage, mFTPUploadInfo.strTargetIP);
                    Global.cancelFileUpload();
                    return;
                }

                if (!mFTPManager.setFileType(FTP.BINARY_FILE_TYPE)) {
                    Log.w(TAG, "[ProcFTPUploadThread.run()] FAIL!! -> setFileType(" + FTP.BINARY_FILE_TYPE + ")");
                    disconnectFTP();
                    Notices.send_FTPUploadResult("fail_setfiletype", mFTPUploadInfo.strFilePath, mFTPUploadInfo.strUploadPath, mFTPUploadInfo.strMessage, mFTPUploadInfo.strTargetIP);
                    Global.cancelFileUpload();
                    return;
                }

                if (!mFTPManager.uploadFile(mFTPUploadInfo.strUploadPath, mFTPUploadInfo.strFilePath)) {
                    Log.w(TAG, "[ProcFTPUploadThread.run()] FAIL!! -> uploadFile(" + mFTPUploadInfo.strUploadPath + "," + mFTPUploadInfo.strFilePath + ")");
                    disconnectFTP();
                    Notices.send_FTPUploadResult("fail_uploadfile", mFTPUploadInfo.strFilePath, mFTPUploadInfo.strUploadPath, mFTPUploadInfo.strMessage, mFTPUploadInfo.strTargetIP);
                    Global.cancelFileUpload();
                    return;
                }

                Thread.sleep(500);
                disconnectFTP();
                LOG_INFO("[ProcFTPUploadThread.run()] Complete upload!!!");
                Notices.send_FTPUploadResult("complete", mFTPUploadInfo.strFilePath, mFTPUploadInfo.strUploadPath, mFTPUploadInfo.strMessage, mFTPUploadInfo.strTargetIP);
                Global.cancelFileUpload();
            } catch (Exception e) {
                Log.e(TAG, "[Exception] ProcFTPUploadThread.run()");
                e.printStackTrace();
                Global.cancelFileUpload();
            }
        }
    }


}