package kr.co.icontrols.ilp300_700.rfreader;

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

import kr.co.icontrols.ilp300_700.MainActivity;
import kr.co.icontrols.ilp300_700.database.DBManager;
import kr.co.icontrols.ilp300_700.global.Configure.ConfigValue;
import kr.co.icontrols.ilp300_700.global.Declare;
import kr.co.icontrols.ilp300_700.global.Global;

public final class STDAccessKeyManager {

    static String TAG = "STDAccessKeyManager";

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

    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 initRFKeySTD(Context context) {
        try {
            mContext = context;
            if (!initSTDKeyData()) return false;
            if (!initRFKeySTD_Resi()) return false;
            if (!initRFKeySTD_AdminDong()) return false;
            if (!initRFKeySTD_AdminEntire()) return false;
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] initRFKeySTD()");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean initRFKeySTD_Resi() {
        try {
            MainActivity.RFKeySTD_RESI_DBManager = new DBManager(mContext, Declare.DBInfo.DATABASE_FILENAME_RFKEY_STD_RESI);

            if (checkDB_RFKeySTD_Resi()) {
                // DB 파일이 존재하는 경우 (일반 부팅으로 간주)
                LOG_INFO("[initRFKeySTD_Resi] RFKeySTD_Resi DB OK!!");
                return getDBRFKeyResi();
            } else {
                // DB 파일이 존재하지 않는 경우 (최초부팅으로 간주)
                cleanDB_RFKeySTD_RESI();
                LOG_INFO("[initRFKeySTD_Resi] RFKeySTD_Resi DB NOT OK!!");
                MainActivity.RFKeySTD_RESI_DBManager.createTable(Declare.DBInfo.TABLE_NAME_RFKEY_STD_RESI, Declare.DBInfo.VALUE_NAME_DEFAULT);
                return createRFKey_Resi();
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] initRFKeySTD_Resi()");
            e.printStackTrace();
            return false;
        }
    }

