package kr.co.icontrols.ilp300_700.rfmsg;

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

import kr.co.icontrols.ilp300_700.ethernet.iMAPCommon.iMAPResult;
import kr.co.icontrols.ilp300_700.global.Configure.ConfigValue;
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_Status extends SendNWaitMultiRF {

    String TAG = "Request_MultiRF_Status";

    boolean bLOG = true;   // true, false
    boolean bLOG_DEBUG = false;   // true, false
    boolean bLOG_INFO = true;   // true, false
    Context mContext;
    iMAPResult miMAPResult;
    iMAPResultCtrl miMAPResultCtrl;

    public Request_MultiRF_Status(Context context) {
        LOG_INFO("[Request_MultiRFStatus] === START ===");
        mContext = context;
    }

    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);
    }

    public boolean send_multirf_status_request() {
        try {
            LOG_INFO("[send_multirf_status_request] ===== START =====");
            return sendNwaitMSG(MultiReaderPacket.CMD_STATUS_REPLY, makePacketMsg(MultiReaderPacket.CMD_STATUS_REQUEST), true);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] send_multirf_status_request()");
            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;

            byte[] hSitecode = Global.StringToHex(Global.ZeroPadding_Head(8, ConfigValue.strSitecode));
            hMsg[4] = hSitecode[0];   // 지역코드 1
            hMsg[5] = hSitecode[1];   // 지역코드 2
            hMsg[6] = hSitecode[2];   // 단지코드 1
            hMsg[7] = hSitecode[3];   // 단지코드 2

            byte[] hDong = Global.StringToHex(Global.ZeroPadding_Head(4, ConfigValue.strDevice_Dong));
            hMsg[8] = hDong[0];   // 동 1
            hMsg[9] = hDong[1];   // 동 2

            byte[] hID = Global.StringToHex(Global.ZeroPadding_Head(4, ConfigValue.strDevice_ID));
            hMsg[10] = hID[0];   // ID 1
            hMsg[11] = hID[1];   // ID 2

            byte[] haRSSI = getOnepassRSSI ();
            hMsg[12] = haRSSI[0];   // 임계치 1
            hMsg[13] = haRSSI[1];   // 임계치 2
            hMsg[14] = haRSSI[2];   // 임계치 3

            hMsg[15] = (Global.bSendBeacon) ? (byte) 0x01 : (byte) 0x00;
            hMsg[16] = (byte) ConfigValue.nBLEStrength;

            for (int i = 17; 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;
        }
    }

    private byte[] getOnepassRSSI() {
        byte[] haResult = { (byte) 0x0A, (byte) 0x0A, (byte) 0x0A };
        try {
            Log.i(TAG, "[getOnepassRSSI] ConfigValue.strOnePassRSSI [" + ConfigValue.strOnePassRSSI + "]");
            String[] strParsed = ConfigValue.strOnePassRSSI.split("/");
            if (strParsed.length != 3) {
                Log.i(TAG, "[getOnepassRSSI] strParsed length is not 3. return dafault values.");
                return haResult;
            }

            for (int i = 0; i < haResult.length; i++) {
                haResult[i] = (byte) Integer.parseInt(strParsed[i]);
            }

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

    @Override
    protected void onProcessDoing() {
        LOG_INFO("[onProcessDoing] ===== START =====");
    }

    @Override
    protected void onProcessDone(iMAPResult eResult) {
        try {
            LOG_INFO("[onProcessDone] eResult = " + eResult);
            if (eResult == iMAPResult.RcvDone) {
                if (strReplyResult != null) {
                    if (strReplyResult.equals("STATUS_OK")) {
                        miMAPResultCtrl.onResult(iMAPResult.OK);
                        return;
                    } else if ((Integer.valueOf(strReplyResult) >= -100) && (Integer.valueOf(strReplyResult) <= 100)) {
                        miMAPResultCtrl.onResult(iMAPResult.OK);
                        return;
                    } else {
                        miMAPResultCtrl.onResult(iMAPResult.ReplyIsNotMatchWithRequest);
                        return;
                    }
                }
                miMAPResultCtrl.onResult(iMAPResult.ReplyHasNoContents);
                return;
            } else {
                miMAPResultCtrl.onResult(eResult);
                return;
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] onProcessDone(eResult)");
            e.printStackTrace();
        }
    }

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


}