package kr.co.icontrols.ilp300_700.rfreader;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import kr.co.icontrols.ilp300_700.global.DataClasses.STDReaderKeyInfo;
import kr.co.icontrols.ilp300_700.global.Global;
import kr.co.icontrols.ilp300_700.global.LobbyAccessCtrl;

public final class HDCIPARKManager {
    static final String TAG = "HDCIPARKManager";
    static boolean bLOG_DEBUG = false;   // true, false
    static boolean bLOG_INFO = true;   // true, false
    static Handler RFTestHandler;
    boolean bLOG = true;   // true, false

    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 void procRcvData(int nReaderType, int nMSGType, int nETC, byte[] hRcvRFData) {
        try {
            LOG_DEBUG("[procRcvData] nReaderType [" + nReaderType + "], nMSGType [" + nMSGType + "], nETC [" + nETC + "], nReaderType.length [" + hRcvRFData.length + "]");
//            LOG_DEBUG("[procRcvData] hRcvRFData = " + Global.printHexs(hRcvRFData));
            String strSitecode = Global.HextoString(hRcvRFData[1]) + Global.HextoString(hRcvRFData[2]) + Global.HextoString(hRcvRFData[3]) + Global.HextoString(hRcvRFData[4]);
            String strDong = Integer.toString(Integer.parseInt(Global.HextoString(hRcvRFData[5]) + Global.HextoString(hRcvRFData[6])));
            String strHo = Integer.toString(Integer.parseInt(Global.HextoString(hRcvRFData[7]) + Global.HextoString(hRcvRFData[8])));
            String strIDNo = Integer.toString(Integer.parseInt(Global.HextoString(hRcvRFData[9])));
            String strIssueCnt = Integer.toString(Integer.parseInt(Global.HextoString(hRcvRFData[10])));
            byte hIDType = (byte) hRcvRFData[11];

            STDReaderKeyInfo KeyInfo = new STDReaderKeyInfo();
            KeyInfo.setInfo(strSitecode, strDong, strHo, strIDNo, strIssueCnt, hIDType);

            LOG_DEBUG("[procRcvData] Sitecode [" + KeyInfo.getSitecode() + "]");
            LOG_DEBUG("[procRcvData] Dong [" + KeyInfo.getDong() + "], Ho [" + KeyInfo.getHo() + "]");
            LOG_DEBUG("[procRcvData] IDNo [" + KeyInfo.getKeyNo() + "], IssueCnt [" + KeyInfo.getIssueCnt() + "], IDType [" + KeyInfo.getIDType() + "]");

            if (RFTestHandler != null)
                sendHandlerMSG(RFTestHandler, RFReaderManager.HANDLE_RFREADER_HDCIPARK, KeyInfo);
            LobbyAccessCtrl.ctrlAccessSTDRFKey(KeyInfo);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] procRcvData(nRFType, nMSGType, nETC, hRcvRFData) : " + e);
            e.printStackTrace();
        }
    }

    public static void setHandler(Handler Handle) {
        try {
            RFTestHandler = Handle;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setHandler(Handle)");
            e.printStackTrace();
        }
    }

    public static void releaseHandler() {
        try {
            RFTestHandler = null;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] releaseHandler()");
            e.printStackTrace();
        }
    }

    private static void sendHandlerMSG(Handler Handle, int nReaderType, Object Msg) {
        try {
            LOG_INFO("[sendHandlerMSG] nReaderType [" + nReaderType + "]");
            Message Serialmsg = Handle.obtainMessage();
            Serialmsg.what = nReaderType;
            Serialmsg.obj = Msg;

            Handle.sendMessage(Serialmsg);

        } catch (Exception e) {
            Log.e(TAG, "[Exception] sendHandlerMSG()");
            e.printStackTrace();
        }
    }

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

}