package kr.co.icontrols.ilp300_700.ftp;

import android.util.Log;

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

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

public class ftps_FileDownload extends Thread {

    String TAG = "ftps_FileDownload";

    String m_strIP="", m_strID="", m_strPWD="", m_strPATH="", m_strFileName="", m_strName="";
    public int m_iDownRet = 0;
    public ftps_FileDownload(String strIP, String strID, String strPWD, String strPATH, String strFileName, String strName) {
        m_strIP = strIP;
        m_strID = strID;
        m_strPWD = strPWD;
        m_strPATH = strPATH;
        m_strFileName = strFileName;
        m_strName = strName;
        m_iDownRet = 0;
    }

    public void run() {
        HDCSFTPManager f = new HDCSFTPManager();
        Log.d(TAG, "업데이트 파일 다운로드 시작");
        //f = new HDCSFTPManager();
        //f.setControlEncoding("euc-kr"); //한글이름의 파일은 FTP 다운로드가 안되기 때문에 인코딩 추가로 넣어줌
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        f.setCtrlEncoding("UTF-8"); //한글이름의 파일은 FTP 다운로드가 안되기 때문에 인코딩 추가로 넣어줌
        Log.d(TAG, "[ftp_FileDownload.run()] setCtrlEncodingt! ]");
        f.setReceieveDataSocketBufferSize(1024*512);
        try {
            f.setReceiveBufferSize(1024*512);
        }
        catch (RuntimeException re)
        {

            return;
        }
        catch (Exception e1) {

            return;
        }

        int recebf = f.getReceiveDataSocketBufferSize();
        Log.d(TAG, "[ftp_FileDownload.run()] getReceiveDataSocketBufferSize! ]");
        FileOutputStream fos = null;
        BufferedOutputStream ffos = null;
        try {

            boolean ret = false;
            //ret = f.connectFTP("10.0.1.1");
            ret = f.connectFTP(m_strIP);
            if (ret == false) {
                m_iDownRet = -1;
                return;
            }
            //f.setCtrlEncoding("euc-kr"); //한글이름의 파일은 FTP 다운로드가 안되기 때문에 인코딩 추가로 넣어줌
            Log.d(TAG, "[ftp_FileDownload.run()] connectFTP! ]");

            Log.d(TAG, "[ftp_FileDownload.run()] Login Try ]");
            for(int i =0; i< 1; i++)
                ret = f.loginFTP(m_strID, m_strPWD);
                //ret = f.loginFTP("valley", "valley123");

            if (ret == false) {
                m_iDownRet = -1;
                return;
            }

            if (true) {
                if (!f.setPassiveMode()) {
                    Log.d(TAG, "[uploadVisitorPicture.run()] FAIL!! -> setPassiveMode(...)");
                    try {
                        if (f.logoutFTP()) {
                            if (f.disconnectFTP()) {
                                Log.d(TAG, "[disconnectFTP] disconnectFTP OK!!");
                            }
                        }
                        Log.d(TAG, "[disconnectFTP] disconnectFTP FAIL!!");
                    } catch (RuntimeException re) {
                        Log.d(TAG, ""+re);
                    }
                    catch (Exception e) {
                        Log.d(TAG,  "[Exception] disconnectFTP()");
                        //e.printStackTrace();
                        Log.d(TAG, ""+e);
                    }
                    Log.d(TAG, "-" +"setPassiveMode False");
                }
            }
            String path = "";
            path = "/faces/810/2001";
            //ret = f.changeWorkingDirectory(path);
            ret = f.changeWorkingDirectory(m_strPATH);
            Log.d(TAG, "set file path " +m_strPATH);
            if (ret == false) {
                m_iDownRet = -1;
                return;
            }

            ret = f.setFileType(FTP.BINARY_FILE_TYPE);
            if (ret == false) {
                m_iDownRet = -1;
                return;
            }

            String localFile = "/storage/emulated/0/InitInfo.jpg";
            //File downloadfile = new File(localFile);

            Log.d(TAG, "down file path " +m_strName);
            File downloadfile = new File(m_strName);
            if (downloadfile.exists()) downloadfile.delete();
            //if (downloadfile.exists()) FileDelete(downloadfile);//.delete();
            fos = new FileOutputStream(downloadfile);
            ffos = new BufferedOutputStream(fos);

            boolean result;
            ///"modules/app/APP_V40_IHND101_2021031602.zip"
            path = "/faces/810/2001/810_2001_test1.jpg";

            Log.d(TAG, "file name " +m_strFileName);
            result = f.retrieveFile(m_strFileName, ffos);
            //result = f.retrieveFile(path, ffos);
            ffos.flush();


            if (result == false) {
                String error = f.getError();
                m_iDownRet = -1;
                return;
            }

            m_iDownRet = 1;

        } catch (RuntimeException re) {
            Log.d(TAG, re.toString());
        }
        catch (Exception e) {
            m_iDownRet = -1;
            return;
        }
        finally {
            if(fos!=null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    Log.d(TAG, "" + e);
                }
            }
            fos = null;

            if(ffos!=null) {
                try {
                    ffos.close();
                } catch (IOException e) {
                    Log.d(TAG, "" +e);
                }
                ffos = null;
            }
        }

        try{
            f.quit();
        } catch (RuntimeException re) {
            Log.e(TAG, ""+re);
        }
        catch (Exception e) {
            Log.e(TAG, "[Exception] ftp_FileDownload.run() 2");

        }
    }

}