    private static boolean checkDB_RFKeySTD_Resi() {
        try {
            if (!MainActivity.RFKeySTD_RESI_DBManager.isDBFileExist()) return false;
            if (!MainActivity.RFKeySTD_RESI_DBManager.initDatabase()) return false;
            if (!MainActivity.RFKeySTD_RESI_DBManager.isTableExist(Declare.DBInfo.TABLE_NAME_RFKEY_STD_RESI))
                return false;
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] checkDB_RFKeySTD_Resi()");
            e.printStackTrace();
            return false;
        }
    }

    private static void cleanDB_RFKeySTD_RESI() {
        try {
            LOG_INFO("[cleanDB_RFKeySTD_RESI] ===== START =====");
            if (MainActivity.RFKeySTD_RESI_DBManager.isDBFileExist()) {
                LOG_INFO("[cleanDB_RFKeySTD_RESI] DBFile exist!!!");
                MainActivity.RFKeySTD_RESI_DBManager.deleteDBFile();
            }
            MainActivity.RFKeySTD_RESI_DBManager.initDatabase();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] cleanDB_RFKeySTD_RESI()");
            e.printStackTrace();
        }
    }

    public static boolean initRFKeySTD_AdminDong() {
        try {
            MainActivity.RFKeySTD_ADMINDONG_DBManager = new DBManager(mContext, Declare.DBInfo.DATABASE_FILENAME_RFKEY_STD_ADMIN_DONG);

            if (checkDB_RFKeySTD_AdminDong()) {
                // DB 파일이 존재하는 경우 (일반 부팅으로 간주)
                LOG_INFO("[initRFKeySTD_AdminDong] RFKeySTD_ADMINDONG DB OK!!");
                MainActivity.RFKeySTD_ADMINDONG_DBManager.initDatabase();
                return getDBRFKeyAdminDong();
            } else {
                // DB 파일이 존재하지 않는 경우 (최초부팅으로 간주)
                LOG_INFO("[initRFKeySTD_AdminDong] RFKeySTD_ADMINDONG DB NOT OK!!");
                cleanDB_RFKeySTD_ADMINDONG();
                MainActivity.RFKeySTD_ADMINDONG_DBManager.createTable(Declare.DBInfo.TABLE_NAME_RFKEY_STD_ADMIN_DONG, Declare.DBInfo.VALUE_NAME_DEFAULT);
                return createRFKey_AdminDong();
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] initRFKeySTD_AdminDong()");
            e.printStackTrace();
            return false;
        }
    }

    private static boolean checkDB_RFKeySTD_AdminDong() {
        try {
            if (!MainActivity.RFKeySTD_ADMINDONG_DBManager.isDBFileExist()) return false;
            if (!MainActivity.RFKeySTD_ADMINDONG_DBManager.initDatabase()) return false;
            if (!MainActivity.RFKeySTD_ADMINDONG_DBManager.isTableExist(Declare.DBInfo.TABLE_NAME_RFKEY_STD_ADMIN_DONG))
                return false;
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] checkDB_RFKeySTD_Resi()");
            e.printStackTrace();
            return false;
        }
    }

    private static void cleanDB_RFKeySTD_ADMINDONG() {
        try {
            LOG_INFO("[cleanDB_RFKeySTD_ADMINDONG] ===== START =====");
            if (MainActivity.RFKeySTD_ADMINDONG_DBManager.isDBFileExist()) {
                LOG_INFO("[cleanDB_RFKeySTD_ADMINDONG] DBFile exist!!!");
                MainActivity.RFKeySTD_ADMINDONG_DBManager.deleteDBFile();
            }
            MainActivity.RFKeySTD_ADMINDONG_DBManager.initDatabase();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] cleanDB_RFKeySTD_ADMINDONG()");
            e.printStackTrace();
        }
    }

    public static boolean initRFKeySTD_AdminEntire() {
        try {
            MainActivity.RFKeySTD_ADMINENTIRE_DBManager = new DBManager(mContext, Declare.DBInfo.DATABASE_FILENAME_RFKEY_STD_ADMIN_ENTIRE);

            if (checkDB_RFKeySTD_AdminEntire()) {
                // DB 파일이 존재하는 경우 (일반 부팅으로 간주)
                LOG_INFO("[initRFKeySTD_AdminEntire] RFKeySTD_ADMINENTIRE DB OK!!");
                return getDBRFKeyAdminEntire();
            } else {
                // DB 파일이 존재하지 않는 경우 (최초부팅으로 간주)
                LOG_INFO("[initRFKeySTD_AdminEntire] RFKeySTD_ADMINENTIRE NOT DB OK!!");
                cleanDB_RFKeySTD_ADMINENTIRE();
                MainActivity.RFKeySTD_ADMINENTIRE_DBManager.createTable(Declare.DBInfo.TABLE_NAME_RFKEY_STD_ADMIN_ENTIRE, Declare.DBInfo.VALUE_NAME_DEFAULT);
                return createRFKey_AdminEntire();
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] initRFKeySTD_AdminEntire()");
            e.printStackTrace();
            return false;
        }
    }

    private static boolean checkDB_RFKeySTD_AdminEntire() {
        try {
            if (!MainActivity.RFKeySTD_ADMINENTIRE_DBManager.isDBFileExist()) return false;
            if (!MainActivity.RFKeySTD_ADMINENTIRE_DBManager.initDatabase()) return false;
            if (!MainActivity.RFKeySTD_ADMINENTIRE_DBManager.isTableExist(Declare.DBInfo.TABLE_NAME_RFKEY_STD_ADMIN_ENTIRE))
                return false;
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] checkDB_RFKeySTD_Resi()");
            e.printStackTrace();
            return false;
        }
    }

    private static void cleanDB_RFKeySTD_ADMINENTIRE() {
        try {
            LOG_INFO("[cleanDB_RFKeySTD_ADMINENTIRE] ===== START =====");
            if (MainActivity.RFKeySTD_ADMINENTIRE_DBManager.isDBFileExist()) {
                LOG_INFO("[cleanDB_RFKeySTD_ADMINENTIRE] DBFile exist!!!");
                MainActivity.RFKeySTD_ADMINENTIRE_DBManager.deleteDBFile();
            }
            MainActivity.RFKeySTD_ADMINENTIRE_DBManager.initDatabase();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] cleanDB_RFKeySTD_ADMINENTIRE()");
            e.printStackTrace();
        }
    }

    // Standard Key 정보 초기화
    private static boolean initSTDKeyData() {
        try {
            AccessKey_STD.aResiKey = new int[100][100];
            AccessKey_STD.aDongAdminKey = new int[100];
            AccessKey_STD.aEntireAdminKey = new int[100];
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] initSTDKeyData()");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean createRFKey_Resi() {
        try {
            if (!initResiKey()) return false;
            if (!setResiKey()) return false;
            return setDBRFKeyResi();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] createRFKey_Resi()");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean createRFKey_AdminDong() {
        try {
            if (!initDongAdminKey()) return false;
            return setDBRFKeyAdminDong();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] createRFKey_AdminDong()");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean createRFKey_AdminEntire() {
        try {
            if (!initEntireAdminKey()) return false;
            return setDBRFKeyAdminEntire();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] createRFKey_AdminEntire()");
            e.printStackTrace();
            return false;
        }
    }

    // 세대 출입키
    public static boolean initResiKey() {
        try {
            // 세대 출입키의 발급횟수를 0으로 초기화 한다.
            for (int i = 0; i < 100; i++) {
                for (int j = 0; j < 100; j++) {
                    AccessKey_STD.aResiKey[i][j] = 0;
                }
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] initResiKey()");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setResiKey() {
        try {
            // 세대 출입키의 발급횟수를 라인정보에 맞추어 1로 초기화한다.
            // 존재하는 세대만 1로 설정한다.
            if (ConfigValue.LineInfo.getCnt() < 0 || 100 < ConfigValue.LineInfo.getCnt()) {
                Log.i(TAG, "[setResiKey] nLineCnt is out of range!!");
                return false;
            }
            for (int i = 0; i < ConfigValue.LineInfo.getCnt(); i++) {
                for (int j = ConfigValue.LineInfo.getLineInfo(i).LOW(); j <= ConfigValue.LineInfo.getLineInfo(i).HIGH(); j++) {
                    if (AccessKey_STD.aResiKey[j][ConfigValue.LineInfo.getLineInfo(i).NAME()] == 0) {
                        // 라인정보 변경시 세대카드정보를 초기화하면 기존의 세대카드정보들도 초기화 되기때문에 정보가 0인 경우에만 1로 세팅하고, 0이 아닌 경우에는 그 값을 보존한다.
                        AccessKey_STD.aResiKey[j][ConfigValue.LineInfo.getLineInfo(i).NAME()] = 1;
                    }
                }
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setResiKey()");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setEntireResiKey(int nIssueCnt) {
        try {
            // 모든 세대 출입키의 발급횟수를 주어진 파라미터값으로 변경한다.
            if (ConfigValue.LineInfo.getCnt() < 0 || 99 < ConfigValue.LineInfo.getCnt()) {
                Log.i(TAG, "[setEntireResiKey] nLineCnt is out of range!!");
                return false;
            }

            if (nIssueCnt < 0 || 99 < nIssueCnt) {
                Log.i(TAG, "[setEntireResiKey] nIssueCnt is out of range!!");
                return false;
            }

            for (int i = 0; i < ConfigValue.LineInfo.getCnt(); i++) {
                for (int j = ConfigValue.LineInfo.getLineInfo(i).LOW(); j <= ConfigValue.LineInfo.getLineInfo(i).HIGH(); j++) {
                    AccessKey_STD.aResiKey[j][ConfigValue.LineInfo.getLineInfo(i).NAME()] = nIssueCnt;
                }
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setEntireResiKey(nIssueCnt)");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setResiKey(int nDong, int nHo, int nIssueCnt) {
        try {
            // 해당 세대의 발급횟수를 변경한다.
            if (nIssueCnt < 0 || 99 < nIssueCnt) {
                Log.i(TAG, "[setResiKey] nIssueCnt is out of range!!");
                return false;
            }
            if (!Global.isExistResi(nHo)) {
                Log.i(TAG, "[setResiKey] nHo is out of range!!");
                return false;
            }

            int nHo_Head = nHo / 100;
            int nHo_Tail = nHo % 100;
            AccessKey_STD.aResiKey[nHo_Head][nHo_Tail] = nIssueCnt;
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setResiKey(nDong, nHo, nIssueCnt)");
            e.printStackTrace();
            return false;
        }
    }

    // 동관리자 출입키
    public static boolean initDongAdminKey() {
        try {
            // 모든 동관리자 출입키의 발급횟수를 1로 설정한다.
            for (int i = 0; i < AccessKey_STD.aDongAdminKey.length; i++) {
                AccessKey_STD.aDongAdminKey[i] = 1;
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] initDongAdminKey()");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setEntireDongAdminKey(int nIssueCnt) {
        try {
            // 모든 동관리자 출입키의 발급횟수를 주어진 파라메터로 설정한다.
            if (nIssueCnt < 0 || 99 < nIssueCnt) {
                Log.i(TAG, "[setEntireDongAdminKey] nIssueCnt is out of range!!");
                return false;
            }

            for (int i = 0; i < AccessKey_STD.aDongAdminKey.length; i++) {
                AccessKey_STD.aDongAdminKey[i] = nIssueCnt;
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setEntireDongAdminKey(nIssueCnt)");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setDongAdminKey(int nIDNo, int nIssueCnt) {
        try {
            // 해당 동관리자 출입키의 발급횟수를 주어진 파라메터값으로 변경한다.
            if (nIDNo < 0 || 99 < nIDNo) {
                Log.i(TAG, "[setDongAdminKey] nIDNo is out of range!!");
                return false;
            }
            if (nIssueCnt < 0 || 99 < nIssueCnt) {
                Log.i(TAG, "[setDongAdminKey] nIssueCnt is out of range!!");
                return false;
            }
            AccessKey_STD.aDongAdminKey[nIDNo] = nIssueCnt;
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setDongAdminKey(nKeyNo, nIssueCnt)");
            e.printStackTrace();
            return false;
        }
    }

    // 전체관리자 출입키
    public static boolean initEntireAdminKey() {
        try {
            // 모든 전체관리자 출입키의 발급횟수를 1로 설정한다.
            for (int i = 0; i < AccessKey_STD.aEntireAdminKey.length; i++) {
                AccessKey_STD.aEntireAdminKey[i] = 1;
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] initEntireAdminKey()");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setEntireAdminKey(int nIssueCnt) {
        try {
            // 모든 전체관리자 출입키의 발급횟수를 주어진 파라메터로 설정한다.
            if (nIssueCnt < 0 || 99 < nIssueCnt) {
                Log.i(TAG, "[setEntireAdminKey] nIssueCnt is out of range!!");
                return false;
            }

            for (int i = 0; i < AccessKey_STD.aEntireAdminKey.length; i++) {
                AccessKey_STD.aEntireAdminKey[i] = nIssueCnt;
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setEntireAdminKey(nIssueCnt)");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setEntireAdminKey(int nIDNo, int nIssueCnt) {
        try {
            // 해당 전체관리자 출입키의 발급횟수를 주어진 파라메터값으로 변경한다.
            if (nIDNo < 0 || 99 < nIDNo) {
                Log.i(TAG, "[setEntireAdminKey] nIDNo is out of range!!");
                return false;
            }
            if (nIssueCnt < 0 || 99 < nIssueCnt) {
                Log.i(TAG, "[setEntireAdminKey] nIssueCnt is out of range!!");
                return false;
            }
            AccessKey_STD.aEntireAdminKey[nIDNo] = nIssueCnt;
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setEntireAdminKey(nIDNo, nIssueCnt)");
            e.printStackTrace();
            return false;
        }
    }

    // 세대 출입키 DB
    public static boolean setDBRFKeyResi() {
        try {
            if (ConfigValue.LineInfo.getCnt() < 0 || 99 < ConfigValue.LineInfo.getCnt()) {
                Log.i(TAG, "[setDBRFKeyResi] nLineCnt is out of range!!");
                return false;
            }

            int nHo_Head = 0;
            int nHo_Tail = 0;
            String strResiInfo = "";
            for (int i = 0; i < ConfigValue.LineInfo.getCnt(); i++) {
                nHo_Tail = ConfigValue.LineInfo.getLineInfo(i).NAME();
                for (int j = ConfigValue.LineInfo.getLineInfo(i).LOW(); j <= ConfigValue.LineInfo.getLineInfo(i).HIGH(); j++) {
                    nHo_Head = j;
                    strResiInfo = ConfigValue.strDevice_Dong + "-" + Integer.toString(nHo_Head) + Global.ZeroPadding_Head(2, Integer.toString(nHo_Tail));
                    MainActivity.RFKeySTD_RESI_DBManager.setData(Declare.DBInfo.TABLE_NAME_RFKEY_STD_RESI, strResiInfo, AccessKey_STD.aResiKey[nHo_Head][nHo_Tail]);
                }
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setDBRFKeyResi()");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setDBRFKeyResi(int nDong, int nHo, int nIssueCnt) {
        try {
            LOG("[setDBRFKeyResi] nDong [" + nDong + "], nHo [" + nHo + "], nIssueCnt [" + nIssueCnt + "]");
            int nHo_Head = nHo / 100;
            int nHo_Tail = nHo % 100;
            String strResiInfo = ConfigValue.strDevice_Dong + "-" + Integer.toString(nHo_Head) + Global.ZeroPadding_Head(2, Integer.toString(nHo_Tail));
            MainActivity.RFKeySTD_RESI_DBManager.setData(Declare.DBInfo.TABLE_NAME_RFKEY_STD_RESI, strResiInfo, AccessKey_STD.aResiKey[nHo_Head][nHo_Tail]);
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setDBRFKeyResi(nDong, nHo, nIssueCnt)");
            e.printStackTrace();
            return false;
        }
    }

    // 동관리자 출입키 DB
    public static boolean setDBRFKeyAdminDong() {
        try {
            for (int i = 0; i < AccessKey_STD.aDongAdminKey.length; i++) {
                MainActivity.RFKeySTD_ADMINDONG_DBManager.setData(Declare.DBInfo.TABLE_NAME_RFKEY_STD_ADMIN_DONG, Integer.toString(i), AccessKey_STD.aDongAdminKey[i]);
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setDBRFKeyAdminDong()");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setDBRFKeyAdminDong(int nIDNo, int nIssueCnt) {
        try {
            LOG("[setDBRFKeyAdminDong] nIDNo [" + nIDNo + "], nIssueCnt [" + nIssueCnt + "]");
            MainActivity.RFKeySTD_ADMINDONG_DBManager.setData(Declare.DBInfo.TABLE_NAME_RFKEY_STD_ADMIN_DONG, Integer.toString(nIDNo), AccessKey_STD.aDongAdminKey[nIDNo]);
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setDBRFKeyAdminDong(nIDNo, nIssueCnt)");
            e.printStackTrace();
            return false;
        }
    }

    // 전체관리자 출입키 DB
    public static boolean setDBRFKeyAdminEntire() {
        try {
            for (int i = 0; i < AccessKey_STD.aEntireAdminKey.length; i++) {
                MainActivity.RFKeySTD_ADMINENTIRE_DBManager.setData(Declare.DBInfo.TABLE_NAME_RFKEY_STD_ADMIN_ENTIRE, Integer.toString(i), AccessKey_STD.aEntireAdminKey[i]);
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setDBRFKeyAdminEntire()");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setDBRFKeyAdminEntire(int nIDNo, int nIssueCnt) {
        try {
            MainActivity.RFKeySTD_ADMINENTIRE_DBManager.setData(Declare.DBInfo.TABLE_NAME_RFKEY_STD_ADMIN_ENTIRE, Integer.toString(nIDNo), AccessKey_STD.aEntireAdminKey[nIDNo]);
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setDBRFKeyAdminEntire(nIDNo, nIssueCnt)");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean getDBRFKeyResi() {
        try {
            if (ConfigValue.LineInfo.getCnt() < 0 || 99 < ConfigValue.LineInfo.getCnt()) {
                Log.i(TAG, "[getDBRFKeyResi] nLineCnt is out of range!!");
                return false;
            }

//            ConfigValue.LineInfo.printValue();

            int nHo_Head = 0;
            int nHo_Tail = 0;
            String strResiInfo = "";
            for (int i = 0; i < ConfigValue.LineInfo.getCnt(); i++) {
                nHo_Tail = ConfigValue.LineInfo.getLineInfo(i).NAME();
                for (int j = ConfigValue.LineInfo.getLineInfo(i).LOW(); j <= ConfigValue.LineInfo.getLineInfo(i).HIGH(); j++) {
                    nHo_Head = j;
                    strResiInfo = ConfigValue.strDevice_Dong + "-" + Integer.toString(nHo_Head) + Global.ZeroPadding_Head(2, Integer.toString(nHo_Tail));
//                    LOG_DEBUG("nHo_Head [" + nHo_Head + "], nHo_Tail [" + nHo_Tail + "], strResiInfo [" + strResiInfo + "]");
//                    LOG_DEBUG("AccessKey_STD.aResiKey[" + nHo_Head + "][" + nHo_Tail + "] = [" + AccessKey_STD.aResiKey[nHo_Head][nHo_Tail] + "]");
                    AccessKey_STD.aResiKey[nHo_Head][nHo_Tail] = Integer.parseInt(MainActivity.RFKeySTD_RESI_DBManager.getData(Declare.DBInfo.TABLE_NAME_RFKEY_STD_RESI, strResiInfo));
                }
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getDBRFKeyResi()");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean getDBRFKeyAdminDong() {
        try {
            for (int i = 0; i < AccessKey_STD.aDongAdminKey.length; i++) {
                AccessKey_STD.aDongAdminKey[i] = Integer.parseInt(MainActivity.RFKeySTD_ADMINDONG_DBManager.getData(Declare.DBInfo.TABLE_NAME_RFKEY_STD_ADMIN_DONG, Integer.toString(i)));
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getDBRFKeyAdminDong()");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean getDBRFKeyAdminEntire() {
        try {
            for (int i = 0; i < AccessKey_STD.aEntireAdminKey.length; i++) {
                AccessKey_STD.aEntireAdminKey[i] = Integer.parseInt(MainActivity.RFKeySTD_ADMINENTIRE_DBManager.getData(Declare.DBInfo.TABLE_NAME_RFKEY_STD_ADMIN_ENTIRE, Integer.toString(i)));
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getDBRFKeyAdminEntire()");
            e.printStackTrace();
            return false;
        }
    }

    // 다른 클래스에 제공되는 함수
    // 라인정보 변경시 변경된 라인정보에 따라 출입키 정보 설정
    public static boolean procLineInfoChanged() {
        try {
            if (!setResiKey()) return false;
            if (!setDBRFKeyResi()) return false;
            if (!getDBRFKeyResi()) return false;
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procLineInfoChanged()");
            e.printStackTrace();
            return false;
        }
    }

    // 키정보 업데이트 함수
    public static boolean updateAllResiAccessKey(int nIssueCnt) {
        try {
            LOG("[updateAllResiAccessKey] nIssueCnt = " + nIssueCnt);
            if (!setEntireResiKey(nIssueCnt)) {
                LOG("[updateAllResiAccessKey] Fail from setEntireResiKey(nIssueCnt");
                return false;
            }

            if (!setDBRFKeyResi()) {
                LOG("[updateAllResiAccessKey] Fail from setDBRFKeyResi()");
                return false;
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] updateAllResiAccessKey(nIssueCnt)");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean updateResiAccessKey(int nDong, int nHo, int nIssueCnt) {
        try {
            LOG("[updateResiAccessKey] nDong = " + nDong + ", nHo = " + nHo + ", nIssueCnt = " + nIssueCnt);
            if (!setResiKey(nDong, nHo, nIssueCnt)) {
                LOG("[updateResiAccessKey] Fail from setResiKey(nDong, nHo, nIssueCnt)");
                return false;
            }

            if (!setDBRFKeyResi(nDong, nHo, nIssueCnt)) {
                LOG("[updateResiAccessKey] Fail from setDBRFKeyResi(nDong, nHo, nIssueCnt)");
                return false;
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] updateResiAccessKey(nDong, nHo, nIssueCnt)");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean updateAllDongAdminAccessKey(int nIssueCnt) {
        try {
            LOG("[updateAllDongAdminAccessKey] nIssueCnt = " + nIssueCnt);
            if (!setEntireDongAdminKey(nIssueCnt)) {
                LOG("[updateAllDongAdminAccessKey] Fail from setEntireDongAdminKey(nIssueCnt");
                return false;
            }

            if (!setDBRFKeyAdminDong()) {
                LOG("[updateAllDongAdminAccessKey] Fail from setEntireDongAdminKey()");
                return false;
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] updateAllDongAdminAccessKey(nIssueCnt)");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean updateDongAdminAccessKey(int nIDNo, int nIssueCnt) {
        try {
            LOG("[updateDongAdminAccessKey] nIDNo = " + nIDNo + ", nIssueCnt = " + nIssueCnt);
            if (!setDongAdminKey(nIDNo, nIssueCnt)) {
                LOG("[updateDongAdminAccessKey] Fail from setDongAdminKey(nIDNo, nIssueCnt)");
                return false;
            }

            if (!setDBRFKeyAdminDong(nIDNo, nIssueCnt)) {
                LOG("[updateDongAdminAccessKey] Fail from setDBRFKeyAdminDong(nIDNo, nIssueCnt)");
                return false;
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] updateDongAdminAccessKey(nIDNo, nIssueCnt)");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean updateAllEntireAdminAccessKey(int nIssueCnt) {
        try {
            LOG("[updateAllEntireAdminAccessKey] nIssueCnt = " + nIssueCnt);
            if (!setEntireAdminKey(nIssueCnt)) {
                LOG("[updateAllEntireAdminAccessKey] Fail from setEntireAdminKey(nIssueCnt");
                return false;
            }

            if (!setDBRFKeyAdminEntire()) {
                LOG("[updateAllEntireAdminAccessKey] Fail from setEntireDongAdminKey()");
                return false;
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] updateAllEntireAdminAccessKey(nIssueCnt)");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean updateEntireAdminAccessKey(int nIDNo, int nIssueCnt) {
        try {
            LOG("[updateEntireAdminAccessKey] nIDNo = " + nIDNo + ", nIssueCnt = " + nIssueCnt);
            if (!setEntireAdminKey(nIDNo, nIssueCnt)) {
                LOG("[updateEntireAdminAccessKey] Fail from setEntireAdminKey(nIDNo, nIssueCnt)");
                return false;
            }

            if (!setDBRFKeyAdminEntire(nIDNo, nIssueCnt)) {
                LOG("[updateEntireAdminAccessKey] Fail from setDBRFKeyAdminEntire(nIDNo, nIssueCnt)");
                return false;
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] updateEntireAdminAccessKey(nIDNo, nIssueCnt)");
            e.printStackTrace();
            return false;
        }
    }

    // 키정보 초기화
    public boolean createRFKey() {
        try {
            if (!initResiKey()) return false;
            if (!setResiKey()) return false;
            if (!initDongAdminKey()) return false;
            if (!initEntireAdminKey()) return false;
            return setDBRFKeyAll();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] createRFKey()");
            e.printStackTrace();
            return false;
        }
    }

    // Database 관련 함수 정의
    public boolean setDBRFKeyAll() {
        try {
            if (!setDBRFKeyResi()) return false;
            if (!setDBRFKeyAdminDong()) return false;
            if (!setDBRFKeyAdminEntire()) return false;
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setDBRFKeyAll()");
            e.printStackTrace();
            return false;
        }
    }

    // 키정보 가저오기
    public boolean getDBRFKeyAll() {
        try {
            if (!getDBRFKeyResi()) return false;
            if (!getDBRFKeyAdminDong()) return false;
            if (!getDBRFKeyAdminEntire()) return false;
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getDBRFKeyAll()");
            e.printStackTrace();
            return false;
        }
    }

    // 로비폰 출입키를 저장하는 클래스
    public static class AccessKey_STD {
        public static int[][] aResiKey;
        public static int[] aDongAdminKey;
        public static int[] aEntireAdminKey;
    }


}