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

package com.ntels.onecable.view.mainmore;

import com.ntels.onecable.R;
import com.ntels.onecable.common.AppConfig;
import com.ntels.onecable.common.Constants;
import com.ntels.onecable.common.c_interface.OnDataSendToReslut;
import com.ntels.onecable.common.db.DatabaseOpenHelper;
import com.ntels.onecable.common.db.SqlManager;
import com.ntels.onecable.common.http.HttpSend;
import com.ntels.onecable.common.http.Response;
import com.ntels.onecable.common.util.Dlog;
import com.ntels.onecable.common.util.Util;
import com.ntels.onecable.common.widget.CustomActionBarActivity;
import com.ntels.onecable.view.HomeStart;
import com.ntels.onecable.view.ImageSelect;
import com.ntels.onecable.view.dialog.CustomDialog;

import org.json.JSONObject;

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;

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

    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 static final int MSG_COM_D_MEMBER = 4;

    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;
    Handler comHandler = new Handler(new IncomingHandlerCallBack());
    /*통신 관련*/
    private int type = 0;
    /*공통*/
    private Activity act;
    private AppConfig appConfig;
    private Intent intent;
    private Util util = new Util();
    private CustomDialog customDialog = new CustomDialog();
    private SqlManager mSqlManager = new SqlManager();
    private Boolean isAutoLogin = false;
    private Boolean isSaveId = false;
    private String fileName = "";
    private String pushTypeModeChgYn = "";
    private String pushTypeRuleExcYn = "";
    private String newEmailId = "";
    private int alarmType = 0;
    private SwipeRefreshLayout swipeRefreshLayout;
    private boolean loading = false;

    @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);

        setToolbar();
        displayListView();
    }

    /**
     * 툴바 셋팅.
     */
    private void setToolbar() {

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

    /**
     * UI display.
     */
    private void setDisp() {

        util.getViewUnderline(act, R.id.tvNameChange);
        util.getViewUnderline(act, R.id.tvPassChange);
        util.getViewUnderline(act, R.id.tvEmailChange);

        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.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);
        util.getView(act, R.id.btnWithdrawal).setOnClickListener(this);

        String url = appConfig.getMy_Image_Url();
        util.setRefreshImage(act, url, 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));

        isAutoLogin = appConfig.getAutoLogin();

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

        pushTypeModeChgYn = 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);
        }

        pushTypeRuleExcYn = 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 =", newEmailId);
        if (newEmailId.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(newEmailId + " "
                    + 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 view) {

        switch (view.getId()) {
            case R.id.llBtn1:
                onBackPressed();
                break;
            case R.id.rlMemberImage:
                intent = new Intent(act, ImageSelect.class);
                intent.putExtra(Constants.EXTRA_CROP_TYPE, true);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_IMAGE_SELECT);
                break;
            case R.id.llAutoLogin:
                if (view.isSelected()) {
                    view.setSelected(false);
                } else {
                    view.setSelected(true);
                }
                isAutoLogin = view.isSelected();
                appConfig.setAutoLogin(isAutoLogin);
                //db 입력작업
                SQLiteDatabase db = DatabaseOpenHelper.getInstance(act).getWritableDatabase();
                String sql = "";
                sql = "";
                sql = mSqlManager.qryUpdateLoginInfoAutoLogin(isAutoLogin);
                db.execSQL(sql);
                db.close();
                break;
            case R.id.btnLogout:
                customDialog.msgAlert(act, Constants.MSG805);
                break;
            case R.id.tvNameChange:
                intent = new Intent(act, SettingNameChange.class);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_SETTING_NAME_CHANGE);
                break;
            case R.id.tvEmailChange:
                intent = new Intent(act, SettingEmailChange.class);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_SETTING_EMAIL_CHANGE);
                break;
            case R.id.tvPassChange:
                intent = new Intent(act, SettingPasswordCheck.class);
                intent.putExtra(Constants.EXTRA_CALL_TYPE, "1");
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_SETTING_PASSWORD_CHK);
                break;
            case R.id.llAlramHomeMode:
                view.setEnabled(false);
                if (view.isSelected()) {
                    pushTypeModeChgYn = "N";
                    alarmType = ALRAM_MODE_OFF;
                } else {
                    pushTypeModeChgYn = "Y";
                    alarmType = ALRAM_MODE_ON;
                }
                comHandler.sendMessageDelayed(comHandler.obtainMessage(MSG_COM_P_MEMBER_PUSH_NOTI), 10);
                break;
            case R.id.llAlramHouse:
                view.setEnabled(false);
                if (view.isSelected()) {
                    pushTypeRuleExcYn = "N";
                    alarmType = ALRAM_RULE_OFF;
                } else {
                    pushTypeRuleExcYn = "Y";
                    alarmType = 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.btnWithdrawal:
                // 홈그룹의 마스터이며 홈허브가 등록되어있으면 홈허브를 먼저 삭제하라는 메세지 노출.
                // 회원탈퇴 시 MAS에서 홈허브 삭제가 자동으로 이루어지지 않음.
                if (appConfig.getSelect_Homegrp_Grade_Code().equalsIgnoreCase("MAS")
                        && !appConfig.getSelect_Homegrp_Homehub_Device_Id().equals("")) {
                    util.setShowToast(act, util.getString(act, R.string.MSG909), Toast.LENGTH_SHORT);
                } else {
                    if (appConfig.getSelect_Homegrp_Grade_Code().equals("MAS")) {
                        customDialog.msgAlert(act, Constants.MSG910);
                    } else {
                        customDialog.msgAlert(act, Constants.MSG911);
                    }
                }
                break;
            default:
                break;
        }
    }

    /**
     * 알림 수신 여부 설정(알림 수신 여부를 설정 한다. (예 : 홈모드가 바뀔 때, 집이 빌 때)) 요청.
     */
    private void sendPutMemberPushNoti() {

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

                    resultPutMemberPushNoti(result);
                }
            });

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

    /**
     * {@link #sendPutMemberPushNoti()} 요청 결과.
     *
     * @param response 결과 data
     */
    private void resultPutMemberPushNoti(Response response) {

        try {
            if (response.getStatusCode() == 200) {
                int tempMsgNum = 0;
                //                Dlog.v(alarmType);
                if (alarmType == ALRAM_MODE_ON) {
                    appConfig.setPush_Type_Mode_Chg_Yn("Y");
                    tempMsgNum = R.string.MSG815;
                } else if (alarmType == ALRAM_MODE_OFF) {
                    appConfig.setPush_Type_Mode_Chg_Yn("N");
                    tempMsgNum = R.string.MSG816;
                } else if (alarmType == ALRAM_RULE_ON) {
                    appConfig.setPush_Type_Prsnc_Chg_Yn("Y");
                    tempMsgNum = R.string.MSG815;
                } else if (alarmType == ALRAM_RULE_OFF) {
                    appConfig.setPush_Type_Prsnc_Chg_Yn("N");
                    tempMsgNum = R.string.MSG816;
                }
                util.setShowToast(act, util.getString(act, tempMsgNum), Toast.LENGTH_SHORT);
                setDisp();
            } else {
                JSONObject joResult = new JSONObject(response.getContent());
                Dlog.v(joResult.toString());
                String errorCode = util.null2string(joResult.get(Constants.KEY_ERR_CODE));
                String errorMsg = util.null2string(joResult.get(Constants.KEY_ERR_MSG));

                if (errorCode.equals("401")) {
                    customDialog.msgAlert(act, Constants.MSG998, errorMsg);
                } else if (errorCode.equals("401-106")) {
                    customDialog.msgAlert(act, Constants.MSG997, errorMsg);
                } else {
                    customDialog.msgAlert(act, Constants.MSG000, errorMsg);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.msgAlert(act, Constants.MSG807);
        }
    }

    /**
     * 멤버 이미지 변경(멤버의 프로필 이미지를 수정) 요청.
     */
    private void sendPostMemberProfileImage() {

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

                    resultPostMemberProfileImage(result);
                }
            });

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

    /**
     * {@link #sendPostMemberProfileImage()} 요청 결과.
     *
     * @param response 결과 data
     */
    private void resultPostMemberProfileImage(Response response) {

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

                //회원 번호
                fileName = util.null2string(joResult.get(Constants.KEY_IMAGE_FILE_NAME));
                util.setRefreshImage(act, fileName, R.id.ivMemberIcon);
            } else {
                JSONObject joResult = new JSONObject(response.getContent());
                Dlog.v(joResult.toString());
                String errorCode = util.null2string(joResult.get(Constants.KEY_ERR_CODE));
                String errorMsg = util.null2string(joResult.get(Constants.KEY_ERR_MSG));

                if (errorCode.equals("401")) {
                    customDialog.msgAlert(act, Constants.MSG998, errorMsg);
                } else if (errorCode.equals("401-106")) {
                    customDialog.msgAlert(act, Constants.MSG997, errorMsg);
                } else {
                    customDialog.msgAlert(act, Constants.MSG000, errorMsg);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.msgAlert(act, Constants.MSG807);
        }
    }

    /**
     * <pre>
     * 회원 로그인 정보 조회 및 푸시 토큰 등록/수정 요청.
     * (인증 토큰을 기준으로 회원의 로그인 정보를 조회하고 푸시 토큰을 등록/수정한다)
     * </pre>
     */
    private void sendPutMember() {

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

                    resultPutMember(result);
                }
            });

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

    /**
     * {@link #sendPutMember()} 요청 결과.
     *
     * @param response 결과 data
     */
    private void resultPutMember(Response response) {

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

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

                //회원이미지 추가
                String imageFileName = "";
                if (joResult.has(Constants.KEY_IMAGE_FILE_NAME)) {
                    imageFileName = util.null2string(joResult.get(Constants.KEY_IMAGE_FILE_NAME));
                    appConfig.setMy_Image_Url(imageFileName);
                }
                //회원명
                String nickname = "";
                if (joResult.has(Constants.KEY_NICKNAME)) {
                    nickname = util.null2string(joResult.get(Constants.KEY_NICKNAME));
                    appConfig.setNickname(nickname);
                }

                newEmailId = "";
                if (joResult.has(Constants.KEY_NEW_EMAIL_ID)) {
                    newEmailId = util.null2string(joResult.get(Constants.KEY_NEW_EMAIL_ID));
                }

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

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

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

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

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

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

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

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

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

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

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

                //회원 번호
                String memberId = util.null2string(joResult.get(Constants.KEY_MEMBER_ID));
                appConfig.setLogin_Member_Id(memberId);
                //이메일 ID
                String emailId = util.null2string(joResult.get(Constants.KEY_EMAIL_ID));
                appConfig.setLogin_Id(emailId);
                appConfig.setLogin_Pass("");
                Dlog.v("isAutoLogin =" + isAutoLogin);
                appConfig.setAutoLogin(isAutoLogin);
                appConfig.setLogin(true);

                //인증 토큰
                String authToken = util.null2string(joResult.get(Constants.KEY_AUTH_TOKEN));
                //공통 변수 입력
                appConfig.setAuthToken(authToken);

                //db 입력작업
                SQLiteDatabase db = DatabaseOpenHelper.getInstance(act).getWritableDatabase();
                String sql = "";
                sql = mSqlManager.qryDelLoginInfo();
                db.execSQL(sql);

                Dlog.v("isAutoLogin =" + isAutoLogin);
                sql = mSqlManager.qryInsertLoginInfo(emailId, authToken, isSaveId, isAutoLogin,
                        appConfig.getManualNotView(), appConfig.getPush());
                db.execSQL(sql);
                db.close();

                String pushTypeModeChgYn = util.null2string(
                        joResult.get(Constants.KEY_PUSH_TYPE_MODE_CHG_YN));
                appConfig.setPush_Type_Mode_Chg_Yn(pushTypeModeChgYn);
                String pushTypePrsncChgYn = util.null2string(
                        joResult.get(Constants.KEY_PUSH_TYPE_PRSNC_CHG_YN));
                appConfig.setPush_Type_Prsnc_Chg_Yn(pushTypePrsncChgYn);
                appConfig.setInvitations_Count(invitationsCount);
                appConfig.setCtrtGroupId(homegrpId);
                appConfig.setSelect_Homegrp_Icon(homegrpImageFileName);
                appConfig.setSelect_Homegrp_Name(homegrpName);
                appConfig.setSelect_Homegrp_Grade_Code(gradeCode);
                appConfig.setSelect_Homegrp_Homehub_Device_Id(homehubDeviceId);
                appConfig.setsSelect_Homegrp_Homehub_Conn_State(homehubConnState);
                appConfig.setsSelect_Homegrp_Homehub_Dongle_State(homehubDongleState);
                appConfig.setsSelect_Servicehub_App_Id(servicehubApplicationId);
                appConfig.setsSelect_Homegrp_Homehub_Dongle_Online_Last_Datetime(deviceOnlineLastDatetime);

                loading = false;
                swipeRefreshLayout.setRefreshing(false);
                setDisp();
            } else {
                JSONObject joResult = new JSONObject(response.getContent());
                Dlog.v(joResult.toString());
                String errorCode = util.null2string(joResult.get(Constants.KEY_ERR_CODE));
                String errorMsg = util.null2string(joResult.get(Constants.KEY_ERR_MSG));

                if (errorCode.equals("401")) {
                    customDialog.msgAlert(act, Constants.MSG998, errorMsg);
                } else if (errorCode.equals("401-106")) {
                    customDialog.msgAlert(act, Constants.MSG997, errorMsg);
                } else {
                    customDialog.msgAlert(act, Constants.MSG000, errorMsg);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.msgAlert(act, Constants.MSG807);
        }
    }

    /**
     * 계정을 삭제해 회원 탈퇴를 처리한다.
     */
    private void sendDeleteMember() {

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

                    resultDeleteMember(result);
                }
            });

            //공통 변수 입력
            httpSend.D_member();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * {@link #sendDeleteMember()} 요청 결과.
     *
     * @param response 결과 data
     */
    private void resultDeleteMember(Response response) {
        try {
            if (response.getStatusCode() == 200) {
                JSONObject joResult = new JSONObject(response.getContent());
                Dlog.v(joResult.toString());
                customDialog.msgAlert(act, Constants.MSG908);

            } else {
                JSONObject joResult = new JSONObject(response.getContent());
                Dlog.v(joResult.toString());
                String errorCode = util.null2string(joResult.get(Constants.KEY_ERR_CODE));
                String errorMsg = util.null2string(joResult.get(Constants.KEY_ERR_MSG));

                if (errorCode.equals("401")) {
                    customDialog.msgAlert(act, Constants.MSG998, errorMsg);
                } else if (errorCode.equals("401-106")) {
                    customDialog.msgAlert(act, Constants.MSG997, errorMsg);
                } else {
                    customDialog.msgAlert(act, Constants.MSG000, errorMsg);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.msgAlert(act, Constants.MSG807);
        }
    }

    @Override
    public void onBackPressed() {

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

    @Override
    public void resultCustomDialogResponse(int msgId, int btnResult) {

        switch (msgId) {

            case Constants.MSG998:
                util.callApp_Start(act, "LOGOUT");
                break;
            case Constants.MSG997:
                util.callApp_Start(act, "HOME_RELOAD");
                break;
            case Constants.MSG805:
                if (btnResult == 1) {
                    appConfig.setLogout(true);
                    util.callApp_Start(act, "LOGOUT");
                }
                break;
            case Constants.MSG807:
                if (btnResult == 1) {
                    comHandler.sendMessageDelayed(comHandler.obtainMessage(type), 10);
                }
                break;
            case Constants.MSG910:
            case Constants.MSG911:
                if (btnResult == 1) {
                    comHandler.sendMessageDelayed(comHandler.obtainMessage(MSG_COM_D_MEMBER), 10);
                }
                break;
            case Constants.MSG908:
                util.callApp_Start(act, "LOGOUT");
                break;
            default:
                break;
        }
    }

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

        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Constants.ACT_SETTING_ADMIN_PASSWORD_CHK:
                    Intent intent = new Intent(act, HomeStart.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:
                    fileName = util.null2string(data.getStringExtra(Constants.EXTRA_IMAGE_FILE_NAME));
                    comHandler.sendMessageDelayed(comHandler.obtainMessage(
                            MSG_COM_P_MEMBER_PROFILE_IMAGE), 10);
                    break;
                default:
                    break;
            }
        } else {
            switch (requestCode) {
                default:
                    break;
            }
        }
    }

    class IncomingHandlerCallBack implements Handler.Callback {

        @Override
        public boolean handleMessage(Message msg) {

            type = msg.what;
            switch (msg.what) {
                case MSG_COM_G_MEMBER:
                    sendPutMember();
                    break;
                case MSG_COM_P_MEMBER_PROFILE_IMAGE:
                    sendPostMemberProfileImage();
                    break;
                case MSG_COM_P_MEMBER_PUSH_NOTI:
                    sendPutMemberPushNoti();
                    break;
                case MSG_COM_D_MEMBER:
                    sendDeleteMember();
                    break;
                default:
                    break;
            }
            return true;
        }
    }
}
