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

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

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.ntels.kneet.v3.R;
import com.ntels.kneet.v3.common.AppConfig;
import com.ntels.kneet.v3.common.Constants;
import com.ntels.kneet.v3.common.c_interface.List_Interface;
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.http.HttpSend;
import com.ntels.kneet.v3.common.http.Response;
import com.ntels.kneet.v3.common.util.Dlog;
import com.ntels.kneet.v3.common.util.Util;
import com.ntels.kneet.v3.common.util.recyclerview.DividerItemDecoration;
import com.ntels.kneet.v3.view.dialog.CustomDialog;
import com.ntels.kneet.v3.view.dialog.Dialog_Custom_List;

import org.json.JSONObject;

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

import jp.wasabeef.recyclerview.adapters.AlphaInAnimationAdapter;
import jp.wasabeef.recyclerview.adapters.ScaleInAnimationAdapter;

public class Fragment_HomeMember extends Fragment implements View.OnClickListener,
                                                             CustomDialog.OnMsgAlterToResult,
                                                             List_Interface {

    /*툴바 관련*/
    private Toolbar toolbar;
    private int iMemberCnt     = 0;
    private int iInvitationCnt = 0;

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

    /*리스트 관련*/
    private Fragment_HomeMember_Adapter dataAdapter;
    private RecyclerView                rcvList;
    private SwipeRefreshLayout          swipeRefreshLayout;
    private GridLayoutManager           glmList;
    private LinearLayoutManager         llmList;
    private Dialog_Custom_List.Builder  dialog_list_builder;
    private boolean loading         = false;
    private boolean bListHeader     = false;
    private boolean bListFooter     = false;
    private boolean bListFooterAnd  = false;
    private int     iListColumnsCnt = 1;

    /*공통*/
    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 String       sActName     = "";

    //    @Override public void onAttach(Context context) {
    //
    //        super.onAttach(context);
    //
    //        Activity activity;
    //
    //        if ( context instanceof Activity ) {
    //            activity = (Activity) context;
    //            try {
    //                mCallback = (Main_Interface) activity;
    //            } catch (ClassCastException e) {
    //                throw new ClassCastException(activity.toString() + " must implement Main_Interface");
    //            }
    //        }
    //    }

    @Override public void onAttach(Activity activity) {

        super.onAttach(activity);

        try {
            mCallback = (Main_Interface) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement Main_Interface");
        }
    }
    /**
     * <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);
        Dlog.v("onCreate()");
    }

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        Dlog.v("onCreateView()");
        View rootView = inflater.inflate(R.layout.fragment_homemember, container, false);

        return rootView;
    }

    @Override public void onStop() {

        super.onStop();
        Dlog.v("onStop()");
        GoogleAnalytics.getInstance(act).reportActivityStop(act);
    }

    @Override public void onStart() {

        super.onStart();
        Dlog.v("onStart()");
        GoogleAnalytics.getInstance(act).reportActivityStart(act);
    }
    @Override public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        Dlog.v("onActivityCreated().");
        Dlog.v(savedInstanceState == null ? "true" : "false");

        act = getActivity();
        sActName = "HomeMember_List";
        appConfig = (AppConfig) act.getApplicationContext();
        customDialog.setOnMsgAlterToResultEvent(this);

        //        Tracker t = appConfig.getTracker(AppConfig.TrackerName.APP_TRACKER);
        //        t.setScreenName(act.getClass().getSimpleName());
        //        t.send(new HitBuilders.AppViewBuilder().build());
        util.callActGoogleAnalytics(sActName);
        setToolbar();

        displayListView();
    }

    private void setToolbar() {

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

        util.getTextView(toolbar, R.id.tvToolbarTitle1).setText(util.getString(act, R.string.layout_join));
        util.getTextView(toolbar, R.id.tvToolbarTitle2).setText("");
        util.getTextView(toolbar, R.id.tvToolbarTitle3).setText(util.getString(act, R.string.layout_wait));
        util.getTextView(toolbar, R.id.tvToolbarTitle4).setText("");

        util.getView(toolbar, R.id.btnToolbarDel).setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {

                v.setEnabled(false);
                Boolean bCheck = false;
                for ( int iRow = 0; iRow < alTotalList.size(); iRow++ ) {
                    if ( alTotalList.get(iRow).get(Constants.HTTP_CHECK_YN).equals("Y") ) {
                        bCheck = true;
                        break;
                    }
                }
                if ( bCheck ) {
                    loading = true;
                    ComHandler.sendMessageDelayed(ComHandler.obtainMessage(MSG_COM_D_HOMEGRP_MEMBERS), 10);
                } else {
                    util.setShowToast(act, util.getString(act, R.string.MSG814), Toast.LENGTH_SHORT);
                    v.setEnabled(true);
                }
            }
        });
        util.getView(toolbar, R.id.btnToolbarClose).setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {

                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.VISIBLE);
                for ( int iRow = 0; iRow < alTotalList.size(); iRow++ ) {
                    alTotalList.get(iRow).put(Constants.HTTP_CHECK_YN, "N");
                }

                dataAdapter.setListCheck(false);
                dataAdapter.notifyDataSetChanged();
                setRefresh(true);
            }
        });
        util.getView(toolbar, R.id.btnToolbarMore).setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {

                ArrayList<HashMap<String, Object>> alMenuList = new ArrayList<HashMap<String, Object>>();
                alMenuList.add(util.getPopMenu(6));
                if ( appConfig.getSelect_Homegrp_Grade_Code().equals("MAS") ) {
                    alMenuList.add(util.getPopMenu(15));
                    if ( alTotalList.size() > 2 ) {
                        alMenuList.add(util.getPopMenu(16));
                    }
                } else {
                    alMenuList.add(util.getPopMenu(17));
                }
                MsgAlertList(alMenuList);
            }
        });
    }

    private void displayListView() {

        util.getView(act, R.id.llNodata_Master).setOnClickListener(this);
        util.getView(act, R.id.llNoHub_Master).setOnClickListener(this);
        util.getViewUnderline(act, R.id.tvWidthdraw);
        util.getView(act, R.id.tvWidthdraw).setOnClickListener(this);
        util.getViewUnderline(act, R.id.tvConnect);

        swipeRefreshLayout = (SwipeRefreshLayout) getView().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 ( !dataAdapter.getListCheck() ) {
                    if ( !loading ) {
                        onListReload();
                    }
                } else {
                    swipeRefreshLayout.setRefreshing(false);
                }
            }
        });
        setRefresh(true);

        bListFooter = true;
        dataAdapter = new Fragment_HomeMember_Adapter(act, R.layout.row_homemember_list, R.layout.row_homemember_list, R.layout.row_footer_homemember_list, alTotalList, this);
        dataAdapter.setFooterCheck(bListFooter);

        rcvList = (RecyclerView) getView().findViewById(R.id.rcvList);
        rcvList.setHasFixedSize(true);
        iListColumnsCnt = 2;
        if ( iListColumnsCnt == 1 ) {
            llmList = new LinearLayoutManager(act);
            rcvList.setLayoutManager(llmList);
        } else {
            glmList = new GridLayoutManager(act, iListColumnsCnt, GridLayoutManager.VERTICAL, false);
            glmList.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override public int getSpanSize(int position) {

                    int iPositionType = dataAdapter.getItemViewType(position);
                    int iTotalConut = dataAdapter.getItemCount();
                    int iValueConut = dataAdapter.getItemCount();
                    if ( bListHeader ) {
                        iValueConut = iTotalConut - 1;
                    }

                    if ( iPositionType == dataAdapter.TYPE_HEADER ) {
                        return glmList.getSpanCount();
                    } else {
                        return dataAdapter.getItemViewType(position) == 1 ? 1 : (iValueConut % iListColumnsCnt == 1 ? glmList.getSpanCount() : 1);
                    }
                }
            });
            rcvList.setLayoutManager(glmList);
        }
        rcvList.addItemDecoration(new DividerItemDecoration(getActivity(), R.drawable.list_color_line_01));
        //        rcvList.setItemAnimator(new FadeInAnimator());
        //        rcvList.setAdapter(dataAdapter);
        AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(dataAdapter);
        alphaAdapter.setDuration(150);
        ScaleInAnimationAdapter scaleAdapter = new ScaleInAnimationAdapter(alphaAdapter);
        scaleAdapter.setDuration(150);
        rcvList.setAdapter(scaleAdapter);

        setHomeHubStateTitle();
        onListReload();
    }

    public void setHomeHubStateAll() {

        setHomeHubStateTitle();
        setHomeHubStateList();
    }

    public void setHomeHubStateTitle() {

        if ( appConfig.getSelect_Homegrp_Id().equals("") ) {
            //홈그룹 없을경우
            util.getView(toolbar, R.id.llToolbar).setVisibility(View.VISIBLE);
            util.getView(toolbar, R.id.llToolbarHomeHub).setVisibility(View.GONE);
            util.getView(toolbar, R.id.btnToolbarMore).setVisibility(View.GONE);
            util.getView(toolbar, R.id.btnToolbarClose).setVisibility(View.GONE);
            if ( dialog_list_builder != null ) {
                dialog_list_builder.create().dismiss();
            }
        } else {
            //홈그룹 있을경우
            if ( appConfig.getSelect_Homegrp_Homehub_Device_Id().equals("") ) {
                //홈그룹은 있고 홈허브가 없는경우
                util.getView(toolbar, R.id.llToolbar).setVisibility(View.GONE);
                util.getView(toolbar, R.id.llToolbarHomeHub).setVisibility(View.VISIBLE);
                util.getTextView(toolbar, R.id.tvToolbarHomeHub).setText(util.getString(act, R.string.layout_homehub_delete));
                util.getView(toolbar, R.id.btnToolbarMore).setVisibility(View.GONE);
                util.getView(toolbar, R.id.btnToolbarClose).setVisibility(View.GONE);
                if ( dialog_list_builder != null ) {
                    dialog_list_builder.create().dismiss();
                }
            } else {
                //                util.getView(toolbar, R.id.btnToolbarMore).setVisibility(View.VISIBLE);
                if ( appConfig.getSelect_Homegrp_Homehub_Online_State().equals("OFF") ) {
                    //홈그룹은 있고 홈허브가 오프라인인경우
                    util.getView(toolbar, R.id.llToolbar).setVisibility(View.GONE);
                    util.getView(toolbar, R.id.llToolbarHomeHub).setVisibility(View.VISIBLE);
                    util.getTextView(toolbar, R.id.tvToolbarHomeHub).setText(util.getString(act, R.string.layout_homehub_offline));
                    //                    util.getView(toolbar, R.id.btnToolbarClose).setVisibility(View.GONE);
                } else {
                    //홈그룹은 있고 홈허브가 온라인인 경우
                    util.getView(toolbar, R.id.llToolbar).setVisibility(View.VISIBLE);
                    util.getView(toolbar, R.id.llToolbarHomeHub).setVisibility(View.GONE);
                }
                // 리스트가 2개 이하일경우에는 본인이 마스터인경우와 푸터랑 해서 2Row이기 때문에 삭제 상태를 다시 일반상태로 변환함
                if ( alTotalList.size() <= 2 ) {
                    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.VISIBLE);
                }
            }
        }
    }

    public boolean setHomeHubStateList() {

        //        Dlog.v("Things setHomeHubStateList()");
        Boolean bListLoad = false;
        swipeRefreshLayout.setVisibility(View.GONE);
        util.getView(act, R.id.llNoHub_Master).setVisibility(View.GONE);
        util.getView(act, R.id.llNoHub_Guest).setVisibility(View.GONE);
        util.getView(act, R.id.llNodata_Master).setVisibility(View.GONE);
        util.getView(act, R.id.llNodata_Guest).setVisibility(View.GONE);

        util.getView(act, R.id.llNoHub_NoHome_Smartcity).setVisibility(View.GONE);
        util.getView(act, R.id.llNoHub_Master_Smartcity).setVisibility(View.GONE);
        util.getView(act, R.id.llNoHub_Guest_Smartcity).setVisibility(View.GONE);
        util.getView(act, R.id.llNodata_Master_Smartcity).setVisibility(View.GONE);
        util.getView(act, R.id.llNodata_Guest_Smartcity).setVisibility(View.GONE);
        if ( appConfig.getSelect_Homegrp_Id().equals("") ) {
            //홈그룹 없을경우
            util.getView(act, R.id.llNoHub_Master).setVisibility(View.VISIBLE);
            dataAdapter.setListCheck(false);
            dataAdapter.notifyDataSetChanged();
            setRefresh(false);
        } else {
            //홈그룹 있을경우
            if ( appConfig.getSelect_Homegrp_Homehub_Device_Id().equals("") ) {
                //홈그룹은 있고 홈허브가 없는경우
                if ( appConfig.getSelect_Homegrp_Grade_Code().equals("MAS") ) {
                    util.getView(act, R.id.llNoHub_Master).setVisibility(View.VISIBLE);
                    dataAdapter.setListCheck(false);
                    dataAdapter.notifyDataSetChanged();
                    setRefresh(false);
                } else {
                    util.getView(act, R.id.llNoHub_Guest).setVisibility(View.VISIBLE);
                    dataAdapter.setListCheck(false);
                    dataAdapter.notifyDataSetChanged();
                    setRefresh(false);
                }
            } else {
                //                if ( alTotalList.size() > 0 ) {
                swipeRefreshLayout.setVisibility(View.VISIBLE);
                // 리스트가 2개 이하일경우에는 본인이 마스터인경우와 푸터랑 해서 2Row이기 때문에 삭제 상태를 다시 일반상태로 변환함
                if ( alTotalList.size() <= 2 ) {
                    dataAdapter.setListCheck(false);
                    setRefresh(true);
                }
                //                } else {
                //                    if ( !loading ) {
                //                        if ( appConfig.getSelect_Homegrp_Grade_Code().equals("MAS") ) {
                //                            util.getView(act, R.id.llNodata_Master).setVisibility(View.VISIBLE);
                //                            util.getTextView(act, R.id.tvNodata_Master_Title).setText(R.string.layout_nodata_desc_06);
                //                        } else {
                //                            util.getView(act, R.id.llNodata_Guest).setVisibility(View.VISIBLE);
                //                            util.getTextView(act, R.id.tvNodata_Guest_Title).setText(R.string.layout_nodata_desc_03);
                //                        }
                //                    }
                //                    dataAdapter.setListCheck(false);
                //                }
                if ( appConfig.getSelect_Homegrp_Grade_Code().equals("MAS") ) {
                    dataAdapter.setFooterVisible(true);
                } else {
                    dataAdapter.setFooterVisible(false);
                }
                dataAdapter.notifyDataSetChanged();
                bListLoad = true;
            }
        }
        return bListLoad;
    }

    private void setRefresh(boolean bVal) {

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

    public void HomeMember_Add() {

        Intent intent = new Intent(act, HomeMember_Add.class);
        util.callActivityForResult(act, intent, Constants.ACT_HOMEMEMBER_ADD);
    }

    @Override public void onClick(View v) {

        switch (v.getId()) {
            case R.id.llNoHub_Master:
                mCallback.onHomehubStart();
                break;
            case R.id.tvWidthdraw:
                mCallback.onHomeMemberWidthdraw();
                break;
            case R.id.llNodata_Master:
                HomeMember_Add();
                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_HOMEGRP_MEMBERS:
                    send_G_homegrp_members();
                    break;
                case MSG_COM_D_HOMEGRP_MEMBERS:
                    send_D_homegrp_members();
                    break;
                default:
                    break;
            }
            return true;
        }
    }

    private void send_D_homegrp_members() {

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

                    result_D_homegrp_members(result);
                }
            });

            String sTemp = "";
            String sTemp1 = "";
            for ( int iRow = 0; iRow < alTotalList.size(); iRow++ ) {
                Dlog.v("alTotalList.get(iRow).get(Constants.HTTP_CHECK_YN) = " + alTotalList.get(iRow).get(Constants.HTTP_CHECK_YN));
                if ( alTotalList.get(iRow).get(Constants.HTTP_CHECK_YN).equals("Y") ) {
                    if ( alTotalList.get(iRow).get(Constants.HTTP_member_type_code).equals("01") ) {
                        sTemp += (sTemp.equals("") ? "" : ",") + alTotalList.get(iRow).get(Constants.HTTP_member_id);
                    } else {
                        sTemp1 += (sTemp1.equals("") ? "" : ",") + alTotalList.get(iRow).get(Constants.HTTP_invitation_hist_id);
                    }
                }
            }

            httpSend.D_homegrp_members(sTemp, sTemp1);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void result_D_homegrp_members(Response rpResult) {

        loading = false;
        try {
            if ( rpResult.getStatusCode() == 200 ) {
                util.setShowToast(act, util.getString(act, R.string.MSG880), Toast.LENGTH_SHORT);

                onListReload();
            } 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 {
                    util.setShowToast(act, sERR_MSG, Toast.LENGTH_SHORT);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.MsgAlert(act, Constants.MSG807);
        }
        try {
            util.getView(toolbar, R.id.btnToolbarDel).setEnabled(true);
        } catch (Exception ex) {

        }
    }

    private void send_G_homegrp_members() {

        try {
            HttpSend httpSend;

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

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

                    result_G_homegrp_members(result);
                }
            });
            alTotalList.clear();
            swipeRefreshLayout.setVisibility(View.GONE);
            httpSend.G_homegrp_members();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void result_G_homegrp_members(Response rpResult) {

        loading = false;
        try {
            if ( rpResult.getStatusCode() == 200 ) {

                JSONObject joResult = new JSONObject(rpResult.getContent());
                String sHTTP_homegrp_member_list = util.null2string(joResult.get(Constants.HTTP_homegrp_member_list));
                ArrayList<HashMap<String, Object>> alList = util.jsonToArrayListMap(sHTTP_homegrp_member_list);
                iMemberCnt = 0;
                iInvitationCnt = 0;
                if ( alList.size() > 0 ) {
                    for ( int iRow = 0; iRow < alList.size(); iRow++ ) {
                        HashMap<String, Object> hmTemp = new HashMap<String, Object>();
                        hmTemp = alList.get(iRow);

                        String sHTTP_invitation = util.null2string(hmTemp.get(Constants.HTTP_invitation));
                        hmTemp.put(Constants.HTTP_CHECK_YN, "N");
                        alTotalList.add(hmTemp);

                        if ( sHTTP_invitation.equals("11") ) {
                            iMemberCnt++;
                        } else if ( sHTTP_invitation.equals("01") ) {
                            iInvitationCnt++;
                        }
                    }

                    HashMap<String, Object> hmTemp = new HashMap<String, Object>();
                    hmTemp.put(Constants.HTTP_CHECK_YN, "N");
                    alTotalList.add(hmTemp);
                }
                util.getTextView(toolbar, R.id.tvToolbarTitle2).setText(iMemberCnt + "");
                util.getTextView(toolbar, R.id.tvToolbarTitle4).setText(iInvitationCnt + "");

                setHomeHubStateAll();
                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 {
                    customDialog.MsgAlert(act, Constants.MSG000, sERR_MSG);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.MsgAlert(act, Constants.MSG807);
        }
    }

    @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);
                } else {
                    act.onBackPressed();
                }
                break;
            default:
                break;
        }
    }

    private void MsgAlertList(ArrayList<HashMap<String, Object>> alList) {

        try {
            dialog_list_builder = new Dialog_Custom_List.Builder(act);
            dialog_list_builder.setList(alList);
            dialog_list_builder.setPositiveButton(new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    Dlog.v("which = " + which);
                    dialog.dismiss();
                    switch (which) {
                        case 1:
                            break;
                        case 2:
                            break;
                        case 3:
                            break;
                        case 4:
                            break;
                        case 5:
                            break;
                        case 6:
                            onListReload();
                            break;
                        case 7:
                            break;
                        case 8:
                            break;
                        case 9:
                            break;
                        case 10:
                            break;
                        case 11:
                            break;
                        case 12:
                            break;
                        case 15:
                            Intent intent = new Intent(act, HomeMember_Add.class);
                            util.callActivityForResult(act, intent, Constants.ACT_HOMEMEMBER_ADD);
                            break;
                        case 16:
                            util.getTextView(toolbar, R.id.btnToolbarDel).setVisibility(View.VISIBLE);
                            util.getTextView(toolbar, R.id.btnToolbarClose).setVisibility(View.VISIBLE);
                            util.getTextView(toolbar, R.id.btnToolbarMore).setVisibility(View.GONE);
                            for ( int iRow = 0; iRow < alTotalList.size(); iRow++ ) {
                                alTotalList.get(iRow).put(Constants.HTTP_CHECK_YN, "N");
                            }

                            dataAdapter.setListCheck(true);
                            dataAdapter.notifyDataSetChanged();
                            setRefresh(false);

                            break;
                        case 17:
                            mCallback.onHomeMemberWidthdraw();
                            break;
                    }
                }
            });
            dialog_list_builder.setNegativeButton(new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    Dlog.v("which = " + which);
                    dialog.dismiss();
                }
            });
            dialog_list_builder.create().show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

        super.onActivityResult(requestCode, resultCode, data);
        Boolean bReload_Flag = false;
        Dlog.v("jino1111");
        if ( resultCode == act.RESULT_OK ) {
            switch (requestCode) {
                case Constants.ACT_HOMEMEMBER_DETAIL:
                case Constants.ACT_HOMEMEMBER_ADD:
                    bReload_Flag = data.getBooleanExtra(Constants.EXTRA_RELOAD_FLAG, false);
                    if ( bReload_Flag ) {
                        onListReload();
                    }
                    break;
            }
        } else {
            switch (requestCode) {
                case Constants.ACT_HOMEMEMBER_DETAIL:
                case Constants.ACT_HOMEMEMBER_ADD:
                    bReload_Flag = data.getBooleanExtra(Constants.EXTRA_RELOAD_FLAG, false);
                    if ( bReload_Flag ) {
                        onListReload();
                    }
                    break;
            }
            // if (appConfig.getGoErr()) {
            // act.setResult(act.RESULT_CANCELED, new Intent());
            // finish();
            // }
        }
    }

    @Override public void onListReload() {

        loading = true;
        //sSearchPlan = "U";
        //sDateTime = sMaxDatetime;

        if ( setHomeHubStateList() ) {
            ComHandler.sendMessageDelayed(ComHandler.obtainMessage(MSG_COM_G_HOMEGRP_MEMBERS), 10);
        }
        // do something
    }

    @Override public void onLogout() {
        //        mCallback.onLogout();
    }
    @Override public void onCallAction(int iCallType, int iPosition) {

        if ( iCallType == 0 ) {
            //추가하기
            HomeMember_Add();
        } else if ( iCallType == 1 ) {
            // 상세보기
        } else if ( iCallType == 2 ) {
            // 삭제하기
        }
    }
}
