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.Declare.MultiReaderPacket;
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_FWVer extends SendNWaitMultiRF {

    String TAG = "Request_MultiRF_FWVer";

    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_FWVer(Context context) {
        LOG_INFO("[Request_MultiRF_FWVer] === 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_fwver_request() {
        try {
            LOG_INFO("[send_multirf_fwver_request] ===== START =====");
            return sendNwaitMSG(MultiReaderPacket.CMD_FW_VER_REPLY, makePacketMsg(), true);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] send_multirf_fwver_request()");
            e.printStackTrace();
            return false;
        }
    }

    private byte[] makePacketMsg() {
        try {
            byte[] hMsg = new byte[28];
            hMsg[0] = MultiReaderPacket.STX;
            hMsg[1] = MultiReaderPacket.ID_RFREADER;
            hMsg[2] = MultiReaderPacket.ID_LOBBY;
            hMsg[3] = MultiReaderPacket.CMD_FW_VER_REQUEST;
            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;
        }
    }

    @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.contains("FWVER_")) {
                        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();
        }
    }


}