package kr.co.icontrols.ilp300_700.rfmsg;

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

import kr.co.icontrols.ilp300_700.global.Configure;
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_ZBSensitivity extends SendNWaitMultiRF {

    String TAG = "Request_MultiRF_ZBSensitivity";

    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_ZBSensitivity(Context context) {
        mContext = context;
    }

    public boolean send_zigbee_sensitivity_request(byte hDATA1) {
        try {
            LOG_INFO("[send_zigbee_sensitivity_request] ===== START =====");
            return sendNwaitMSG(MultiReaderPacket.CMD_ZIGBEE_SENSITIVITY_REPLY, makePacketMsg(MultiReaderPacket.CMD_ZIGBEE_SENSITIVITY_REQUEST, hDATA1), true);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] send_zigbee_sensitivity_request(byte hDATA1)");
            e.printStackTrace();
            return false;
        }
    }

    private byte[] makePacketMsg(byte hCMD, byte hDATA1) {
        try {
            LOG("[makePacketMsg] hCMD [" + Global.HextoString(hCMD) + "], hData1 [" + Global.HextoString(hDATA1) + "]");
            byte[] hMsg = new byte[28];
            hMsg[0] = MultiReaderPacket.STX;
            hMsg[1] = MultiReaderPacket.ID_RFREADER;
            hMsg[2] = MultiReaderPacket.ID_LOBBY;
            hMsg[3] = hCMD;
            hMsg[4] = hDATA1;

            if (hCMD == MultiReaderPacket.CMD_ZIGBEE_SENSITIVITY_REQUEST) {
                if (hDATA1 == MultiReaderPacket.DATA_GET_ZIGBEE_SENSITIVITY) {
                    for (int i = 5; i < hMsg.length - 2; i++) {
                        hMsg[i] = 0x00;
                    }
                } else if (hDATA1 == MultiReaderPacket.DATA_SET_ZIGBEE_SENSITIVITY) {
                    LOG("[makePacketMsg] ConfigValue.nZigBeeSensitivity [" + Configure.ConfigValue.nZigBeeSensitivity + "], [" + Global.HextoString((byte) Configure.ConfigValue.nZigBeeSensitivity) + "]");
                    hMsg[5] = (byte) Configure.ConfigValue.nZigBeeSensitivity;
                    hMsg[6] = 0x00;
                    hMsg[7] = 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();
        }
    }
}
