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.SystemUpgradeInfo;
import kr.co.icontrols.ilp300_700.global.Enumeration.UPGRADESTATUS;
import kr.co.icontrols.ilp300_700.global.Global;
import kr.co.icontrols.ilp300_700.global.SystemUpgrade;

public class Request_propose_upgrade extends SendNWaitMSG {

    String TAG = "Request_propose_upgrade";

    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;
    SystemUpgradeInfo RcvdSystemUpgradeInfo;
    public Request_propose_upgrade(Context context) {
        LOG_INFO("[Request_propose_upgrade] === START ===");
        mContext = context;
        setDefaultServerInfo(ConfigValue.strMainServerIPAddress,ConfigValue.nMainServerPortNum);
        if (ConfigValue.eMainServerType == SetValue.Server2dot0_Integrated)
            nTargetPort = nEncryptedTargetPort;
        RcvdSystemUpgradeInfo = new SystemUpgradeInfo();
    }

    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_propose_upgrade_request() {
        try {
            return sendNwaitMSG(nTargetPort, MakeiMAPMSG.makeRequest_ProposeUpgrade(SystemUpgrade.FinalSystemUpgradeInfo));
        } catch (Exception e) {
            Log.e(TAG, "[Exception] send_propose_upgrade_request()");
            e.printStackTrace();
            Global.setUpgradeStatus(UPGRADESTATUS.IDLE);
            return false;
        }
    }

    public void setResultCtrl(iMAPResultCtrl eResult) {
        try {
            miMAPResultCtrl = eResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setResultCtrl(eResult)");
            e.printStackTrace();
        }
    }

    @Override
    protected void onProcessDoing() {
    }

    @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("propose_upgrade")) && (getAttributeValue(0, "type").equalsIgnoreCase("reply"))) {
                                int nItemCnt = setItem("guidemap");
                                RcvdSystemUpgradeInfo.nModuleCnt = nItemCnt;
                                RcvdSystemUpgradeInfo.setArray();
                                for (int i = 0; i < nItemCnt; i++) {
                                    RcvdSystemUpgradeInfo.aModule_Type[i] = getAttributeValue(i, "type");
                                    RcvdSystemUpgradeInfo.aModule_FileName[i] = getAttributeValue(i, "fname");
                                    RcvdSystemUpgradeInfo.aModule_FileSize[i] = getAttributeValue(i, "fsize");
                                    LOG_INFO("[onProcessDone] aModule_Type[" + i + "] = " + RcvdSystemUpgradeInfo.aModule_Type[i]);
                                    LOG_INFO("[onProcessDone] aModule_FileName[" + i + "] = " + RcvdSystemUpgradeInfo.aModule_FileName[i]);
                                    LOG_INFO("[onProcessDone] aModule_FileSize[" + i + "] = " + RcvdSystemUpgradeInfo.aModule_FileSize[i]);
                                }

                                int nMatchCnt = 0;
                                if (SystemUpgrade.FinalSystemUpgradeInfo.nModuleCnt == RcvdSystemUpgradeInfo.nModuleCnt) {
                                    for (int i = 0; i < nItemCnt; i++) {
                                        for (int j = 0; j < nItemCnt; j++) {
                                            if (SystemUpgrade.FinalSystemUpgradeInfo.aModule_Type[i].equalsIgnoreCase(RcvdSystemUpgradeInfo.aModule_Type[j])) {
                                                if (SystemUpgrade.FinalSystemUpgradeInfo.aModule_FileName[i].equalsIgnoreCase(RcvdSystemUpgradeInfo.aModule_FileName[j])) {
                                                    SystemUpgrade.FinalSystemUpgradeInfo.aModule_FileSize[i] = RcvdSystemUpgradeInfo.aModule_FileSize[j];
                                                    nMatchCnt++;
                                                }
                                            }
                                        }
                                    }

                                    if (nItemCnt == nMatchCnt) {
                                        LOG_INFO("[FinalSystemUpgradeInfo] strType = " + SystemUpgrade.FinalSystemUpgradeInfo.strType);
                                        LOG_INFO("[FinalSystemUpgradeInfo] nModuleCnt = " + SystemUpgrade.FinalSystemUpgradeInfo.nModuleCnt);
                                        LOG_INFO("[FinalSystemUpgradeInfo] strUserID = " + SystemUpgrade.FinalSystemUpgradeInfo.strUserID);
                                        LOG_INFO("[FinalSystemUpgradeInfo] strPassword = " + SystemUpgrade.FinalSystemUpgradeInfo.strPassword);
                                        for (int i = 0; i < SystemUpgrade.FinalSystemUpgradeInfo.nModuleCnt; i++) {
                                            LOG_INFO("[FinalSystemUpgradeInfo] aModule_Type[" + i + "] = " + SystemUpgrade.FinalSystemUpgradeInfo.aModule_Type[i]);
                                            LOG_INFO("[FinalSystemUpgradeInfo] aModule_FileName[" + i + "] = " + SystemUpgrade.FinalSystemUpgradeInfo.aModule_FileName[i]);
                                            LOG_INFO("[FinalSystemUpgradeInfo] aModule_Path[" + i + "] = " + SystemUpgrade.FinalSystemUpgradeInfo.aModule_Path[i]);
                                            LOG_INFO("[FinalSystemUpgradeInfo] aModule_FileSize[" + i + "] = " + SystemUpgrade.FinalSystemUpgradeInfo.aModule_FileSize[i]);
                                        }

                                        miMAPResultCtrl.onResult(iMAPResult.OK);
                                        return;
                                    } else {
                                        miMAPResultCtrl.onResult(iMAPResult.ReplyHasFault);
                                        return;
                                    }
                                } else {
                                    miMAPResultCtrl.onResult(iMAPResult.ReplyHasFault);
                                    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();
        }
    }


}