package kr.co.icontrols.ilp300_700.imap;

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

import kr.co.icontrols.ilp300_700.ethernet.EthernetCtrl;
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;
import kr.co.icontrols.ilp300_700.global.Declare.DeviceType;

public class Request_check_call_option extends SendNWaitMSG {

    public String strReply_Message = "";
    public OPTION eReply_Option = OPTION.NONE;
    String TAG = "Request_check_call_option";
    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_check_call_option(Context context) {
        LOG_INFO("[Request_check_call_option] === 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_check_call_option_request(String strTargetIP) {
        try {
            LOG_INFO("[send_check_call_option_request] strTargetIP [" + strTargetIP + "]");
            return sendNwaitMSG(strTargetIP, nTargetPort, MakeiMAPMSG.makeRequest_check_call_option());
        } catch (Exception e) {
            Log.e(TAG, "[Exception] send_check_call_option_request(String strTargetIP)");
            e.printStackTrace();
            return false;
        }
    }

    public boolean send_check_call_option_request(String strDong, String strHo) {
        try {
            LOG_INFO("[send_check_call_option_request] strDong [" + strDong + "], strHo [" + strHo + "]");
            String strTargetIP = EthernetCtrl.makeIPAddress(DeviceType.RESIDENCE, strDong, strHo);
            return sendNwaitMSG(strTargetIP, nTargetPort, MakeiMAPMSG.makeRequest_check_call_option());
        } catch (Exception e) {
            Log.e(TAG, "[Exception] send_check_call_option_request(String strDong, String strHo)");
            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("check_call_option")) && (getAttributeValue(0, "type").equalsIgnoreCase("reply"))) {

                                String sreReply_Option = getItemValue("option");
                                if (sreReply_Option.equals(OPTION.SIP.toString().toLowerCase()))
                                    eReply_Option = OPTION.SIP;
                                else if (sreReply_Option.equals(OPTION.DEAF.toString().toLowerCase()))
                                    eReply_Option = OPTION.DEAF;
                                else if (sreReply_Option.equals(OPTION.BLIND.toString().toLowerCase()))
                                    eReply_Option = OPTION.BLIND;
                                else if (sreReply_Option.equals(OPTION.DISABLE.toString().toLowerCase()))
                                    eReply_Option = OPTION.DISABLE;

                                strReply_Message = getItemValue("message");
                                miMAPResultCtrl.onResult(iMAPResult.OK);
                                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(eResult)");
            e.printStackTrace();
        }
    }

    // 문자통화모드 메지시 종류
    public enum OPTION {
        SIP,   // SIP
        DEAF,   // 청각장애인
        BLIND,   // 시각장애인
        DISABLE,   // 통화불가능
        NONE,   // 통화불가능
    }


}