package kr.co.icontrols.ilp300_700.rfmsg;

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

import kr.co.icontrols.ilp300_700.global.Declare.MultiReaderPacket;
import kr.co.icontrols.ilp300_700.global.Global;
import kr.co.icontrols.ilp300_700.imap.iMAPResultCtrl;
import kr.co.icontrols.ilp300_700.rfreader.SendNWaitMultiRF;
import kr.co.icontrols.ilp300_700.serialport.HDCStandardReaderSerial;

public class Request_MultiRF_Detection extends SendNWaitMultiRF {

    String TAG = "Request_MultiRF_Detection";

    boolean bLOG = true;
    boolean bLOG_DEBUG = false;
    boolean bLOG_INFO = true;
    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);
    }

    iMAPResultCtrl miMAPResultCtrl;

    Context mContext;

    public Request_MultiRF_Detection(Context context) {
        mContext = context;
    }

    public boolean send_smartkeycr_detection() {
        try {
            LOG_INFO("[send_smartkeycr_detection] ===== START =====");
            return sendNwaitMSG(MultiReaderPacket.CMD_SMARTKEY_CARDREADER, makePacketMsg(MultiReaderPacket.CMD_SMARTKEY_CARDREADER), false);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] send_smartkeycr_detection");
            e.printStackTrace();
            return false;
        }
    }

    private byte[] makePacketMsg(byte hCMD) {
        try {
            LOG_DEBUG("[makePacketMsg] hCMD [" + Global.HextoString(hCMD) + "]");
            byte[] hMsg = new byte[28];
            hMsg[0] = MultiReaderPacket.STX;
            hMsg[1] = MultiReaderPacket.ID_RFREADER;
            hMsg[2] = MultiReaderPacket.ID_LOBBY;
            hMsg[3] = hCMD;
            for (int i = 4; i < hMsg.length - 2; i++) {
                hMsg[i] = 0x00;
            }

            hMsg[hMsg.length - 2] = HDCStandardReaderSerial.makeChecksum(hMsg);
            hMsg[hMsg.length - 1] = MultiReaderPacket.ETX;
            return hMsg;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] makePacketMsg()");
            e.printStackTrace();
            return null;
        }
    }

    public void setResultCtrl(iMAPResultCtrl eResult) {
        try {
            miMAPResultCtrl = eResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setResultCtrl(eResult)");
            e.printStackTrace();
        }
    }

}
