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_lobby_access extends SendNWaitMSG {

    String TAG = "Request_save_lobby_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_lobby_access() {
        LOG_INFO("[Request_save_lobby_access] === START ===");
//        mContext = context;
        setDefaultServerInfo(ConfigValue.strMainServerIPAddress,ConfigValue.nMainServerPortNum);
        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_lobby_access_request(String strAction, String strTryby, String strWherefrom, String strMessage) {
        try {
            LOG_INFO("[send_save_lobby_access_request] strAction [" + strAction + "], strTryby [" + strTryby + "], strWherefrom [" + strWherefrom + "]");
            LOG_INFO("[send_save_lobby_access_request] strMessage [" + strMessage + "]");
            return sendNwaitMSG(ConfigValue.strMonitoringPCIPAddress, nTargetPort, MakeiMAPMSG.makeRequest_save_lobby_access(strAction, strTryby, strWherefrom, strMessage));
        } catch (Exception e) {
            Log.e(TAG, "[Exception] send_save_lobby_access_request(strAction, strTryby, strWherefrom, strMessage)");
            e.printStackTrace();
            return false;
        }
    }

    // 세대카드를 통한 출입시 사용
    public boolean send_save_lobby_access_request(String strAction, String strTryby, String strResiAddrIP, int nHCardNo, int nHCardCnt, String strMessage) {
        try {
            LOG_INFO("[send_save_lobby_access_request] strAction [" + strAction + "], strTryby [" + strTryby + "], strResiAddrIP [" + strResiAddrIP + "], nHCardNo [" + nHCardNo + "], nHCardCnt [" + nHCardCnt + "]");
            LOG_INFO("[send_save_lobby_access_request] strMessage [" + strMessage + "]");
            return sendNwaitMSG(ConfigValue.strMonitoringPCIPAddress, nTargetPort, MakeiMAPMSG.makeRequest_save_lobby_access(strAction, strTryby, strResiAddrIP, nHCardNo, nHCardCnt, strMessage));
        } catch (Exception e) {
            Log.e(TAG, "[Exception] send_save_lobby_access_request(strAction, strTryby, strHomeAddrIP, nHomeCardNo, nHomeCardCount, strMessage)");
            e.printStackTrace();
            return false;
        }
    }

    // 관리자 카드를 통한 출입시 사용
    public boolean send_save_lobby_access_request(String strAction, String strTryby, int nMCardNo, int nMCardCnt, String strMessage) {
        try {
            LOG_INFO("[send_save_lobby_access_request] strAction [" + strAction + "], strTryby [" + strTryby + "], nMCardNo [" + nMCardNo + "], nMCardCnt [" + nMCardCnt + "]");
            LOG_INFO("[send_save_lobby_access_request] strMessage [" + strMessage + "]");
            return sendNwaitMSG(ConfigValue.strMonitoringPCIPAddress, nTargetPort, MakeiMAPMSG.makeRequest_save_lobby_access(strAction, strTryby, nMCardNo, nMCardCnt, strMessage));
        } catch (Exception e) {
            Log.e(TAG, "[Exception] send_save_lobby_access_request(strAction, strTryby, nMasterCardNo, nMasterCardCount, strMessage)");
            e.printStackTrace();
            return false;
        }
    }

    // 생체정보 인증을 통한 출입시 사용
    public boolean send_save_lobby_access_request(String strAction, String strTryby, String strRecogResult, String strRecogReason, String strOwnerID, String strOwnerName, String strMessage) {
        try {
            LOG_INFO("[send_save_lobby_access_request] strAction [" + strAction + "], strTryby [" + strTryby + "], strRecogResult [" + strRecogResult + "], strRecogReason [" + strRecogReason + "], strOwnerID [" + strOwnerID + "], strOwnerName [" + strOwnerName + "]");
            LOG_INFO("[send_save_lobby_access_request] strMessage [" + strMessage + "]");
            return sendNwaitMSG(ConfigValue.strMonitoringPCIPAddress, nTargetPort, MakeiMAPMSG.makeRequest_save_lobby_access(strAction, strTryby, strRecogResult, strRecogReason, strOwnerID, strOwnerName, strMessage));
        } catch (Exception e) {
            Log.e(TAG, "[Exception] send_save_lobby_access_request(String strAction, String strTryby, String strRecogResult, String strRecogReason, String strOwnerID, String strOwnerName, String strMessage)");
            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_lobby_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_lobby_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();
        }
    }

}