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;
import kr.co.icontrols.ilp300_700.global.DataClasses.ConfigValueTable;

public class Request_get_setup_info extends SendNWaitMSG {

    static final String TAG = "Request_get_setup_info";

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

    private static void LOG(String log) {
        if (bLOG) Log.v(TAG, log);
    }

    private static void LOG_DEBUG(String log) {
        if (bLOG_DEBUG) Log.d(TAG, log);
    }

    private static void LOG_INFO(String log) {
        if (bLOG_INFO) Log.i(TAG, log);
    }

    public boolean send_get_setup_info_request(ConfigValueTable GetSetupInfoValue) {
        try {
            LOG("[send_get_setup_info_request] GetSetupInfoValue.getCnt() [" + GetSetupInfoValue.getCnt() + "]");
            return sendNwaitMSG(ConfigValue.strMainServerIPAddress, nTargetPort, MakeiMAPMSG.makeRequest_get_setup_info(GetSetupInfoValue));
        } catch (Exception e) {
            Log.e(TAG, "[Exception] send_get_setup_info_request(...)");
            e.printStackTrace();
            return false;
        }
    }

    @Override
    protected void onProcessDoing() {
        Log.d(TAG, "[onProcessDoing] ===== START =====");
    }

    @Override
    protected void onProcessDone(iMAPResult eResult) {
        try {
            Log.d(TAG, "[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("get_setup_info")) && (getAttributeValue(0, "type").equalsIgnoreCase("reply"))) {
                                miMAPResultCtrl.onResult(iMAPResult.OK);
                                return;
                            } else {
                                miMAPResultCtrl.onResult(iMAPResult.ReplyIsNotMatchWithRequest);
                                return;
                            }
                        } else if (strResult.equalsIgnoreCase("fail")) {
                            miMAPResultCtrl.onResult(iMAPResult.ResultFail);
                            return;
                        } else if (strResult.equalsIgnoreCase("none")) {
                            miMAPResultCtrl.onResult(iMAPResult.None);
                            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();
        }
    }

}