/*
 * Copyright (c) 2015. Ncomz. All Rights Reserved
 */

package com.ntels.kneet.v3.view.main_more;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.ntels.kneet.v3.common.AppConfig;
import com.ntels.kneet.v3.common.Constants;
import com.ntels.kneet.v3.common.db.SQLManager;
import com.ntels.kneet.v3.common.gcm.support.KneetMessageLocalBroadCast;
import com.ntels.kneet.v3.common.http.HttpSend;
import com.ntels.kneet.v3.common.http.Response;
import com.ntels.kneet.v3.common.c_interface.Main_Interface;
import com.ntels.kneet.v3.common.c_interface.OnDataSendToReslut;
import com.ntels.kneet.v3.common.util.Dlog;
import com.ntels.kneet.v3.common.util.LogUtil;
import com.ntels.kneet.v3.common.util.Util;
import com.ntels.kneet.v3.common.websocket.WebSocketService;
import com.ntels.kneet.v3.common.websocket.onWebSocketResponse;
import com.ntels.kneet.v3.common.widget.CustomActionBarActivity;
import com.ntels.kneet.v3.R;
import com.ntels.kneet.v3.view.dialog.CustomDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class Homehub extends CustomActionBarActivity implements View.OnClickListener,
                                                                CustomDialog.OnMsgAlterToResult {

    private static final String TAG = Homehub.class.getSimpleName();

    /*툴바 관련*/
    private Toolbar toolbar;

    /*통신 관련*/
    private              ArrayList<HashMap<String, Object>> alTotalList                      = new ArrayList<HashMap<String, Object>>();
    private              int                                iType                            = 0;
    private static final int                                MSG_COM_G_DEVICES_HOMEHUB_STATUS = 1;
    private static final int                                MSG_COM_G_DEVICES_NODES          = 2;

    /*공통*/
    private Activity            act;
    private AppConfig           appConfig;
    private Main_Interface      mCallback;
    private Intent              intent;
    private Util                util                        = new Util();
    private CustomDialog        customDialog                = new CustomDialog();
    private String              sCallType                   = "";
    private SQLManager          sqlManager                  = new SQLManager();
    private Boolean             bAutoLogin                  = false;
    private Boolean             bSaveId                     = false;
    private String              sFileName                   = "";
    private String              sHTTP_push_type_mode_chg_yn = "";
    private String              sHTTP_push_type_rule_exc_yn = "";
    private int                 iAlram_Type                 = 0;
    private SwipeRefreshLayout  swipeRefreshLayout;
    private boolean             loading                     = false;
    private WebSocketService    webSocketService;
    private boolean             bBound                      = false;

    private static final int ALRAM_MODE_ON  = 1;
    private static final int ALRAM_MODE_OFF = 2;
    private static final int ALRAM_RULE_ON  = 3;
    private static final int ALRAM_RULE_OFF = 4;

    @Override protected void onNewIntent(Intent saveIntent) {

        super.onNewIntent(saveIntent);
        if ( null != saveIntent ) {
            Boolean bTEmp = saveIntent.getBooleanExtra(KneetMessageLocalBroadCast.INTENT_ACTION_HOMEHUB_STATE_CHANGE, false);
            Dlog.v("bTEmp", bTEmp);
            setHomeHubStateAll();
        }
    }
    /**
     * <PRE>
     * 1. MethodName: onCreate
     * 2. ClassName :
     * 3. Comment   : onCreate
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    @Override public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        act = Homehub.this;
        appConfig = (AppConfig) act.getApplicationContext();
        customDialog.setOnMsgAlterToResultEvent(this);

        Intent serviceIntent = new Intent(act, WebSocketService.class);
        act.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);

        View viewToLoad = LayoutInflater.from(act).inflate(R.layout.homehub, null);
        this.setContentView(viewToLoad);

        if ( savedInstanceState != null ) {
            // sTitle =
            // savedInstanceState.getString(Constants.EXTRA_SPINNER_TITLE);
            // aList1 =
            // savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST1);
            // aList2 =
            // savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST2);
            // // 0: 세로 한개 선택, 1: 가로 한개선택
            // iType = savedInstanceState.getInt(Constants.EXTRA_SPINNER_TYPE,
            // 0);
            // sBtn1 =
            // savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN1);
            // sBtn2 =
            // savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN2);
            // iPosition =
            // savedInstanceState.getInt(Constants.EXTRA_SPINNER_POSITION, 0);
        } else {
            Intent intent = getIntent();
            sCallType = intent.getStringExtra(Constants.EXTRA_CALL_TYPE);
        }

        bAutoLogin = appConfig.getAutoLogin();

        setToolbar();
        displayListView();
    }

    /**
     *  Service bind listener
     *
     */
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LogUtil.i(TAG, "onServiceConnected");
            WebSocketService.WebSocketBinder binder = (WebSocketService.WebSocketBinder) service;
            webSocketService = binder.getService();
            webSocketService.addWebSocketResponse(webSocketResponse);
            bBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            bBound = false;
        }
    };

    /**
     *  WebSocket listener
     *
     */
    private onWebSocketResponse webSocketResponse = new onWebSocketResponse() {
        @Override
        public void onWebSocketResponse(String result) {
            try {
                JSONObject jsonObject = new JSONObject(result);
                LogUtil.i(TAG, "websocket message type : " + jsonObject.get(Constants.WEBSOCKET_KEY_MESSAGE_TYPE));
                String resultCode = util.null2string(jsonObject.get(Constants.WEBSOCKET_KEY_RESULT_CODE));
                String resultMsg = "";
                if ( jsonObject.has(Constants.WEBSOCKET_KEY_RESULT_MSG) ) {
                    resultMsg = util.null2string(jsonObject.get(Constants.WEBSOCKET_KEY_RESULT_MSG));
                }
                if ( resultCode.equals("00") ) {
                    appConfig.setSelect_Homegrp_Homehub_Device_Id("");
                    appConfig.setsSelect_Homegrp_Homehub_Device_Key("");
                    appConfig.setsSelect_Homegrp_Homehub_Dongle_State("");
                    appConfig.setsSelect_Homegrp_Homehub_Conn_State("");

                    intent = new Intent();
                    intent.putExtra(Constants.EXTRA_RELOAD_FLAG, true);
                    act.setResult(RESULT_OK, intent);
                    finish();
                } else {
                    final String finalResultMsg = resultMsg;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            util.setShowToast(act, finalResultMsg, Toast.LENGTH_SHORT);
                        }
                    });
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onWebSocketError(String message) {
            // TODO : 웹소켓 에러 처리
        }
    };

    private void setToolbar() {

        toolbar = (Toolbar) act.findViewById(R.id.toolbar);
        util.setKeyboardHide(act, toolbar);
        util.getTextView(toolbar, R.id.tvToolbarTitle1).setVisibility(View.VISIBLE);
        util.getTextView(toolbar, R.id.tvToolbarTitle2).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.tvToolbarTitleDivider).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.tvToolbarTitle3).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.tvToolbarTitle4).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.btnToolbarDel).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.btnToolbarClose).setVisibility(View.GONE);
        util.getTextView(toolbar, R.id.btnToolbarMore).setVisibility(View.GONE);

        util.getTextView(toolbar, R.id.tvToolbarTitle1).setText(util.getString(act, R.string.layout_homehub_01));

        setHomeHubStateAll();
    }

    public void setHomeHubStateAll() {

        setHomeHubState();
    }

    private void setHomeHubState() {

        if ( appConfig.getsSelect_Homegrp_Homehub_Conn_State().equalsIgnoreCase("connect") ) {
            util.getView(act, R.id.llState_Online).setVisibility(View.VISIBLE);
            util.getView(act, R.id.llState_Offline).setVisibility(View.GONE);
        } else {
            util.getView(act, R.id.llState_Online).setVisibility(View.GONE);
            util.getView(act, R.id.llState_Offline).setVisibility(View.VISIBLE);
        }

        if ( appConfig.getsSelect_Homegrp_Homehub_Dongle_State().equalsIgnoreCase("ON") ) {
            util.getView(act, R.id.llState_Dongle_Online).setVisibility(View.VISIBLE);
            util.getView(act, R.id.llState_Dongle_Offline).setVisibility(View.GONE);
        } else {
            util.getView(act, R.id.llState_Dongle_Online).setVisibility(View.GONE);
            util.getView(act, R.id.llState_Dongle_Offline).setVisibility(View.VISIBLE);
            if ( !appConfig.getsSelect_Homegrp_Homehub_Dongle_Online_Last_Datetime().equals("") ) {
                util.getTextView(act, R.id.tvRecentDate).setVisibility(View.VISIBLE);
                util.getTextView(act, R.id.tvRecentDate).setText(act.getString(R.string.layout_homehub_15,
                        util.stringToDate(appConfig.getsSelect_Homegrp_Homehub_Dongle_Online_Last_Datetime())));
            } else {
                util.getTextView(act, R.id.tvRecentDate).setVisibility(View.GONE);
            }
        }

        /*if ( appConfig.getSelect_Homegrp_Homehub_Online_State().equals("ON") ) {
            util.getView(act, R.id.llState_Online).setVisibility(View.VISIBLE);
            util.getView(act, R.id.llState_Offline).setVisibility(View.GONE);
        } else if ( appConfig.getSelect_Homegrp_Homehub_Online_State().equals("OFF") ) {
            util.getView(act, R.id.llState_Online).setVisibility(View.GONE);
            util.getView(act, R.id.llState_Offline).setVisibility(View.VISIBLE);
        }*/
    }

    private void displayListView() {

        swipeRefreshLayout = (SwipeRefreshLayout) act.findViewById(R.id.swiperefreshlayou);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_red_light, android.R.color.holo_green_light, android.R.color.holo_blue_bright, android.R.color.holo_orange_light);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override public void onRefresh() {

                if ( !loading ) {
                    loading = true;
                    ComHandler.sendMessageDelayed(ComHandler.obtainMessage(MSG_COM_G_DEVICES_NODES), 10);
                }
            }
        });
        setDisp();
        ComHandler.sendMessageDelayed(ComHandler.obtainMessage(MSG_COM_G_DEVICES_NODES), 10);
    }

    private void setDisp() {

        util.getView(act, R.id.btnKeyChange).setOnClickListener(this);
        util.getView(act, R.id.llBtn1).setOnClickListener(this);
        util.getView(act, R.id.btnHomehubDelete).setOnClickListener(this);

            if ( appConfig.getSelect_Homegrp_Grade_Code().equals("MAS") ) {
                util.getView(act, R.id.llKeyChange).setVisibility(View.VISIBLE);
                util.getView(act, R.id.llHomehubDelete).setVisibility(View.VISIBLE);
            } else {
                util.getView(act, R.id.llKeyChange).setVisibility(View.GONE);
                util.getView(act, R.id.llHomehubDelete).setVisibility(View.GONE);
            }
    }

    @Override public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @Override public void onClick(View v) {

        switch (v.getId()) {
            case R.id.llBtn1:
                onBackPressed();
                break;
            case R.id.btnKeyChange:
                intent = new Intent(act, Homehub_Password_Change.class);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_HOMEHUB_PASSWORD_CHANGE);
                break;
            case R.id.btnHomehubDelete:
                customDialog.MsgAlert(act, Constants.MSG904);
                break;
            default:
                break;
        }
    }

    Handler ComHandler = new Handler(new IncomingHandlerCallBack());

    class IncomingHandlerCallBack implements Handler.Callback {

        @Override public boolean handleMessage(Message msg) {

            iType = msg.what;
            switch (msg.what) {
                case MSG_COM_G_DEVICES_HOMEHUB_STATUS:
//                    send_G_devices_homehub_status();
                    break;
                case MSG_COM_G_DEVICES_NODES:
                    send_G_devices_device_id();
                    break;

                default:
                    break;
            }
            return true;
        }
    }

    /**
     * <PRE>
     * 1. MethodName: send_G_devices_homehub_status
     * 2. ClassName :
     * 3. Comment   : [Send][GET]홈허브 장치 상태 정보 조회(현재 홈그룹에 등록된 홈허브의 상태 정보를 조회한다.)
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    /*private void send_G_devices_homehub_status() {

        try {
            HttpSend httpSend;

            httpSend = new HttpSend(act, !swipeRefreshLayout.isRefreshing());
            //            httpSend = new HttpSend(act);

            httpSend.setOnDataSendToReslutEvent(new OnDataSendToReslut() {
                @Override public void resultResponse(Response result) {

                    result_G_devices_homehub_status(result);
                }
            });
            httpSend.G_devices_homehub_status();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }*/

    /**
     * <PRE>
     * 1. MethodName: send_G_devices_device_id
     * 2. ClassName :
     * 3. Comment   : [Send][GET]해당 디바이스 상세 정보 조회
     * 4. Developer : 정백호
     * 5. Created   : 2016. 07.
     * </PRE>
     */
    private void send_G_devices_device_id() {
        try {
            HttpSend httpSend;

            httpSend = new HttpSend(act, !swipeRefreshLayout.isRefreshing());
            //            httpSend = new HttpSend(act);

            httpSend.setOnDataSendToReslutEvent(new OnDataSendToReslut() {
                @Override public void resultResponse(Response result) {

                    result_G_devices_device_id(result);
                }
            });
            httpSend.G_devices_device_id(appConfig.getSelect_Homegrp_Homehub_Device_Id(), "");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * <PRE>
     * 1. MethodName: result_G_devices_homehub_status
     * 2. ClassName :
     * 3. Comment   : [Result][GET]홈허브 장치 상태 정보 조회(현재 홈그룹에 등록된 홈허브의 상태 정보를 조회한다.)
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
   /* private void result_G_devices_homehub_status(Response rpResult) {

        try {
            if ( rpResult.getStatusCode() == 200 ) {
                String sTempHomehub_State = appConfig.getSelect_Homegrp_Homehub_Online_State();
                JSONObject joResult = new JSONObject(rpResult.getContent());
                String sHTTP_online_state = util.null2string(joResult.get(Constants.HTTP_online_state));
                String sHTTP_create_datetime = util.null2string(joResult.get(Constants.HTTP_create_datetime));

                appConfig.setSelect_Homegrp_Homehub_Online_State(sHTTP_online_state);
                if ( !sTempHomehub_State.equals(sHTTP_online_state) ) {
                    //상태가 변경되었을때만 브로드캐스트를 날려준다
                    Intent intent = new Intent(KneetMessageLocalBroadCast.INTENT_ACTION);
                    intent.putExtra(KneetMessageLocalBroadCast.INTENT_NAME_TYPE, KneetMessageLocalBroadCast.INTENT_ACTION_HOMEHUB_STATE_CHANGE);
                    intent.putExtra(KneetMessageLocalBroadCast.INTENT_ACTION_HOMEHUB_STATE_CHANGE, true);
                    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
                }

                setHomeHubState();
                util.getTextView(act, R.id.tvDate).setText(util.UTCtoLocal5(sHTTP_create_datetime));

                loading = false;
                swipeRefreshLayout.setRefreshing(false);
            } else {
                JSONObject joResult = new JSONObject(rpResult.getContent());
                Dlog.v(joResult.toString());
                String sERR_CODE = util.null2string(joResult.get(Constants.HTTP_err_code));
                String sERR_MSG = util.null2string(joResult.get(Constants.HTTP_err_msg));

                if ( sERR_CODE.equals("401") ) {
                    customDialog.MsgAlert(act, Constants.MSG998, sERR_MSG);
                } else if ( sERR_CODE.equals("401-106") ) {
                    customDialog.MsgAlert(act, Constants.MSG997, sERR_MSG);
                } else {
                    if ( sERR_CODE.equals("400-404") ) {
                        appConfig.setSelect_Homegrp_Homehub_Online_State("");
                        //상태가 변경되었을때만 브로드캐스트를 날려준다
                        Intent intent = new Intent(KneetMessageLocalBroadCast.INTENT_ACTION);
                        intent.putExtra(KneetMessageLocalBroadCast.INTENT_NAME_TYPE, KneetMessageLocalBroadCast.INTENT_ACTION_HOMEHUB_STATE_CHANGE);
                        intent.putExtra(KneetMessageLocalBroadCast.INTENT_ACTION_HOMEHUB_STATE_CHANGE, true);
                        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.MsgAlert(act, Constants.MSG807);
        }
    }*/

    /**
     * <PRE>
     * 1. MethodName: result_G_devices_device_id
     * 2. ClassName :
     * 3. Comment   : [Result][GET]해당 디바이스 상세 정보 조회
     * 4. Developer : 정백호
     * 5. Created   : 2016. 07.
     * </PRE>
     */
    private void result_G_devices_device_id(Response rpResult) {

        try {
            if ( rpResult.getStatusCode() == 200 ) {
//                String sTempHomehub_State = appConfig.getSelect_Homegrp_Homehub_Online_State();
                String sTempDevice_Online = appConfig.getsSelect_Homegrp_Homehub_Dongle_State();
                JSONObject joResult = new JSONObject(rpResult.getContent());
//                String sHTTP_online_state = util.null2string(joResult.get(Constants.HTTP_online_state));
                String sHTTP_device_conn = util.null2string(joResult.get(Constants.HTTP_device_conn));
                String sHTTP_device_online = util.null2string(joResult.get(Constants.HTTP_device_online));
                String sHTTP_device_online_last_datetime = util.null2string(joResult.get(Constants.HTTP_device_online_last_datetime));
                String sHTTP_device_sn = util.null2string(joResult.get(Constants.HTTP_device_sn));
                String sHTTP_device_model_name = util.null2string(joResult.get(Constants.HTTP_device_model_id)) +
                        " | " + util.null2string(joResult.get(Constants.HTTP_device_mf_id));
                String sHTTP_device_id = util.null2string(joResult.get(Constants.HTTP_device_id));
                String sHTTP_device_key = util.null2string(joResult.get(Constants.HTTP_device_key));

//                appConfig.setSelect_Homegrp_Homehub_Online_State(sHTTP_online_state);
                appConfig.setsSelect_Homegrp_Homehub_Conn_State(sHTTP_device_conn);
                appConfig.setsSelect_Homegrp_Homehub_Dongle_State(sHTTP_device_online);
                appConfig.setsSelect_Homegrp_Homehub_Dongle_Online_Last_Datetime(sHTTP_device_online_last_datetime);
                appConfig.setSelect_Homegrp_Homehub_Device_Id(sHTTP_device_id);
                appConfig.setsSelect_Homegrp_Homehub_Device_Key(sHTTP_device_key);
                if ( !sTempDevice_Online.equals(sHTTP_device_online) ) {
                    //상태가 변경되었을때만 브로드캐스트를 날려준다
                    Intent intent = new Intent(KneetMessageLocalBroadCast.INTENT_ACTION);
                    intent.putExtra(KneetMessageLocalBroadCast.INTENT_NAME_TYPE, KneetMessageLocalBroadCast.INTENT_ACTION_HOMEHUB_STATE_CHANGE);
                    intent.putExtra(KneetMessageLocalBroadCast.INTENT_ACTION_HOMEHUB_STATE_CHANGE, true);
                    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
                }

                setHomeHubState();
//                util.getTextView(act, R.id.tvDate).setText(util.UTCtoLocal5(sHTTP_create_datetime));
                util.getTextView(act, R.id.tvDeviceSN).setText(sHTTP_device_sn);
                util.getTextView(act, R.id.tvModelName).setText(sHTTP_device_model_name);

                loading = false;
                swipeRefreshLayout.setRefreshing(false);
            } else {
                JSONObject joResult = new JSONObject(rpResult.getContent());
                Dlog.v(joResult.toString());
                String sERR_CODE = util.null2string(joResult.get(Constants.HTTP_err_code));
                String sERR_MSG = util.null2string(joResult.get(Constants.HTTP_err_msg));

                if ( sERR_CODE.equals("401") ) {
                    customDialog.MsgAlert(act, Constants.MSG998, sERR_MSG);
                } else if ( sERR_CODE.equals("401-106") ) {
                    customDialog.MsgAlert(act, Constants.MSG997, sERR_MSG);
                } else {
                    if ( sERR_CODE.equals("400-404") ) {
                        appConfig.setsSelect_Homegrp_Homehub_Dongle_State("");
                        //상태가 변경되었을때만 브로드캐스트를 날려준다
                        Intent intent = new Intent(KneetMessageLocalBroadCast.INTENT_ACTION);
                        intent.putExtra(KneetMessageLocalBroadCast.INTENT_NAME_TYPE, KneetMessageLocalBroadCast.INTENT_ACTION_HOMEHUB_STATE_CHANGE);
                        intent.putExtra(KneetMessageLocalBroadCast.INTENT_ACTION_HOMEHUB_STATE_CHANGE, true);
                        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.MsgAlert(act, Constants.MSG807);
        }
    }

    /**
     * <PRE>
     * 1. MethodName: onBackPressed
     * 2. ClassName :
     * 3. Comment   : 뒤로가기 버튼
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    @Override public void onBackPressed() {

        intent = new Intent();
        act.setResult(RESULT_CANCELED, intent);
        finish();
        return;
    }

    /**
     * <PRE>
     * 1. MethodName: onResume
     * 2. ClassName :
     * 3. Comment   : onResume
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    @Override public void onResume() {

        super.onResume();
    }

    @Override public void resultCustomDialogResponse(int iMsgID, int iBtnResult) {

        switch (iMsgID) {
            case Constants.MSG998:
                util.callApp_Start(act, "LOGOUT");
                break;
            case Constants.MSG997:
                util.callApp_Start(act, "HOME_RELOAD");
                break;
            case Constants.MSG807:
                if ( iBtnResult == 1 ) {
                    ComHandler.sendMessageDelayed(ComHandler.obtainMessage(iType), 10);
                }
                break;
            case Constants.MSG904:
                if ( iBtnResult == 1 ) {
                    send_WS_device_delete();
                }
                break;
            default:
                break;
        }
    }

    @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        if ( resultCode == act.RESULT_OK ) {
            switch (requestCode) {
                //                case Constants.ACT_SETTING_ACCOUNT_EDIT:
                //                case Constants.ACT_SETTING_NAME_CHANGE:
                //                case Constants.ACT_SETTING_ACCOUNT_EMAIL:
                //                    ComHandler.sendMessageDelayed(ComHandler.obtainMessage(MSG_COM_G_MEMBER), 10);
                //                    break;
                //                case Constants.ACT_IMAGE_SELECT:
                //                    sFileName = util.null2string(data.getStringExtra(Constants.EXTRA_IMAGE_FILE_NAME));
                //                    ComHandler.sendMessageDelayed(ComHandler.obtainMessage(MSG_COM_P_MEMBER_PROFILE_IMAGE), 10);
                //                    break;
            }
        } else {
            switch (requestCode) {
                default:
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        if ( bBound ) {
            webSocketService.removeWebSocketResponse(webSocketResponse);
            unbindService(serviceConnection);
            bBound = false;
        }
        super.onDestroy();
    }

    /**
     * <PRE>
     * 1. MethodName: send_WS_device_delete
     * 2. ClassName :
     * 3. Comment   : [Send]서비스 허브에 등록된 홈허브 디바이스를 삭제한다.
     * 4. Developer : 정백호
     * 5. Created   : 2016. 07.
     * </PRE>
     */
    private void send_WS_device_delete() {
        HashMap<String, Object> sendParams = new HashMap<>();
        sendParams.put(Constants.WEBSOCKET_KEY_MESSAGE_TYPE, Constants.WEBSOCKET_TYPE_DEVICE_DELETE);
        sendParams.put(Constants.WEBSOCKET_KEY_DEVICE_ID, appConfig.getSelect_Homegrp_Homehub_Device_Id());
        sendParams.put(Constants.WEBSOCKET_KEY_DEVICE_KEY, appConfig.getsSelect_Homegrp_Homehub_Device_Key());
        sendParams.put(Constants.WEBSOCKET_KEY_REQUEST_ID, appConfig.getiSelect_Servicehub_Request_Id());

        webSocketService.send(Constants.WEBSOCKET_SEND_DEVICE_REGISTER, sendParams);
    }
}
