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

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

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
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.DatabaseOpenHelper;
import com.ntels.kneet.v3.common.db.SQLManager;
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.Util;
import com.ntels.kneet.v3.common.widget.CustomActionBarActivity;
import com.ntels.kneet.v3.R;
import com.ntels.kneet.v3.view.dialog.CustomDialog;
import com.ntels.kneet.v3.view.Home_Start;
import com.ntels.kneet.v3.view.Image_Select;

import org.json.JSONObject;

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

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

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

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

    /*공통*/
    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              String       sHTTP_NEW_EMAIL_ID          = "";
    private              int          iAlram_Type                 = 0;
    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;
    private SwipeRefreshLayout swipeRefreshLayout;
    private boolean loading = false;

    /**
     * <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 = Setting.this;
        appConfig = (AppConfig) act.getApplicationContext();
        customDialog.setOnMsgAlterToResultEvent(this);

        View viewToLoad = LayoutInflater.from(act).inflate(R.layout.setting, 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);
        }

        setToolbar();
        displayListView();
    }

    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_setting_01));
    }

    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_MEMBER), 10);
                }
            }
        });
        setDisp();
        ComHandler.sendMessageDelayed(ComHandler.obtainMessage(MSG_COM_G_MEMBER), 10);
    }

    private void setRefresh(boolean bVal) {

        if ( bVal ) {
            swipeRefreshLayout.setEnabled(true);
        } else {
            swipeRefreshLayout.setEnabled(false);
        }
    }

    private void setDisp() {

        util.getViewUnderline(act, R.id.tvNameChange);
        util.getViewUnderline(act, R.id.tvPassChange);
        util.getViewUnderline(act, R.id.tvEmailChange);
        //        util.getViewUnderline(act, R.id.llGotoService);
        //        util.getViewUnderline(act, R.id.llGotoPolicy);
        //        util.getViewUnderline(act, R.id.llGotoGeo);

        util.getView(act, R.id.rlMemberImage).setOnClickListener(this);
        util.getView(act, R.id.llAutoLogin).setOnClickListener(this);
        util.getView(act, R.id.btnLogout).setOnClickListener(this);
        util.getView(act, R.id.tvNameChange).setOnClickListener(this);
        util.getView(act, R.id.tvPassChange).setOnClickListener(this);
        util.getView(act, R.id.tvEmailChange).setOnClickListener(this);
        util.getView(act, R.id.llGotoService).setOnClickListener(this);
        util.getView(act, R.id.llGotoPolicy).setOnClickListener(this);
        //        util.getView(act, R.id.llGotoGeo).setOnClickListener(this);
        util.getView(act, R.id.llAlramHomeMode).setOnClickListener(this);
        util.getView(act, R.id.llAlramHouse).setOnClickListener(this);
        util.getView(act, R.id.btnAdminSetting).setOnClickListener(this);
        util.getView(act, R.id.llBtn1).setOnClickListener(this);

        String sUrl = appConfig.getMy_Image_Url();
        util.setRefreshImage(act, sUrl, util.getImageView(act, R.id.ivMemberIcon));

        util.getTextView(act, R.id.tvEmail).setText(appConfig.getLogin_Id());
        util.getTextView(act, R.id.tvName).setText(appConfig.getNickname());
        util.getTextView(act, R.id.tvAppVersion).setText(util.getVersionName(act));

        bAutoLogin = appConfig.getAutoLogin();

        Dlog.v("bAutoLogin =" + bAutoLogin);
        if ( bAutoLogin ) {
            util.getView(act, R.id.llAutoLogin).setSelected(true);
        } else {
            util.getView(act, R.id.llAutoLogin).setSelected(false);
        }

        sHTTP_push_type_mode_chg_yn = appConfig.getPush_Type_Mode_Chg_Yn();
        if ( appConfig.getPush_Type_Mode_Chg_Yn().equals("Y") ) {
            util.getView(act, R.id.llAlramHomeMode).setSelected(true);
        } else {
            util.getView(act, R.id.llAlramHomeMode).setSelected(false);
        }

        sHTTP_push_type_rule_exc_yn = appConfig.getPush_Type_Prsnc_Chg_Yn();
        if ( appConfig.getPush_Type_Prsnc_Chg_Yn().equals("Y") ) {
            util.getView(act, R.id.llAlramHouse).setSelected(true);
        } else {
            util.getView(act, R.id.llAlramHouse).setSelected(false);
        }
        if ( appConfig.getSelect_Homegrp_Homehub_Online_State().equals("ON") ) {
            util.getView(act, R.id.llAlramHomeMode).setEnabled(true);
            util.getView(act, R.id.llAlramHouse).setEnabled(true);
            util.getView(act, R.id.llAlramNoHomehub).setVisibility(View.GONE);
        } else {
            util.getView(act, R.id.llAlramHomeMode).setEnabled(false);
            util.getView(act, R.id.llAlramHouse).setEnabled(false);
            util.getView(act, R.id.llAlramNoHomehub).setVisibility(View.VISIBLE);
        }
        Dlog.v("sNEW_EMAIL_ID =", sHTTP_NEW_EMAIL_ID);
        if ( sHTTP_NEW_EMAIL_ID.equals("") ) {
            util.getTextView(act, R.id.tvEmailConfirm).setVisibility(View.GONE);
            util.getTextView(act, R.id.tvEmailConfirm).setText("");
        } else {
            util.getTextView(act, R.id.tvEmailConfirm).setVisibility(View.VISIBLE);
            util.getTextView(act, R.id.tvEmailConfirm).setText(sHTTP_NEW_EMAIL_ID + util.getString(act, R.string.layout_setting_21));
        }
            util.getView(act, R.id.llAdminSetting).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.rlMemberImage:
                intent = new Intent(act, Image_Select.class);
                intent.putExtra(Constants.EXTRA_CROP_TYPE, true);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_IMAGE_SELECT);
                break;
            case R.id.llAutoLogin:
                if ( v.isSelected() ) {
                    v.setSelected(false);
                } else {
                    v.setSelected(true);
                }
                bAutoLogin = v.isSelected();
                appConfig.setAutoLogin(bAutoLogin);
                //db 입력작업
                SQLiteDatabase db = DatabaseOpenHelper.getInstance(act).getWritableDatabase();
                String sSql = "";
                sSql = "";
                sSql = sqlManager.QryUpdateLoginInfo_AutoLogin(bAutoLogin);
                db.execSQL(sSql);
                db.close();
                break;
            case R.id.btnLogout:
                customDialog.MsgAlert(act, Constants.MSG805);
                break;
            case R.id.tvNameChange:
                intent = new Intent(act, Setting_Name_Change.class);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_SETTING_NAME_CHANGE);
                break;
            case R.id.tvEmailChange:
                intent = new Intent(act, Setting_Email_Change.class);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_SETTING_EMAIL_CHANGE);
                break;
            case R.id.tvPassChange:
                intent = new Intent(act, Setting_Password_Chk.class);
                intent.putExtra(Constants.EXTRA_CALL_TYPE, "1");
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_SETTING_PASSWORD_CHK); break;
            case R.id.btnAdminSetting:
                intent = new Intent(act, Setting_Admin_Password_Chk.class);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_SETTING_ADMIN_PASSWORD_CHK);
                break;
            case R.id.llAlramHomeMode:
                v.setEnabled(false);
                if ( v.isSelected() ) {
                    sHTTP_push_type_mode_chg_yn = "N";
                    iAlram_Type = ALRAM_MODE_OFF;
                } else {
                    sHTTP_push_type_mode_chg_yn = "Y";
                    iAlram_Type = ALRAM_MODE_ON;
                }
                ComHandler.sendMessageDelayed(ComHandler.obtainMessage(MSG_COM_P_MEMBER_PUSH_NOTI), 10);
                break;
            case R.id.llAlramHouse:
                v.setEnabled(false);
                if ( v.isSelected() ) {
                    sHTTP_push_type_rule_exc_yn = "N";
                    iAlram_Type = ALRAM_RULE_OFF;
                } else {
                    sHTTP_push_type_rule_exc_yn = "Y";
                    iAlram_Type = ALRAM_RULE_ON;
                }
                ComHandler.sendMessageDelayed(ComHandler.obtainMessage(MSG_COM_P_MEMBER_PUSH_NOTI), 10);
                break;
            case R.id.llGotoService:
                intent = new Intent(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse("http://home-api.allofthings.com/guide/20001/service"));
                startActivity(intent);
                break;
            case R.id.llGotoPolicy:
                intent = new Intent(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse("http://home-api.allofthings.com/guide/20001/policy"));
                startActivity(intent);
                break;
            //            case R.id.llGotoGeo:
            //                intent = new Intent(Intent.ACTION_VIEW);
            //                intent.addCategory(Intent.CATEGORY_DEFAULT);
            //                intent.setData(Uri.parse("https://home-api.allofthings.com/guide/geo"));
            //                startActivity(intent);
            //                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_MEMBER:
                    send_G_member();
                    break;
                case MSG_COM_P_MEMBER_PROFILE_IMAGE:
                    send_P_member_profile_image();
                    break;
                case MSG_COM_P_MEMBER_PUSH_NOTI:
                    send_P_member_push_noti();
                    break;
                default:
                    break;
            }
            return true;
        }
    }

    private void send_P_member_push_noti() {

        try {
            HttpSend httpSend = new HttpSend(act);
            httpSend.setOnDataSendToReslutEvent(new OnDataSendToReslut() {
                @Override public void resultResponse(Response result) {

                    result_P_member_push_noti(result);
                }
            });

            //공통 변수 입력
            httpSend.P_member_push_noti(sHTTP_push_type_mode_chg_yn, sHTTP_push_type_rule_exc_yn);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void result_P_member_push_noti(Response rpResult) {

        try {
            if ( rpResult.getStatusCode() == 200 ) {
                int iTempMsgNum = 0;
                //                Dlog.v(iAlram_Type);
                if ( iAlram_Type == ALRAM_MODE_ON ) {
                    appConfig.setPush_Type_Mode_Chg_Yn("Y");
                    iTempMsgNum = R.string.MSG815;
                } else if ( iAlram_Type == ALRAM_MODE_OFF ) {
                    appConfig.setPush_Type_Mode_Chg_Yn("N");
                    iTempMsgNum = R.string.MSG816;
                } else if ( iAlram_Type == ALRAM_RULE_ON ) {
                    appConfig.setPush_Type_Prsnc_Chg_Yn("Y");
                    iTempMsgNum = R.string.MSG815;
                } else if ( iAlram_Type == ALRAM_RULE_OFF ) {
                    appConfig.setPush_Type_Prsnc_Chg_Yn("N");
                    iTempMsgNum = R.string.MSG816;
                }
                util.setShowToast(act, util.getString(act, iTempMsgNum), Toast.LENGTH_SHORT);
                //                util.setShowToast(act, util.getString(act, iTempMsgNum), Toast.LENGTH_SHORT);
                setDisp();
            } 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 {
                    customDialog.MsgAlert(act, Constants.MSG000, sERR_MSG);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.MsgAlert(act, Constants.MSG807);
        }
    }

    private void send_P_member_profile_image() {

        try {
            HttpSend httpSend = new HttpSend(act);
            httpSend.setOnDataSendToReslutEvent(new OnDataSendToReslut() {
                @Override public void resultResponse(Response result) {

                    result_P_member_profile_image(result);
                }
            });

            //공통 변수 입력
            httpSend.P_member_profile_image(Constants.HTTP_image_file, sFileName);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void result_P_member_profile_image(Response rpResult) {

        try {
            if ( rpResult.getStatusCode() == 200 ) {
                JSONObject joResult = new JSONObject(rpResult.getContent());
                Dlog.v(joResult.toString());

                //회원 번호
                sFileName = util.null2string(joResult.get(Constants.HTTP_image_file_name));
                util.setRefreshImage(act, sFileName, R.id.ivMemberIcon);
            } 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 {
                    customDialog.MsgAlert(act, Constants.MSG000, sERR_MSG);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.MsgAlert(act, Constants.MSG807);
        }
    }

    private void send_G_member() {

        try {
            HttpSend httpSend = new HttpSend(act, !swipeRefreshLayout.isRefreshing());
            httpSend.setOnDataSendToReslutEvent(new OnDataSendToReslut() {
                @Override public void resultResponse(Response result) {

                    result_G_member(result);
                }
            });

            //공통 변수 입력
            httpSend.G_member(appConfig.getUuid(), appConfig.getPush_Token(), Constants.OS_TYPE, util.getHostName());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void result_G_member(Response rpResult) {

        try {
            if ( rpResult.getStatusCode() == 200 ) {

                JSONObject joResult = new JSONObject(rpResult.getContent());
                Dlog.v(joResult.toString());

                //회원 번호
                String sMEMBER_ID = util.null2string(joResult.get(Constants.HTTP_member_id));
                //이메일 ID
                String sEMAIL_ID = util.null2string(joResult.get(Constants.HTTP_email_id));
                //서비스 ID
                String sSERVICE_ID = util.null2string(joResult.get(Constants.HTTP_service_id));
                //국가 코드 (ex: KR)
                String sCOUNTRY_CODE = util.null2string(joResult.get(Constants.HTTP_country_code));
                //타임존 ID
                String sTIMEZONE_ID = util.null2string(joResult.get(Constants.HTTP_timezone_id));
                //타임존 시간 (ex: +09:00)
                String sTIMEZONE = util.null2string(joResult.get(Constants.HTTP_timezone));
                //언어 코드 (ex: en)
                String sLANG = util.null2string(joResult.get(Constants.HTTP_lang));
                //인증 토큰
                String sAUTH_TOKEN = util.null2string(joResult.get(Constants.HTTP_auth_token));

                //회원이미지 추가
                String sIMAGE_FILE_NAME = "";
                if ( joResult.has(Constants.HTTP_image_file_name) ) {
                    sIMAGE_FILE_NAME = util.null2string(joResult.get(Constants.HTTP_image_file_name));
                    appConfig.setMy_Image_Url(sIMAGE_FILE_NAME);
                }
                //회원명
                String sNICKNAME = "";
                if ( joResult.has(Constants.HTTP_nickname) ) {
                    sNICKNAME = util.null2string(joResult.get(Constants.HTTP_nickname));
                    appConfig.setNickname(sNICKNAME);
                }

                sHTTP_NEW_EMAIL_ID = "";
                if ( joResult.has(Constants.HTTP_new_email_id) ) {
                    sHTTP_NEW_EMAIL_ID = util.null2string(joResult.get(Constants.HTTP_new_email_id));
                }

                String sHTTP_TEMP_PASSWORD_YN = "";
                if ( joResult.has(Constants.HTTP_temp_password_yn) ) {
                    sHTTP_TEMP_PASSWORD_YN = util.null2string(joResult.get(Constants.HTTP_temp_password_yn));
                }
                appConfig.setTemp_Password_Yn(sHTTP_TEMP_PASSWORD_YN);

                String sPUSH_TYPE_MODE_CHG_YN = util.null2string(joResult.get(Constants.HTTP_push_type_mode_chg_yn));
                String sPUSH_TYPE_PRSNC_CHG_YN = util.null2string(joResult.get(Constants.HTTP_push_type_prsnc_chg_yn));

                String sHTTP_invitations_count = "";
                if ( joResult.has(Constants.HTTP_invitations_count) ) {
                    sHTTP_invitations_count = util.null2string(joResult.get(Constants.HTTP_invitations_count));
                }

                String sHTTP_homegrp_id = "";
                if ( joResult.has(Constants.HTTP_homegrp_id) ) {
                    sHTTP_homegrp_id = util.null2string(joResult.get(Constants.HTTP_homegrp_id));
                }

                String sHTTP_homegrp_image_file_name = "";
                if ( joResult.has(Constants.HTTP_homegrp_image_file_name) ) {
                    sHTTP_homegrp_image_file_name = util.null2string(joResult.get(Constants.HTTP_homegrp_image_file_name));
                }

                String sHTTP_homegrp_name = "";
                if ( joResult.has(Constants.HTTP_homegrp_name) ) {
                    sHTTP_homegrp_name = util.null2string(joResult.get(Constants.HTTP_homegrp_name));
                }

                String sHTTP_grade_code = "";
                if ( joResult.has(Constants.HTTP_grade_code) ) {
                    sHTTP_grade_code = util.null2string(joResult.get(Constants.HTTP_grade_code));
                }

                String sHTTP_homehub_device_id = "";
                if ( joResult.has(Constants.HTTP_homehub_device_id) ) {
                    sHTTP_homehub_device_id = util.null2string(joResult.get(Constants.HTTP_homehub_device_id));
                }

                String sHTTP_homehub_conn_state = "";
                if ( joResult.has(Constants.HTTP_device_conn) ) {
                    sHTTP_homehub_conn_state = util.null2string(joResult.get(Constants.HTTP_device_conn));
                }

                String sHTTP_homehub_dongle_state = "";
                if ( joResult.has(Constants.HTTP_device_online) ) {
                    sHTTP_homehub_dongle_state = util.null2string(joResult.get(Constants.HTTP_device_online));
                }

                /*String sHTTP_homehub_online_state = "";
                if ( joResult.has(Constants.HTTP_homehub_online_state) ) {
                    sHTTP_homehub_online_state = util.null2string(joResult.get(Constants.HTTP_homehub_online_state));
                }*/

                appConfig.setLogin_Member_Id(sMEMBER_ID);
                appConfig.setLogin_Id(sEMAIL_ID);
                appConfig.setLogin_Pass("");
                Dlog.v("bAutoLogin =" + bAutoLogin);
                appConfig.setAutoLogin(bAutoLogin);
                appConfig.setLogin(true);

                //공통 변수 입력
                appConfig.setAuth_Token(sAUTH_TOKEN);

                //db 입력작업
                SQLiteDatabase db = DatabaseOpenHelper.getInstance(act).getWritableDatabase();
                String sSql = "";
                sSql = "";
                sSql = sqlManager.QryDelLoginInfo();
                db.execSQL(sSql);

                sSql = "";
                Dlog.v("bAutoLogin =" + bAutoLogin);
                sSql = sqlManager.QryInsertLoginInfo(sEMAIL_ID, sAUTH_TOKEN, bSaveId, bAutoLogin, appConfig.getManualNotView(), appConfig.getPush());
                db.execSQL(sSql);
                db.close();

                appConfig.setPush_Type_Mode_Chg_Yn(sPUSH_TYPE_MODE_CHG_YN);
                appConfig.setPush_Type_Prsnc_Chg_Yn(sPUSH_TYPE_PRSNC_CHG_YN);
                appConfig.setInvitations_Count(sHTTP_invitations_count);
                appConfig.setSelect_Homegrp_Id(sHTTP_homegrp_id);
                appConfig.setSelect_Homegrp_Icon(sHTTP_homegrp_image_file_name);
                appConfig.setSelect_Homegrp_Name(sHTTP_homegrp_name);
                appConfig.setSelect_Homegrp_Grade_Code(sHTTP_grade_code);
                appConfig.setSelect_Homegrp_Homehub_Device_Id(sHTTP_homehub_device_id);
                appConfig.setsSelect_Homegrp_Homehub_Conn_State(sHTTP_homehub_conn_state);
                appConfig.setsSelect_Homegrp_Homehub_Dongle_State(sHTTP_homehub_dongle_state);
//                appConfig.setSelect_Homegrp_Homehub_Online_State(sHTTP_homehub_online_state);

                loading = false;
                swipeRefreshLayout.setRefreshing(false);
                setDisp();
            } 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 {
                    customDialog.MsgAlert(act, Constants.MSG000, sERR_MSG);
                }
            }
        } 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.MSG805:
                if ( iBtnResult == 1 ) {
                    util.callApp_Start(act, "LOGOUT");
                }
                break;
            case Constants.MSG807:
                if ( iBtnResult == 1 ) {
                    ComHandler.sendMessageDelayed(ComHandler.obtainMessage(iType), 10);
                }
                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_ADMIN_PASSWORD_CHK:
                    Intent intent = new Intent(act, Home_Start.class);
                    util.callActivityForResultNoAnim(act, intent, Constants.ACT_HOME_START);
                    break;
                case Constants.ACT_HOME_START:
                    util.callApp_Start(act, "HOME_RELOAD");
                    break;
                case Constants.ACT_SETTING_PASSWORD_CHK:
                case Constants.ACT_SETTING_NAME_CHANGE:
                case Constants.ACT_SETTING_EMAIL_CHANGE:
                    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;
            }
        }
    }
}
