package kr.co.icontrols.ilp300_700.imap;

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

import kr.co.icontrols.ilp300_700.ethernet.SendNWaitMSG;
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.Configure.SetValue;

public class Request_save_uidcard_access extends SendNWaitMSG {

    String TAG = "Request_save_vcard_access";

    boolean bLOG = true;   // true, false
    boolean bLOG_DEBUG = false;   // true, false
    boolean bLOG_INFO = true;   // true, false
    Context mContext;
    iMAPResult miMAPResult;
    iMAPResultCtrl miMAPResultCtrl;
    int nNonEncryptedTargetPort = 10000;
    int nEncryptedTargetPort = 11000;
    int nTargetPort = nNonEncryptedTargetPort;
    public Request_save_uidcard_access() {
        LOG_INFO("[Request_save_uidcard_access] === START ===");
//        mContext = context;
        if (ConfigValue.eMainServerType == SetValue.Server2dot0_Integrated)
            nTargetPort = nEncryptedTargetPort;
    }

    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_save_uidcard_access_request(String strAction, String strDistinct, String strID_High, String strID_Low, String strCardType, String strSN, String strUID, String strMessage) {
        try {
            LOG_INFO("[send_save_uidcard_access_request] Action [" + strAction + "], Distinct [" + strDistinct + "], ID_High [" + strID_High + "], ID_Low [" + strID_Low + "]");
            LOG_INFO("[send_save_uidcard_access_request] CardType [" + strCardType + "], SN [" + strSN + "], UID [" + strUID + "]");
            LOG_INFO("[send_save_uidcard_access_request] Message [" + strMessage + "]");
            return sendNwaitMSG(ConfigValue.strMonitoringPCIPAddress, nTargetPort, MakeiMAPMSG.makeRequest_save_uidcard_access(strAction, strDistinct, strID_High, strID_Low, strCardType, strSN, strUID, strMessage));
        } catch (Exception e) {
            Log.e(TAG, "[Exception] send_save_uidcard_access_request(...)");
            e.printStackTrace();
            return false;
        }
    }

    @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 (DocMsg != null) {
                    setItem("service");
                    String strResult = getAttributeValue(0, "result");
                    if (strResult != null) {
                        if (strResult.equalsIgnoreCase("ok")) {
                            if ((getAttributeValue(0, "name").equalsIgnoreCase("save_uidcard_access")) && (getAttributeValue(0, "type").equalsIgnoreCase("reply"))) {
                                miMAPResultCtrl.onResult(iMAPResult.OK);
                                return;
                            } else {
                                miMAPResultCtrl.onResult(iMAPResult.ReplyIsNotMatchWithRequest);
                                return;
                            }
                        } else if (strResult.equalsIgnoreCase("parcel")) {
                            if ((getAttributeValue(0, "name").equalsIgnoreCase("save_uidcard_access")) && (getAttributeValue(0, "type").equalsIgnoreCase("reply"))) {
                                miMAPResultCtrl.onResult(iMAPResult.Parcel);
                                return;
                            } else {
                                miMAPResultCtrl.onResult(iMAPResult.ReplyIsNotMatchWithRequest);
                                return;
                            }
                        } else if (strResult.equalsIgnoreCase("fail")) {
                            miMAPResultCtrl.onResult(iMAPResult.ResultFail);
                            return;
                        } else {
                            miMAPResultCtrl.onResult(iMAPResult.UnexpectedReply);
                            return;
                        }
                    } else {
                        miMAPResultCtrl.onResult(iMAPResult.ReplyHasFault);
                        return;
                    }
                } else {
                    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(Result)");
            e.printStackTrace();
        }
    }

}