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

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

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ntels.kneet.v3.common.AppConfig;
import com.ntels.kneet.v3.common.Constants;
import com.ntels.kneet.v3.common.db.SQLManager;
import com.ntels.kneet.v3.common.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.dialog.Dialog_Spinner;

import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;

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

    private Boolean       bDeviceDelYn  = false;
    private DecimalFormat decimalFormat = new DecimalFormat("00");
    LinearLayoutManager mLayoutManager;
    int                 firstVisibleItem, visibleItemCount, totalItemCount;
    private String                             sHTTP_homegrp_rule_id = "";
    private String                             sHTTP_pred_rule_id    = "";
    private String                             sRuleUse_Yn           = "N";
    private String                             sRuleUse_Yn_Text      = "";
    private String                             sHTTP_rule_name       = "";
    private String                             sTempText1            = "ON";
    private String                             sTempText2            = "OFF";
    private Boolean                            bSend                 = false;
    private ArrayList<HashMap<String, Object>> alList_Conditions     = new ArrayList<HashMap<String, Object>>();
    private ArrayList<HashMap<String, Object>> alList_Actions        = new ArrayList<HashMap<String, Object>>();
    private ArrayList<HashMap<String, Object>> alList_Triggers       = new ArrayList<HashMap<String, Object>>();
    private ArrayList<HashMap<String, Object>> alList_Triggers1      = new ArrayList<HashMap<String, Object>>();
    //    private String                             sCallType         = "";
    String sTRIGGERS;
    String sACTIONS;
    String sCONDITIONS;
    String sCustomJason        = "";
    String sRules_Detail_Jason = "";
    private ArrayList<String> alModeId      = new ArrayList<String>();
    private ArrayList<String> alModeTitle   = new ArrayList<String>();
    private ArrayList<String> alModeResult  = new ArrayList<String>();
    private String            sMode         = "";
    private String            sMode_Code    = "";
    private int               iModePosition = -1;
    private int               iModeType     = 2;
    //    private SwipeRefreshLayout swipeRefreshLayout;
    private boolean           loading       = false;
    private Boolean           bReloadFlag   = false;

    //    private View callChildView;

    private TextView tvTempBody;

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

    /*통신 관련*/
    private              ArrayList<HashMap<String, Object>> alTotalList                     = new ArrayList<HashMap<String, Object>>();
    private              int                                iType                           = 0;
    private static final int                                MSG_COM_G_RULES_HOMEGRP_RULE_ID = 1;
    private static final int                                MSG_COM_G_MODES                 = 2;
    private static final int                                MSG_COM_P_RULES                 = 3;
    private static final int                                MSG_COM_P_RULES_HOMEGRP_RULE_ID = 4;

    /*공통*/
    private Activity       act;
    private AppConfig      appConfig;
    private Main_Interface mCallback;
    private Intent         intent;
    private              Util         util                        = new Util();
    private              CustomDialog customDialog                = new CustomDialog();
    private              String       sCallType                   = "";
    private              SQLManager   sqlManager                  = new SQLManager();
    private              Boolean      bAutoLogin                  = false;
    private              Boolean      bSaveId                     = false;
    private              String       sFileName                   = "";
    private              String       sHTTP_push_type_mode_chg_yn = "";
    private              String       sHTTP_push_type_rule_exc_yn = "";
    private              int          iAlram_Type                 = 0;
    private 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;

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

        View viewToLoad = LayoutInflater.from(act).inflate(R.layout.rules_edit, 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();
            //EXTRA_CALL_TYPE = 1:규칙 만들기 2:규칙수정
            sCallType = intent.getStringExtra(Constants.EXTRA_CALL_TYPE);
            sHTTP_homegrp_rule_id = intent.getStringExtra(Constants.EXTRA_MEMBER_RULE_ID);
        }

        setToolbar();
        setDisp();
    }

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

        if ( sCallType.equals("1") ) {
            util.getTextView(toolbar, R.id.tvToolbarTitle1).setText(util.getString(act, R.string.layout_rules_edit_01));
        } else if ( sCallType.equals("2") ) {
            util.getTextView(toolbar, R.id.tvToolbarTitle1).setText(util.getString(act, R.string.layout_rules_edit_10));
        }
    }

    private void setDisp() {

        util.getView(act, R.id.llTriggerTitle).setOnClickListener(this);
        util.getView(act, R.id.llActionTitle).setOnClickListener(this);
        util.getView(act, R.id.llAlramTitle).setOnClickListener(this);
        util.getView(act, R.id.llConditionTitle).setOnClickListener(this);
        util.getView(act, R.id.llConditionBody1Title).setOnClickListener(this);
        util.getView(act, R.id.llConditionBody2Title).setOnClickListener(this);
        util.getView(act, R.id.llConditionBody3Title).setOnClickListener(this);

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

        util.setKeyboardHide(act, util.getLinearLayout(act, R.id.llPageRoot));
        util.setKeyboardHide(act, util.getLinearLayout(act, R.id.llSvbody));

        if ( sCallType.equals("2") ) {
            ComHandler.sendMessageDelayed(ComHandler.obtainMessage(MSG_COM_G_RULES_HOMEGRP_RULE_ID), 10);
        }
    }

    @Override public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @Override public void onClick(View v) {

        switch (v.getId()) {
            case R.id.llBtn1:
                if ( util.getEditTextVal(act, R.id.edtRulesName).equals("") ) {
                    util.setShowToast(act, util.getString(act, R.string.MSG824), Toast.LENGTH_SHORT);
                    return;
                }

                Dlog.v("111111");
                //알람내용이 없으면 삭제한다
                for ( int iRow = 0; iRow < alList_Actions.size(); iRow++ ) {
                    Dlog.v("222222");
                    String sHTTP_item_sub_type_code = util.null2string(alList_Actions.get(iRow).get(Constants.HTTP_item_sub_type_code));
                    if ( sHTTP_item_sub_type_code.equals("03") ) {
                        Dlog.v("333333");
                        if ( !util.null2string(alList_Actions.get(iRow).get(Constants.HTTP_item_sub)).equals("") ) {
                            Dlog.v("444444");
                            ArrayList<HashMap<String, Object>> alList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Actions.get(iRow).get(Constants.HTTP_item_sub);
                            for ( int iRow1 = 0; iRow1 < alList_Item_Sub.size(); iRow1++ ) {
                                Dlog.v("555555");
                                String sHTTP_cmdcls_value = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_cmdcls_value));
                                if ( sHTTP_cmdcls_value.equals("") ) {
                                    alList_Item_Sub.remove(iRow1);
                                    if ( alList_Item_Sub.size() == 0 ) {
                                        alList_Actions.remove(iRow);
                                    }
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }

                if ( alList_Triggers.size() == 0 ) {
                    util.setShowToast(act, util.getString(act, R.string.MSG825), Toast.LENGTH_SHORT);
                    return;
                }

                if ( alList_Actions.size() == 0 ) {
                    util.setShowToast(act, util.getString(act, R.string.MSG832), Toast.LENGTH_SHORT);
                    return;
                }

                if ( sRuleUse_Yn.equals("X") ) {
                    Boolean bCheck = false;
                    if ( !bCheck ) {
                        if ( alList_Triggers.size() > 0 ) {
                            for ( int iRow = 0; iRow < alList_Triggers.size(); iRow++ ) {
                                ArrayList<HashMap<String, Object>> alTempList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Triggers.get(iRow).get(Constants.HTTP_item_sub);

                                for ( int iRow1 = 0; iRow1 < alTempList_Item_Sub.size(); iRow1++ ) {
                                    String sHTTP_available_yn = util.null2string(alTempList_Item_Sub.get(iRow1).get(Constants.HTTP_available_yn));
                                    if ( sHTTP_available_yn.equals("N") ) {
                                        bCheck = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if ( !bCheck ) {
                        if ( alList_Actions.size() > 0 ) {
                            for ( int iRow = 0; iRow < alList_Actions.size(); iRow++ ) {
                                ArrayList<HashMap<String, Object>> alTempList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Actions.get(iRow).get(Constants.HTTP_item_sub);

                                for ( int iRow1 = 0; iRow1 < alTempList_Item_Sub.size(); iRow1++ ) {
                                    String sHTTP_available_yn = util.null2string(alTempList_Item_Sub.get(iRow1).get(Constants.HTTP_available_yn));
                                    if ( sHTTP_available_yn.equals("N") ) {
                                        bCheck = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if ( !bCheck ) {
                        if ( alList_Conditions.size() > 0 ) {
                            for ( int iRow = 0; iRow < alList_Conditions.size(); iRow++ ) {
                                ArrayList<HashMap<String, Object>> alTempList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Conditions.get(iRow).get(Constants.HTTP_item_sub);

                                for ( int iRow1 = 0; iRow1 < alTempList_Item_Sub.size(); iRow1++ ) {
                                    String sHTTP_available_yn = util.null2string(alTempList_Item_Sub.get(iRow1).get(Constants.HTTP_available_yn));
                                    if ( sHTTP_available_yn.equals("N") ) {
                                        bCheck = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    if ( bCheck ) {
                        customDialog.MsgAlert(act, Constants.MSG875);
                        return;
                    }
                }

                if ( sCallType.equals("1") ) {
                    ComHandler.sendMessage(ComHandler.obtainMessage(MSG_COM_P_RULES));
                } else if ( sCallType.equals("2") ) {
                    ComHandler.sendMessage(ComHandler.obtainMessage(MSG_COM_P_RULES_HOMEGRP_RULE_ID));
                }
                break;
            case R.id.llBtn2:
                onBackPressed();
                break;
            case R.id.llTriggerTitle:
                if ( !v.isSelected() ) {
                    Intent intent = new Intent(act, Rules_Triggers_Select.class);
                    intent.putExtra(Constants.EXTRA_TRIGGERS, alList_Triggers);
                    intent.putExtra(Constants.EXTRA_CONDITIONS, alList_Conditions);
                    util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_TRIGGERS_SELECT);
                }
                break;
            case R.id.llActionTitle:
                util.callBtnGoogleAnalytics(act, "Set_Action", "");
                //                if ( !v.isSelected() ) {
                intent = new Intent(act, Rules_Things_Select.class);
                intent.putExtra(Constants.EXTRA_CALL_TYPE, "2");
                intent.putExtra(Constants.EXTRA_TRIGGERS, alList_Triggers);
                intent.putExtra(Constants.EXTRA_ACTIONS, alList_Actions);
                intent.putExtra(Constants.EXTRA_CONDITIONS, alList_Conditions);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_THINGS_SELECT);
                //                }
                break;
            case R.id.llAlramTitle:
                if ( !v.isSelected() ) {
                    util.callBtnGoogleAnalytics(act, "Set_Msg", "");
                    String sSOURCE_ID = "";
                    String sSOURCE_NAME = "";
                    String sSOURCE_SUB_ID = "";
                    String sSOURCE_SUB_NAME = "";
                    String sCONDITION_TYPE_CODE = "";
                    String sCONDITION_TYPE_CODE_NAME = "";
                    String sCMDCLS_VALUE = "";
                    String sCMDCLS_VALUE_MSG = "";
                    String sDATA_TYPE_CODE = "";
                    String sDAYOFWEEK = "";
                    String sREPEATABLE = "";
                    String sHOUR = "";
                    String sMINUTE = "";
                    String sUNIT = "";
                    String sIMAGE_FILE_NAME = "";
                    String sAVAILABLE_YN = "";
                    sCONDITION_TYPE_CODE = "02";

                    HashMap<String, Object> hmItem_Sub = new HashMap<String, Object>();
                    hmItem_Sub.put(Constants.HTTP_source_id, sSOURCE_ID);
                    hmItem_Sub.put(Constants.HTTP_source_name, sSOURCE_NAME);
                    hmItem_Sub.put(Constants.HTTP_source_sub_id, sSOURCE_SUB_ID);
                    hmItem_Sub.put(Constants.HTTP_source_sub_name, sSOURCE_SUB_NAME);
                    hmItem_Sub.put(Constants.HTTP_condition_type_code, sCONDITION_TYPE_CODE);
                    hmItem_Sub.put(Constants.HTTP_condition_type_code_name, sCONDITION_TYPE_CODE_NAME);
                    hmItem_Sub.put(Constants.HTTP_cmdcls_value, sCMDCLS_VALUE);
                    hmItem_Sub.put(Constants.HTTP_cmdcls_value_msg, sCMDCLS_VALUE_MSG);
                    hmItem_Sub.put(Constants.HTTP_unit, sUNIT);
                    hmItem_Sub.put(Constants.HTTP_data_type_code, sDATA_TYPE_CODE);
                    hmItem_Sub.put(Constants.HTTP_dayofweek, sDAYOFWEEK);
                    hmItem_Sub.put(Constants.HTTP_repeatable, sREPEATABLE);
                    hmItem_Sub.put(Constants.HTTP_hour, sHOUR);
                    hmItem_Sub.put(Constants.HTTP_minute, sMINUTE);
                    hmItem_Sub.put(Constants.HTTP_image_file_name, sIMAGE_FILE_NAME);
                    hmItem_Sub.put(Constants.HTTP_available_yn, sAVAILABLE_YN);
                    hmItem_Sub.put(Constants.HTTP_action_type_code, "00");

                    ArrayList<HashMap<String, Object>> alTempList_Item_Sub = new ArrayList<HashMap<String, Object>>();
                    HashMap<String, Object> hmTempActions = new HashMap<String, Object>();
                    //액션이 아예 없는 경우
                    alTempList_Item_Sub.add(hmItem_Sub);
                    hmTempActions.put(Constants.HTTP_item_name, util.getString(act, R.string.layout_rules_detail_04));
                    hmTempActions.put(Constants.HTTP_item_sub_type_code, "03");
                    hmTempActions.put(Constants.HTTP_item_sub, alTempList_Item_Sub);
                    alList_Actions.add(hmTempActions);

                    setDispActions();
                }
                break;
            case R.id.llConditionTitle:
                if ( !v.isSelected() ) {
                    v.setSelected(true);
                    util.getView(act, R.id.llConditionBody).setVisibility(View.VISIBLE);
                } else {
                    v.setSelected(false);
                    util.getView(act, R.id.llConditionBody).setVisibility(View.GONE);
                }
                break;
            case R.id.llConditionBody1Title:
                if ( !v.isSelected() ) {
                    util.callBtnGoogleAnalytics(act, "Set_Condition", "Condi_Homemode");
                    sMode = "";
                    sMode_Code = "";
                    iModePosition = -1;
                    alModeResult.clear();
                    CallMode_SPINNER();
                }
                break;
            case R.id.llConditionBody2Title:
                if ( !v.isSelected() ) {
                    util.callBtnGoogleAnalytics(act, "Set_Condition", "Condi_Period");
                    intent = new Intent(act, Rules_Date_Select.class);
                    intent.putExtra(Constants.EXTRA_DATE, "");
                    util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_DATE_SELECT);
                }
                break;
            case R.id.llConditionBody3Title:
                util.callBtnGoogleAnalytics(act, "Set_Condition", "Condi_Things");
                intent = new Intent(act, Rules_Things_Select.class);
                intent.putExtra(Constants.EXTRA_CALL_TYPE, "3");
                intent.putExtra(Constants.EXTRA_TRIGGERS, alList_Triggers);
                intent.putExtra(Constants.EXTRA_ACTIONS, alList_Actions);
                intent.putExtra(Constants.EXTRA_CONDITIONS, alList_Conditions);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_THINGS_SELECT);
                break;
            default:
                break;
        }
    }
    void CallMode_SPINNER() {

        if ( alModeId.size() > 0 ) {
            if ( !sMode_Code.equals("") ) {
                alModeResult.clear();
                String[] strArray = sMode_Code.split("\\,", 100);
                for ( int iRow = 0; iRow < strArray.length; iRow++ ) {
                    for ( int iRow1 = 0; iRow1 < alModeId.size(); iRow1++ ) {
                        if ( strArray[iRow].equals(alModeId.get(iRow1)) ) {
                            alModeResult.add("" + iRow1);
                            break;
                        }
                    }
                }
            } else {
                for ( int iRow1 = 0; iRow1 < alModeId.size(); iRow1++ ) {
                    alModeResult.add("" + iRow1);
                }
            }

            intent = new Intent();
            intent.setClass(act, Dialog_Spinner.class);
            intent.putExtra(Constants.EXTRA_SPINNER_LIST1, alModeTitle);
            if (AppConfig.getLanguageType().equalsIgnoreCase(Locale.ENGLISH.toString())) {
                intent.putExtra(Constants.EXTRA_SPINNER_TITLE, util.getString(act, R.string.layout_rules_edit_07).substring(4));
            } else {
                intent.putExtra(Constants.EXTRA_SPINNER_TITLE, util.getString(act, R.string.layout_rules_edit_07));
            }
            intent.putExtra(Constants.EXTRA_SPINNER_TYPE, iModeType);
            if ( iModeType == 2 ) {
                intent.putExtra(Constants.EXTRA_SPINNER_POSITION_LIST, alModeResult);
            } else {
                intent.putExtra(Constants.EXTRA_SPINNER_POSITION, iModePosition);
            }
            util.callActivityForResultNoAnim(act, intent, Constants.ACT_SPINNER);
        } else {
            ComHandler.sendMessage(ComHandler.obtainMessage(MSG_COM_G_MODES));
        }
    }
    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_RULES_HOMEGRP_RULE_ID:
                    //dialog_show();
                    send_G_rules_homegrp_rule_id();
                    break;
                case MSG_COM_G_MODES:
                    //dialog_show();
                    send_G_modes();
                    break;
                case MSG_COM_P_RULES:
                    //dialog_show();
                    send_P_rules();
                    break;
                case MSG_COM_P_RULES_HOMEGRP_RULE_ID:
                    //dialog_show();
                    send_P_rules_homegrp_rule_id();
                    break;

                default:
                    break;
            }
            return true;
        }
    }

    /**
     * <PRE>
     * 1. MethodName: send_P_rules
     * 2. ClassName :
     * 3. Comment   : [Send][POST]홈 규칙 등록(규칙을 등록한다.)
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    private void send_P_rules() {

        try {
            HttpSend httpSend;

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

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

                    result_P_rules(result);
                }
            });

            sHTTP_rule_name = util.getEditTextVal(act, R.id.edtRulesName);
            HashMap<String, Object> hmTemp = new HashMap<String, Object>();
            hmTemp.put(Constants.HTTP_rule_name, sHTTP_rule_name);
            hmTemp.put(Constants.HTTP_use_yn, "Y");
            hmTemp.put(Constants.HTTP_triggers, alList_Triggers);
            hmTemp.put(Constants.HTTP_actions, alList_Actions);
            hmTemp.put(Constants.HTTP_conditions, alList_Conditions);

            Dlog.v(util.MapToJson(hmTemp));
            httpSend.P_rules(util.MapToJson(hmTemp));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * <PRE>
     * 1. MethodName: result_P_rules
     * 2. ClassName :
     * 3. Comment   : [Result][POST]홈 규칙 등록(규칙을 등록한다.)
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    private void result_P_rules(Response rpResult) {

        try {
            if ( rpResult.getStatusCode() == 200 ) {
                String sHTTP_item_sub_type_code = util.null2string(alList_Triggers.get(0).get(Constants.HTTP_item_sub_type_code));
                intent = new Intent(act, Rules_Add_Sucess.class);
                intent.putExtra(Constants.EXTRA_CALL_TYPE, sHTTP_item_sub_type_code);
                intent.putExtra(Constants.EXTRA_RULE_NAME, sHTTP_rule_name);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_ADD_SUCESS);
            } 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: send_P_rules_homegrp_rule_id
     * 2. ClassName :
     * 3. Comment   : [Send][POST]홈 규칙 수정(해당 규칙을 수정한다.)
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    private void send_P_rules_homegrp_rule_id() {

        try {
            HttpSend httpSend;

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

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

                    result_P_rules_homegrp_rule_id(result);
                }
            });

            sHTTP_rule_name = util.getEditTextVal(act, R.id.edtRulesName);
            HashMap<String, Object> hmTemp = new HashMap<String, Object>();
            hmTemp.put(Constants.HTTP_rule_name, sHTTP_rule_name);
            hmTemp.put(Constants.HTTP_use_yn, "Y");
            hmTemp.put(Constants.HTTP_triggers, alList_Triggers);
            hmTemp.put(Constants.HTTP_actions, alList_Actions);
            hmTemp.put(Constants.HTTP_conditions, alList_Conditions);

            Dlog.v(util.MapToJson(hmTemp));

            httpSend.P_rules_homegrp_rule_id(sHTTP_homegrp_rule_id, util.MapToJson(hmTemp));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * <PRE>
     * 1. MethodName: result_P_rules_homegrp_rule_id
     * 2. ClassName :
     * 3. Comment   : [Result][PUT]홈 규칙 수정(해당 규칙을 수정한다.)
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    private void result_P_rules_homegrp_rule_id(Response rpResult) {

        try {
            if ( rpResult.getStatusCode() == 200 ) {
                util.setShowToast(act, util.getString(act, R.string.MSG817), Toast.LENGTH_SHORT);
                Intent intent1 = new Intent();
                intent1.putExtra(Constants.EXTRA_RELOAD_FLAG, true);
                this.setResult(RESULT_OK, intent1);
                finish();
            } 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: send_G_modes
     * 2. ClassName :
     * 3. Comment   : [Send][GET]홈모드 조회(홈그룹의 홈모드 목록을 불러온다.)
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    private void send_G_modes() {

        try {
            HttpSend httpSend;

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

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

                    result_G_modes(result);
                }
            });
            httpSend.G_modes();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * <PRE>
     * 1. MethodName: result_G_modes
     * 2. ClassName :
     * 3. Comment   : [Result][GET]홈모드 조회(홈그룹의 홈모드 목록을 불러온다.)
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    private void result_G_modes(Response rpResult) {

        try {
            if ( rpResult.getStatusCode() == 200 ) {
                ArrayList<HashMap<String, Object>> alList = util.jsonToArrayListMap(rpResult.getContent());
                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_mode_id = util.null2string(hmTemp.get(Constants.HTTP_mode_id));
                        String sHTTP_mode_name = util.null2string(hmTemp.get(Constants.HTTP_mode_name));
                        alModeId.add(sHTTP_mode_id);
                        alModeTitle.add(sHTTP_mode_name);
                    }
                }
                CallMode_SPINNER();
            } 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: send_G_rules_homegrp_rule_id
     * 2. ClassName :
     * 3. Comment   : [Send][GET]홈 규칙 상세 조회(해당 규칙의 정보를 상세 조회한다.)
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    private void send_G_rules_homegrp_rule_id() {

        try {
            HttpSend httpSend;

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

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

                    result_G_rules_homegrp_rule_id(result);
                }
            });
            httpSend.G_rules_homegrp_rule_id(sHTTP_homegrp_rule_id);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * <PRE>
     * 1. MethodName: result_G_rules_homegrp_rule_id
     * 2. ClassName :
     * 3. Comment   : [Result][GET]홈 규칙 상세 조회(해당 규칙의 정보를 상세 조회한다.)
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    private void result_G_rules_homegrp_rule_id(Response rpResult) {

        try {
            if ( rpResult.getStatusCode() == 200 ) {
                sRules_Detail_Jason = rpResult.getContent();

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

                if ( sERR_CODE.equals("401") ) {
                    customDialog.MsgAlert(act, Constants.MSG998, sERR_MSG);
                } else if ( sERR_CODE.equals("401-106") ) {
                    customDialog.MsgAlert(act, Constants.MSG997, sERR_MSG);
                } else {
                    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.MSG807:
                if ( iBtnResult == 1 ) {
                    ComHandler.sendMessageDelayed(ComHandler.obtainMessage(iType), 10);
                }
                break;
            case Constants.MSG875:
                if ( iBtnResult == 1 ) {

                    if ( alList_Triggers.size() > 0 ) {
                        for ( int iRow = 0; iRow < alList_Triggers.size(); iRow++ ) {
                            ArrayList<HashMap<String, Object>> alTempList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Triggers.get(iRow).get(Constants.HTTP_item_sub);

                            for ( int iRow1 = alTempList_Item_Sub.size() - 1; iRow1 >= 0; iRow1-- ) {
                                String sHTTP_available_yn = util.null2string(alTempList_Item_Sub.get(iRow1).get(Constants.HTTP_available_yn));
                                if ( sHTTP_available_yn.equals("N") ) {
                                    alTempList_Item_Sub.remove(iRow1);
                                    if ( alTempList_Item_Sub.size() == 0 ) {
                                        alList_Triggers.remove(iRow);
                                    }
                                }
                            }
                        }
                    }
                    if ( alList_Actions.size() > 0 ) {
                        for ( int iRow = 0; iRow < alList_Actions.size(); iRow++ ) {
                            ArrayList<HashMap<String, Object>> alTempList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Actions.get(iRow).get(Constants.HTTP_item_sub);

                            for ( int iRow1 = alTempList_Item_Sub.size() - 1; iRow1 >= 0; iRow1-- ) {
                                String sHTTP_available_yn = util.null2string(alTempList_Item_Sub.get(iRow1).get(Constants.HTTP_available_yn));
                                if ( sHTTP_available_yn.equals("N") ) {
                                    alTempList_Item_Sub.remove(iRow1);
                                    if ( alTempList_Item_Sub.size() == 0 ) {
                                        alList_Actions.remove(iRow);
                                    }
                                }
                            }
                        }
                    }
                    if ( alList_Conditions.size() > 0 ) {
                        for ( int iRow = 0; iRow < alList_Conditions.size(); iRow++ ) {
                            ArrayList<HashMap<String, Object>> alTempList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Conditions.get(iRow).get(Constants.HTTP_item_sub);

                            for ( int iRow1 = alTempList_Item_Sub.size() - 1; iRow1 >= 0; iRow1-- ) {
                                String sHTTP_available_yn = util.null2string(alTempList_Item_Sub.get(iRow1).get(Constants.HTTP_available_yn));
                                if ( sHTTP_available_yn.equals("N") ) {
                                    alTempList_Item_Sub.remove(iRow1);
                                    if ( alTempList_Item_Sub.size() == 0 ) {
                                        alList_Conditions.remove(iRow);
                                    }
                                }
                            }
                        }
                    }
                    sRuleUse_Yn = "N";

                    setDispTriggers();
                    setDispActions();
                    getAlList_Conditions(0);
                    util.getView(act, R.id.llBtn1).performClick();
                }
                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_RULES_ADD_SUCESS:
                    act.setResult(RESULT_OK, data);
                    finish();
                    break;
                case Constants.ACT_RULES_TRIGGERS_SELECT:
                    alList_Triggers = (ArrayList<HashMap<String, Object>>) data.getSerializableExtra(Constants.EXTRA_TRIGGERS);
                    alList_Conditions = (ArrayList<HashMap<String, Object>>) data.getSerializableExtra(Constants.EXTRA_CONDITIONS);

                    setDispTriggers();
                    break;
                case Constants.ACT_RULES_THINGS_SELECT:
                    alList_Actions = (ArrayList<HashMap<String, Object>>) data.getSerializableExtra(Constants.EXTRA_ACTIONS);
                    alList_Conditions = (ArrayList<HashMap<String, Object>>) data.getSerializableExtra(Constants.EXTRA_CONDITIONS);

                    setDispActions();
                    getAlList_Conditions(0);
                    break;
                case Constants.ACT_SPINNER:
                    sMode = "";
                    sMode_Code = "";
                    if ( iModePosition != data.getIntExtra(Constants.EXTRA_SPINNER_RESULT, -1) ) {
                        iModePosition = data.getIntExtra(Constants.EXTRA_SPINNER_RESULT, 0);
                        sMode = util.null2string(alModeTitle.get(iModePosition));
                        sMode_Code = util.null2string(alModeId.get(iModePosition));
                    } else {
                        alModeResult = data.getStringArrayListExtra(Constants.EXTRA_SPINNER_RESULT_LIST);
                        if ( alModeResult != null ) {
                            if ( alModeResult.size() > 0 ) {
                                for ( int iRow = 0; iRow < alModeResult.size(); iRow++ ) {
                                    if ( iRow > 0 ) {
                                        sMode += ",";
                                        sMode_Code += ",";
                                    }
                                    sMode += util.null2empty(alModeTitle.get(util.str2int(alModeResult.get(iRow))));
                                    sMode_Code += util.null2empty(alModeId.get(util.str2int(alModeResult.get(iRow))));
                                }
                            }
                        }
                    }

                    if ( !sMode.equals("") ) {
                        Dlog.v("sMode", sMode);
                        Dlog.v("sMode_Code", sMode_Code);
                        String sSOURCE_ID = "";
                        String sSOURCE_NAME = "";
                        String sSOURCE_SUB_ID = "";
                        String sSOURCE_SUB_NAME = "";
                        String sCONDITION_TYPE_CODE = "";
                        String sCONDITION_TYPE_CODE_NAME = "";
                        String sCMDCLS_VALUE = "";
                        String sCMDCLS_VALUE_MSG = "";
                        String sDATA_TYPE_CODE = "";
                        String sDAYOFWEEK = "";
                        String sREPEATABLE = "";
                        String sHOUR = "";
                        String sMINUTE = "";
                        String sUNIT = "";
                        String sIMAGE_FILE_NAME = "";
                        String sAVAILABLE_YN = "";
                        sCMDCLS_VALUE = sMode_Code;
                        sCMDCLS_VALUE_MSG = sMode;

                        HashMap<String, Object> hmItem_Sub = new HashMap<String, Object>();
                        hmItem_Sub.put(Constants.HTTP_source_id, sSOURCE_ID);
                        hmItem_Sub.put(Constants.HTTP_source_name, sSOURCE_NAME);
                        hmItem_Sub.put(Constants.HTTP_source_sub_id, sSOURCE_SUB_ID);
                        hmItem_Sub.put(Constants.HTTP_source_sub_name, sSOURCE_SUB_NAME);
                        hmItem_Sub.put(Constants.HTTP_condition_type_code, sCONDITION_TYPE_CODE);
                        hmItem_Sub.put(Constants.HTTP_condition_type_code_name, sCONDITION_TYPE_CODE_NAME);
                        hmItem_Sub.put(Constants.HTTP_cmdcls_value, sCMDCLS_VALUE);
                        hmItem_Sub.put(Constants.HTTP_cmdcls_value_msg, sCMDCLS_VALUE_MSG);
                        hmItem_Sub.put(Constants.HTTP_unit, sUNIT);
                        hmItem_Sub.put(Constants.HTTP_data_type_code, sDATA_TYPE_CODE);
                        hmItem_Sub.put(Constants.HTTP_dayofweek, sDAYOFWEEK);
                        hmItem_Sub.put(Constants.HTTP_repeatable, sREPEATABLE);
                        hmItem_Sub.put(Constants.HTTP_hour, sHOUR);
                        hmItem_Sub.put(Constants.HTTP_minute, sMINUTE);
                        hmItem_Sub.put(Constants.HTTP_image_file_name, sIMAGE_FILE_NAME);
                        hmItem_Sub.put(Constants.HTTP_available_yn, sAVAILABLE_YN);

                        ArrayList<HashMap<String, Object>> alTempList_Item_Sub = new ArrayList<HashMap<String, Object>>();

                        HashMap<String, Object> hmTempConditions = new HashMap<String, Object>();
                        Boolean bCheck = false;
                        if ( alList_Conditions.size() > 0 ) {
                            for ( int iRow = 0; iRow < alList_Conditions.size(); iRow++ ) {
                                String sHTTP_item_sub_type_code = util.null2string(alList_Conditions.get(iRow).get(Constants.HTTP_item_sub_type_code));
                                if ( sHTTP_item_sub_type_code.equals("02") ) {
                                    alTempList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Conditions.get(iRow).get(Constants.HTTP_item_sub);
                                    alTempList_Item_Sub.clear();
                                    alTempList_Item_Sub.add(hmItem_Sub);
                                    bCheck = true;
                                    break;
                                }
                            }
                        }
                        if ( !bCheck ) {
                            alTempList_Item_Sub.add(hmItem_Sub);
                            //컨디션은 있지만 모드가 없는경우 추가하는경우
                            hmTempConditions.put(Constants.HTTP_item_name, util.getString(act, R.string.layout_rules_edit_07));
                            hmTempConditions.put(Constants.HTTP_item_sub_type_code, "02");
                            hmTempConditions.put(Constants.HTTP_item_sub, alTempList_Item_Sub);
                            alList_Conditions.add(hmTempConditions);
                        }
                        getAlList_Conditions(0);
                    }
                    break;
                case Constants.ACT_RULES_DATE_SELECT:
                    String sDate = data.getStringExtra(Constants.EXTRA_DATE);
                    if ( !sDate.equals("") ) {
                        Dlog.v("sDate", sDate);
                        String sSOURCE_ID = "";
                        String sSOURCE_NAME = "";
                        String sSOURCE_SUB_ID = "";
                        String sSOURCE_SUB_NAME = "";
                        String sCONDITION_TYPE_CODE = "";
                        String sCONDITION_TYPE_CODE_NAME = "";
                        String sCMDCLS_VALUE = "";
                        String sCMDCLS_VALUE_MSG = "";
                        String sDATA_TYPE_CODE = "";
                        String sDAYOFWEEK = "";
                        String sREPEATABLE = "";
                        String sHOUR = "";
                        String sMINUTE = "";
                        String sUNIT = "";
                        String sIMAGE_FILE_NAME = "";
                        String sAVAILABLE_YN = "";
                        sCMDCLS_VALUE = sDate;

                        HashMap<String, Object> hmItem_Sub = new HashMap<String, Object>();
                        hmItem_Sub.put(Constants.HTTP_source_id, sSOURCE_ID);
                        hmItem_Sub.put(Constants.HTTP_source_name, sSOURCE_NAME);
                        hmItem_Sub.put(Constants.HTTP_source_sub_id, sSOURCE_SUB_ID);
                        hmItem_Sub.put(Constants.HTTP_source_sub_name, sSOURCE_SUB_NAME);
                        hmItem_Sub.put(Constants.HTTP_condition_type_code, sCONDITION_TYPE_CODE);
                        hmItem_Sub.put(Constants.HTTP_condition_type_code_name, sCONDITION_TYPE_CODE_NAME);
                        hmItem_Sub.put(Constants.HTTP_cmdcls_value, sCMDCLS_VALUE);
                        hmItem_Sub.put(Constants.HTTP_cmdcls_value_msg, sCMDCLS_VALUE_MSG);
                        hmItem_Sub.put(Constants.HTTP_unit, sUNIT);
                        hmItem_Sub.put(Constants.HTTP_data_type_code, sDATA_TYPE_CODE);
                        hmItem_Sub.put(Constants.HTTP_dayofweek, sDAYOFWEEK);
                        hmItem_Sub.put(Constants.HTTP_repeatable, sREPEATABLE);
                        hmItem_Sub.put(Constants.HTTP_hour, sHOUR);
                        hmItem_Sub.put(Constants.HTTP_minute, sMINUTE);
                        hmItem_Sub.put(Constants.HTTP_image_file_name, sIMAGE_FILE_NAME);
                        hmItem_Sub.put(Constants.HTTP_available_yn, sAVAILABLE_YN);

                        ArrayList<HashMap<String, Object>> alTempList_Item_Sub = new ArrayList<HashMap<String, Object>>();

                        HashMap<String, Object> hmTempConditions = new HashMap<String, Object>();
                        Boolean bCheck = false;
                        if ( alList_Conditions.size() > 0 ) {
                            for ( int iRow = 0; iRow < alList_Conditions.size(); iRow++ ) {
                                String sHTTP_item_sub_type_code = util.null2string(alList_Conditions.get(iRow).get(Constants.HTTP_item_sub_type_code));
                                if ( sHTTP_item_sub_type_code.equals("21") ) {
                                    alTempList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Conditions.get(iRow).get(Constants.HTTP_item_sub);
                                    alTempList_Item_Sub.clear();
                                    alTempList_Item_Sub.add(hmItem_Sub);
                                    bCheck = true;
                                    break;
                                }
                            }
                        }
                        if ( !bCheck ) {
                            alTempList_Item_Sub.add(hmItem_Sub);
                            //컨디션은 있지만 날짜가 없는경우 추가하는경우
                            hmTempConditions.put(Constants.HTTP_item_name, util.getString(act, R.string.layout_rules_edit_08));
                            hmTempConditions.put(Constants.HTTP_item_sub_type_code, "21");
                            hmTempConditions.put(Constants.HTTP_item_sub, alTempList_Item_Sub);
                            alList_Conditions.add(hmTempConditions);
                        }
                        getAlList_Conditions(0);
                    }
                    break;
                default:
                    break;
            }
        } else {
            switch (requestCode) {
                default:
                    break;
            }
        }
    }

    void setRulesJason_init(String sContent) {

        try {
            JSONObject joResult = new JSONObject(sContent);

            sHTTP_homegrp_rule_id = util.null2string(joResult.get(Constants.HTTP_homegrp_rule_id));
            sHTTP_rule_name = util.null2string(joResult.get(Constants.HTTP_rule_name));
            util.getEditText(act, R.id.edtRulesName).setText(sHTTP_rule_name);

            //            if ( joResult.has(Constants.HTTP_latest_execution_datetime) ) {
            //                util.getView(act, R.id.llLastActionTime).setVisibility(View.VISIBLE);
            //                String sHTTP_latest_execution_datetime = util.null2string(joResult.get(Constants.HTTP_latest_execution_datetime));
            //                util.getTextView(act, R.id.tvLastActionTime).setText(sHTTP_latest_execution_datetime);
            //            } else {
            //                util.getView(act, R.id.llLastActionTime).setVisibility(View.GONE);
            //                util.getTextView(act, R.id.tvLastActionTime).setText("");
            //            }

            sRuleUse_Yn = util.null2string(joResult.get(Constants.HTTP_use_yn));

            //            util.getButton(act, R.id.btnOnOff).setSelected(false);
            //            if ( sRuleUse_Yn.equals("Y") ) {
            //                bDeviceDelYn = false;
            //                util.getButton(act, R.id.btnOnOff).setSelected(true);
            //                util.getView(act, R.id.tvNotice).setVisibility(View.GONE);
            //            } else if ( sRuleUse_Yn.equals("N") ) {
            //                bDeviceDelYn = false;
            //                util.getView(act, R.id.tvNotice).setVisibility(View.GONE);
            //            } else if ( sRuleUse_Yn.equals("X") ) {
            //                bDeviceDelYn = true;
            //                util.getView(act, R.id.tvNotice).setVisibility(View.VISIBLE);
            //            }

            sCONDITIONS = util.null2empty(joResult.get(Constants.HTTP_conditions)).toString();
            sTRIGGERS = util.null2empty(joResult.get(Constants.HTTP_triggers)).toString();
            sACTIONS = util.null2empty(joResult.get(Constants.HTTP_actions)).toString();

            setDispConditions_init();
            setDispTriggers_init();
            setDispActions_init();
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.MsgAlert(act, Constants.MSG807);
        }
    }

    void setDispTriggers_init() {

        Dlog.v("sTRIGGERS=", sTRIGGERS);
        alList_Triggers.clear();
        alList_Triggers = util.jsonToArrayListMap(sTRIGGERS);
        setDispTriggers();
    }

    void setDispTriggers() {

        Dlog.v("alList_Triggers1 = ", alList_Triggers.toString());
        util.getLinearLayout(act, R.id.llTrigger).setVisibility(View.VISIBLE);
        util.getView(act, R.id.llTriggerTitle).setSelected(false);
        util.getTextView(act, R.id.tvTriggerTitle).setText(util.getString(act, R.string.layout_rules_edit_03));
        util.getView(act, R.id.ivTriggerAdd).setVisibility(View.VISIBLE);
        util.getLinearLayout(act, R.id.llTriggerBody).removeAllViews();
        if ( alList_Triggers.size() > 0 ) {
            //현재로써는 트리거는 단일임 나중에 멀티일때 풀어야 함
            //            for ( int iRow = 0; iRow < alList_Triggers.size(); iRow++ ) {
            int iRow = 0;
            HashMap<String, Object> hmTemp = new HashMap<String, Object>();
            hmTemp = alList_Triggers.get(iRow);
            LinearLayout childView = (LinearLayout) View.inflate(this, R.layout.row_rules_triggers_list, null);
            //            util.getView(childView, R.id.btnRowDelete).setVisibility(View.GONE);
            util.getView(childView, R.id.btnRowDelete).setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {

                    //                    alList_Conditions.clear();
                    delAlList_Conditions(1);
                    sTRIGGERS = sCustomJason;
                    setDispTriggers_init();
                }
            });

            util.getView(childView, R.id.llRowRoot).setTag(iRow);
            util.getView(childView, R.id.llRowRoot).setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {

                    Intent intent = new Intent(act, Rules_Triggers_Select.class);
                    intent.putExtra(Constants.EXTRA_TRIGGERS, alList_Triggers);
                    intent.putExtra(Constants.EXTRA_CONDITIONS, alList_Conditions);
                    util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_TRIGGERS_SELECT);
                }
            });

            String sHTTP_item_name = util.null2string(hmTemp.get(Constants.HTTP_item_name));
            String sHTTP_item_sub_type_code = util.null2string(hmTemp.get(Constants.HTTP_item_sub_type_code));

            util.getView(act, R.id.llTriggerTitle).setSelected(true);
            util.getTextView(act, R.id.tvTriggerTitle).setText(sHTTP_item_name);
            util.getView(act, R.id.ivTriggerAdd).setVisibility(View.GONE);
            ArrayList<HashMap<String, Object>> alList_Item_Sub = new ArrayList<HashMap<String, Object>>();
            if ( !util.null2string(hmTemp.get(Constants.HTTP_item_sub)).equals("") ) {
                alList_Item_Sub = (ArrayList<HashMap<String, Object>>) hmTemp.get(Constants.HTTP_item_sub);
                int iRow1 = 0;
                //                callChildView = childView;
                if ( sHTTP_item_sub_type_code.equals("05") ) {
                    //time
                    String sHTTP_hour = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_hour));
                    String sHTTP_minute = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_minute));
                    String sAMPM = (util.str2int(sHTTP_hour) >= 12 ? util.getString(act, R.string.layout_pm) : util.getString(act, R.string.layout_am));
                    String sHour = (util.str2int(sHTTP_hour) >= 12 ? decimalFormat.format((util.str2int(sHTTP_hour) - 12)) : sHTTP_hour);
                    String sDayofweek = getAlList_Conditions(1);

                    //                    String sHTTP_delete_yn = util.null2string(alList_Item_Sub.get(iRow).get(Constants.HTTP_delete_yn));

                    if ( appConfig.getLanguage_Id().equals("ko") ) {
                        util.getTextView(childView, R.id.tvRowTitle).setText(sAMPM + " " + sHour + util.getString(act, R.string.layout_hour) + " " + sHTTP_minute + util.getString(act,
                                                                                                                                                                                   R.string.layout_minute));
                    } else {
                        util.getTextView(childView, R.id.tvRowTitle).setText(sHour
                                                                             + util.getString(act, R.string.layout_hour)
                                                                             + sHTTP_minute
                                                                             + util.getString(act, R.string.layout_minute)
                                                                             + " "
                                                                             + sAMPM);
                    }

                    util.getTextView(childView, R.id.tvRowBody).setText(sDayofweek);
                    util.getTextView(childView, R.id.tvRowState).setVisibility(View.GONE);

                    util.setImage(act, R.drawable.img_rule_trigger_icon_time, util.getImageView(childView, R.id.ivRowIcon));
                } else if ( sHTTP_item_sub_type_code.equals("32") ) {
                    //sun
                    String sHTTP_source_name = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_source_name));
                    String sHTTP_source_sub_name = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_source_sub_name));
                    String sDayofweek = getAlList_Conditions(1);

                    //                    String sHTTP_delete_yn = util.null2string(alList_Item_Sub.get(iRow).get(Constants.HTTP_delete_yn));

                    util.getTextView(childView, R.id.tvRowTitle).setText(sHTTP_source_sub_name);

                    util.getTextView(childView, R.id.tvRowBody).setText(sHTTP_source_name + "/" + sDayofweek);
                    util.getTextView(childView, R.id.tvRowState).setVisibility(View.GONE);

                    util.setImage(act, R.drawable.img_rule_trigger_icon_sunset, util.getImageView(childView, R.id.ivRowIcon));
                } else if ( sHTTP_item_sub_type_code.equals("31") ) {
                    //더울 때 /추울때
                    String sHTTP_source_name = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_source_name));
                    String sHTTP_condition_type_code = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_condition_type_code));
                    String sHTTP_cmdcls_value = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_cmdcls_value));
                    String sDayofweek = getAlList_Conditions(1);

                    //                    String sHTTP_delete_yn = util.null2string(alList_Item_Sub.get(iRow).get(Constants.HTTP_delete_yn));

                    String sTempMsg = "";
                    if ( sHTTP_condition_type_code.equals("02") ) {
                        sTempMsg = util.getString(act, R.string.layout_rules_triggers_hot_03);
                    } else if ( sHTTP_condition_type_code.equals("04") ) {
                        sTempMsg = util.getString(act, R.string.layout_rules_triggers_hot_02);
                    }

                    util.getTextView(childView, R.id.tvRowTitle).setText(sTempMsg);

                    util.getTextView(childView, R.id.tvRowBody).setText(sHTTP_source_name + "/" + sHTTP_cmdcls_value + util.getString(act, R.string.layout_temperature1) + "/" + sDayofweek);
                    util.getTextView(childView, R.id.tvRowState).setVisibility(View.GONE);

                    util.setImage(act, R.drawable.img_rule_trigger_icon_temperature, util.getImageView(childView, R.id.ivRowIcon));
                } else if ( sHTTP_item_sub_type_code.equals("01") ) {
                    //상태가 변할때
                    String sHTTP_source_name = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_source_name));
                    String sHTTP_source_sub_name = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_source_sub_name));
                    String sHTTP_condition_type_code = util.null2string(alList_Item_Sub.get(iRow).get(Constants.HTTP_condition_type_code));
                    String sHTTP_condition_type_code_name = util.null2string(alList_Item_Sub.get(iRow).get(Constants.HTTP_condition_type_code_name));
                    String sHTTP_cmdcls_value = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_cmdcls_value));
                    String sHTTP_cmdcls_value_msg = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_cmdcls_value_msg));
                    String sHTTP_unit = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_unit));
                    String sHTTP_image_file_name = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_image_file_name));
                    //                    String sDayofweek = getAlList_Conditions(1);

                    String sHTTP_available_yn = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_available_yn));

                    if ( sHTTP_available_yn.equals("N") ) {
                        util.getTextView(childView, R.id.tvRowTitle).setText(util.setSpCenterLine(act, sHTTP_source_name));
                    } else {
                        util.getTextView(childView, R.id.tvRowTitle).setText(sHTTP_source_name);
                    }
                    util.getTextView(childView, R.id.tvRowBody).setText(sHTTP_source_sub_name);
                    util.getTextView(childView, R.id.tvRowState).setVisibility(View.VISIBLE);
                    util.getTextView(childView, R.id.tvRowState).setText((sHTTP_cmdcls_value_msg.equals("") ? sHTTP_cmdcls_value : sHTTP_cmdcls_value_msg) + sHTTP_unit + (
                            sHTTP_condition_type_code.equals("01")
                            ? ""
                            : sHTTP_condition_type_code_name));
                    ;

                    util.setImage(act, sHTTP_image_file_name, util.getImageView(childView, R.id.ivRowIcon));
                }
            }
            util.getLinearLayout(act, R.id.llTriggerBody).addView(childView);
            //            }
            //        } else {
            //            util.getLinearLayout(act, R.id.llTrigger).setVisibility(View.GONE);
        }
    }

    void setDispActions_init() {

        Dlog.v("sACTIONS=", sACTIONS);
        alList_Actions.clear();
        alList_Actions = util.jsonToArrayListMap(sACTIONS);
        setDispActions();
    }

    void setDispActions() {

        Dlog.v("alList_Actions1 = ", alList_Actions.toString());
        util.getLinearLayout(act, R.id.llAction).setVisibility(View.VISIBLE);
        util.getLinearLayout(act, R.id.llActionBody).removeAllViews();

        util.getLinearLayout(act, R.id.llAlram).setVisibility(View.VISIBLE);
        util.getLinearLayout(act, R.id.llAlramBody).removeAllViews();
        util.getView(act, R.id.ivActionAdd).setVisibility(View.VISIBLE);
        util.getView(act, R.id.ivAlramAdd).setVisibility(View.VISIBLE);
        util.getView(act, R.id.llActionTitle).setSelected(false);
        util.getView(act, R.id.llAlramTitle).setSelected(false);
        if ( alList_Actions.size() > 0 ) {
            for ( int iRow = 0; iRow < alList_Actions.size(); iRow++ ) {
                HashMap<String, Object> hmTemp = new HashMap<String, Object>();
                hmTemp = alList_Actions.get(iRow);

                String sHTTP_item_sub_type_code = util.null2string(hmTemp.get(Constants.HTTP_item_sub_type_code));

                ArrayList<HashMap<String, Object>> alList_Item_Sub = new ArrayList<HashMap<String, Object>>();
                if ( !util.null2string(hmTemp.get(Constants.HTTP_item_sub)).equals("") ) {
                    alList_Item_Sub = (ArrayList<HashMap<String, Object>>) hmTemp.get(Constants.HTTP_item_sub);
                    if ( sHTTP_item_sub_type_code.equals("01") ) {
                        util.getView(act, R.id.llActionTitle).setSelected(true);
                        for ( int iRow1 = 0; iRow1 < alList_Item_Sub.size(); iRow1++ ) {
                            LinearLayout childView = (LinearLayout) View.inflate(this, R.layout.row_rules_actions_list, null);

                            util.getView(childView, R.id.llRowRoot).setTag(iRow1);
                            util.getView(childView, R.id.llRowRoot).setOnClickListener(new View.OnClickListener() {
                                int iTag = 0;

                                @Override public void onClick(View v) {

                                    iTag = (int) v.getTag();

                                    for ( int iRow = 0; iRow < alList_Actions.size(); iRow++ ) {
                                        String sHTTP_item_sub_type_code = util.null2string(alList_Actions.get(iRow).get(Constants.HTTP_item_sub_type_code));
                                        if ( !util.null2string(alList_Actions.get(iRow).get(Constants.HTTP_item_sub)).equals("") ) {
                                            ArrayList<HashMap<String, Object>> alList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Actions.get(iRow).get(Constants.HTTP_item_sub);
                                            if ( sHTTP_item_sub_type_code.equals("01") ) {
                                                String sHTTP_source_id = util.null2string(alList_Item_Sub.get(iTag).get(Constants.HTTP_source_id));
                                                String sHTTP_source_sub_id = util.null2string(alList_Item_Sub.get(iTag).get(Constants.HTTP_source_sub_id));
                                                String sHTTP_available_yn = util.null2string(alList_Item_Sub.get(iTag).get(Constants.HTTP_available_yn));

                                                if ( sHTTP_available_yn.equals("N") ) {
                                                    util.setShowToast(act, util.getString(act, R.string.MSG900), Toast.LENGTH_SHORT);
                                                    return;
                                                } else {
                                                    intent = new Intent(act, Rules_Things_Select.class);
                                                    intent.putExtra(Constants.EXTRA_CALL_TYPE, "2");
                                                    intent.putExtra(Constants.EXTRA_TRIGGERS, alList_Triggers);
                                                    intent.putExtra(Constants.EXTRA_ACTIONS, alList_Actions);
                                                    intent.putExtra(Constants.EXTRA_CONDITIONS, alList_Conditions);
                                                    intent.putExtra(Constants.EXTRA_DEVICE_ID, sHTTP_source_id);
                                                    intent.putExtra(Constants.EXTRA_NODE_ID, sHTTP_source_sub_id);
                                                    intent.putExtra(Constants.EXTRA_POSITION, iTag);

                                                    util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_THINGS_SELECT);
                                                }
                                                break;
                                            }
                                        }
                                    }
                                }
                            });

                            util.getView(childView, R.id.btnRowDelete).setTag(iRow1);
                            util.getView(childView, R.id.btnRowDelete).setOnClickListener(new View.OnClickListener() {
                                int iTag = 0;

                                @Override public void onClick(View v) {

                                    iTag = (int) v.getTag();
                                    for ( int iRow = 0; iRow < alList_Actions.size(); iRow++ ) {
                                        String sHTTP_item_sub_type_code = util.null2string(alList_Actions.get(iRow).get(Constants.HTTP_item_sub_type_code));
                                        ArrayList<HashMap<String, Object>> alList_Item_Sub = new ArrayList<HashMap<String, Object>>();
                                        if ( !util.null2string(alList_Actions.get(iRow).get(Constants.HTTP_item_sub)).equals("") ) {
                                            alList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Actions.get(iRow).get(Constants.HTTP_item_sub);
                                            if ( sHTTP_item_sub_type_code.equals("01") ) {
                                                alList_Item_Sub.remove(iTag);
                                                if ( alList_Item_Sub.size() == 0 ) {
                                                    alList_Actions.remove(iRow);
                                                }
                                                break;
                                            }
                                        }
                                    }
                                    setDispActions();
                                }
                            });
                            //                            callChildView = childView;
                            String sHTTP_source_name = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_source_name));
                            String sHTTP_source_sub_name = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_source_sub_name));
                            String sHTTP_condition_type_code = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_condition_type_code));
                            String sHTTP_condition_type_code_name = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_condition_type_code_name));
                            String sHTTP_cmdcls_value = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_cmdcls_value));
                            String sHTTP_unit = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_unit));
                            String sHTTP_cmdcls_value_msg = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_cmdcls_value_msg));
                            String sHTTP_image_file_name = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_image_file_name));
                            //                    String sDayofweek = getAlList_Conditions(1);

                            String sHTTP_available_yn = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_available_yn));

                            if ( sHTTP_available_yn.equals("N") ) {
                                util.getTextView(childView, R.id.tvRowTitle).setText(util.setSpCenterLine(act, sHTTP_source_name));
                            } else {
                                util.getTextView(childView, R.id.tvRowTitle).setText(sHTTP_source_name);
                            }
                            util.getTextView(childView, R.id.tvRowBody).setText(sHTTP_source_sub_name);
                            util.getTextView(childView, R.id.tvRowState).setVisibility(View.VISIBLE);
                            util.getTextView(childView, R.id.tvRowState).setText((sHTTP_cmdcls_value_msg.equals("") ? sHTTP_cmdcls_value : sHTTP_cmdcls_value_msg) + sHTTP_unit + (
                                    sHTTP_condition_type_code.equals("01")
                                    ? ""
                                    : sHTTP_condition_type_code_name));
                            ;

                            util.setImage(act, sHTTP_image_file_name, util.getImageView(childView, R.id.ivRowIcon));
                            util.getLinearLayout(act, R.id.llActionBody).addView(childView);
                        }
                    } else if ( sHTTP_item_sub_type_code.equals("03") ) {
                        util.getView(act, R.id.llAlramTitle).setSelected(true);
                        util.getView(act, R.id.ivAlramAdd).setVisibility(View.GONE);
                        int iRow1 = 0;
                        LinearLayout childView = (LinearLayout) View.inflate(this, R.layout.row_rules_actions_alram_list1, null);

                        util.getView(childView, R.id.btnRowDelete).setTag(iRow1);
                        util.getView(childView, R.id.btnRowDelete).setOnClickListener(new View.OnClickListener() {
                            int iTag = 0;

                            @Override public void onClick(View v) {

                                iTag = (int) v.getTag();
                                for ( int iRow = 0; iRow < alList_Actions.size(); iRow++ ) {
                                    String sHTTP_item_sub_type_code = util.null2string(alList_Actions.get(iRow).get(Constants.HTTP_item_sub_type_code));
                                    ArrayList<HashMap<String, Object>> alList_Item_Sub = new ArrayList<HashMap<String, Object>>();
                                    if ( !util.null2string(alList_Actions.get(iRow).get(Constants.HTTP_item_sub)).equals("") ) {
                                        alList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Actions.get(iRow).get(Constants.HTTP_item_sub);
                                        if ( sHTTP_item_sub_type_code.equals("03") ) {
                                            alList_Item_Sub.remove(iTag);
                                            if ( alList_Item_Sub.size() == 0 ) {
                                                alList_Actions.remove(iRow);
                                            }
                                            util.getView(act, R.id.llAlramTitle).setSelected(false);
                                            break;
                                        }
                                    }
                                }
                                setDispActions();
                            }
                        });
                        String sHTTP_cmdcls_value = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_cmdcls_value));

                        //                        util.getEditText(childView, R.id.edtRowTitle).setText(sHTTP_cmdcls_value);

                        final EditText edtBody = util.getEditText(childView, R.id.edtRowTitle);
                        edtBody.setText(sHTTP_cmdcls_value);
                        edtBody.setTag(iRow1);
                        edtBody.addTextChangedListener(new TextWatcher() {
                            String previousString = "";

                            @Override public void onTextChanged(CharSequence s, int start, int before, int count) {

                            }

                            @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                                previousString = s.toString();
                            }

                            @Override public void afterTextChanged(Editable s) {

                                previousString = s.toString();
                                int iTag = (int) edtBody.getTag();

                                for ( int iRow = 0; iRow < alList_Actions.size(); iRow++ ) {
                                    String sHTTP_item_sub_type_code = util.null2string(alList_Actions.get(iRow).get(Constants.HTTP_item_sub_type_code));
                                    if ( !util.null2string(alList_Actions.get(iRow).get(Constants.HTTP_item_sub)).equals("") ) {
                                        ArrayList<HashMap<String, Object>> alList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Actions.get(iRow).get(Constants.HTTP_item_sub);
                                        if ( sHTTP_item_sub_type_code.equals("03") ) {
                                            alList_Item_Sub.get(iTag).put(Constants.HTTP_cmdcls_value, previousString);
                                            break;
                                        }
                                    }
                                }
                            }
                        });
                        util.getLinearLayout(act, R.id.llAlramBody).addView(childView);
                    }
                }
            }
            //        } else {
            //            util.getLinearLayout(act, R.id.llAction).setVisibility(View.GONE);
        }
    }

    void setDispConditions_init() {

        Dlog.v("sACTIONS=", sACTIONS);
        alList_Conditions.clear();
        alList_Conditions = util.jsonToArrayListMap(sCONDITIONS);
        getAlList_Conditions(0);
    }

    private String getAlList_Conditions(int iType) {
        //        iType   = 1: 요일 담아주기
        String sReturn = "";
        if ( iType == 1 ) {
            if ( alList_Conditions.size() > 0 ) {
                for ( int iRow = 0; iRow < alList_Conditions.size(); iRow++ ) {
                    HashMap<String, Object> hmTemp = new HashMap<String, Object>();
                    hmTemp = alList_Conditions.get(iRow);

                    String sHTTP_item_sub_type_code = util.null2string(hmTemp.get(Constants.HTTP_item_sub_type_code));
                    if ( sHTTP_item_sub_type_code.equals("22") ) {
                        //요일
                        ArrayList<HashMap<String, Object>> alList_Item_Sub = new ArrayList<HashMap<String, Object>>();
                        if ( !util.null2string(hmTemp.get(Constants.HTTP_item_sub)).equals("") ) {
                            alList_Item_Sub = (ArrayList<HashMap<String, Object>>) hmTemp.get(Constants.HTTP_item_sub);
                            if ( alList_Item_Sub.size() > 0 ) {
                                HashMap<String, Object> hmTempItem_sub = new HashMap<String, Object>();
                                hmTempItem_sub = alList_Item_Sub.get(0);

                                String sHTTP_cmdcls_value_msg = util.null2string(hmTempItem_sub.get(Constants.HTTP_cmdcls_value_msg));
                                sReturn = sHTTP_cmdcls_value_msg;
                            }
                        }
                        break;
                    }
                }
            }
        } else {
            util.getView(act, R.id.llCondition).setVisibility(View.VISIBLE);
            util.getView(act, R.id.llConditionBody).setVisibility(View.GONE);
            util.getView(act, R.id.llConditionTitle).setSelected(false);
            util.getView(act, R.id.ivConditionBody1TitleAdd).setVisibility(View.VISIBLE);
            util.getView(act, R.id.ivConditionBody2TitleAdd).setVisibility(View.VISIBLE);
            util.getView(act, R.id.ivConditionBody3TitleAdd).setVisibility(View.VISIBLE);
            util.getLinearLayout(act, R.id.llConditionBody1).removeAllViews();
            util.getLinearLayout(act, R.id.llConditionBody2).removeAllViews();
            util.getLinearLayout(act, R.id.llConditionBody3).removeAllViews();
            if ( alList_Conditions.size() > 0 ) {
                //수정필요
                for ( int iRow = 0; iRow < alList_Conditions.size(); iRow++ ) {
                    HashMap<String, Object> hmTemp = new HashMap<String, Object>();
                    hmTemp = alList_Conditions.get(iRow);

                    String sHTTP_item_sub_type_code = util.null2string(hmTemp.get(Constants.HTTP_item_sub_type_code));
                    if ( !sHTTP_item_sub_type_code.equals("22") ) {
                        util.getView(act, R.id.llConditionBody).setVisibility(View.VISIBLE);
                        util.getView(act, R.id.llConditionTitle).setSelected(true);
                        ArrayList<HashMap<String, Object>> alList_Item_Sub = new ArrayList<HashMap<String, Object>>();
                        if ( !util.null2string(hmTemp.get(Constants.HTTP_item_sub)).equals("") ) {
                            alList_Item_Sub = (ArrayList<HashMap<String, Object>>) hmTemp.get(Constants.HTTP_item_sub);
                            if ( alList_Item_Sub.size() > 0 ) {
                                for ( int iRow1 = 0; iRow1 < alList_Item_Sub.size(); iRow1++ ) {
                                    HashMap<String, Object> hmTempItem_sub = new HashMap<String, Object>();
                                    hmTempItem_sub = alList_Item_Sub.get(iRow1);

                                    LinearLayout childView = (LinearLayout) View.inflate(this, R.layout.row_rules_conditions_list, null);

                                    util.getView(childView, R.id.llRowBody_Body).setVisibility(View.GONE);
                                    util.getView(childView, R.id.tvRowState).setVisibility(View.VISIBLE);

                                    String sTempTitle = "";
                                    if ( sHTTP_item_sub_type_code.equals("02") ) {
                                        //모드
                                        util.getView(act, R.id.ivConditionBody1TitleAdd).setVisibility(View.GONE);
                                        util.getView(childView, R.id.vEmp1).setVisibility(View.VISIBLE);

                                        util.getView(childView, R.id.llRowRoot).setTag(iRow1);
                                        util.getView(childView, R.id.llRowRoot).setOnClickListener(new View.OnClickListener() {
                                            int iTag = 0;

                                            @Override public void onClick(View v) {

                                                iTag = (int) v.getTag();

                                                for ( int iRow = 0; iRow < alList_Conditions.size(); iRow++ ) {
                                                    String sHTTP_item_sub_type_code = util.null2string(alList_Conditions.get(iRow).get(Constants.HTTP_item_sub_type_code));
                                                    if ( !util.null2string(alList_Conditions.get(iRow).get(Constants.HTTP_item_sub)).equals("") ) {
                                                        ArrayList<HashMap<String, Object>> alList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Conditions.get(iRow).get(
                                                                Constants.HTTP_item_sub);
                                                        if ( sHTTP_item_sub_type_code.equals("02") ) {
                                                            sMode_Code = util.null2string(alList_Item_Sub.get(iTag).get(Constants.HTTP_cmdcls_value));
                                                            sMode = util.null2string(alList_Item_Sub.get(iTag).get(Constants.HTTP_source_sub_name));

                                                            CallMode_SPINNER();
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                        });
                                        util.getView(childView, R.id.btnRowDelete).setTag(iRow1);
                                        util.getView(childView, R.id.btnRowDelete).setOnClickListener(new View.OnClickListener() {
                                            int iTag = 0;

                                            @Override public void onClick(View v) {

                                                iTag = (int) v.getTag();
                                                for ( int iRow = 0; iRow < alList_Conditions.size(); iRow++ ) {
                                                    String sHTTP_item_sub_type_code = util.null2string(alList_Conditions.get(iRow).get(Constants.HTTP_item_sub_type_code));
                                                    ArrayList<HashMap<String, Object>> alList_Item_Sub = new ArrayList<HashMap<String, Object>>();
                                                    if ( !util.null2string(alList_Conditions.get(iRow).get(Constants.HTTP_item_sub)).equals("") ) {
                                                        alList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Conditions.get(iRow).get(Constants.HTTP_item_sub);
                                                        if ( sHTTP_item_sub_type_code.equals("02") ) {
                                                            alList_Item_Sub.remove(iTag);
                                                            if ( alList_Item_Sub.size() == 0 ) {
                                                                alList_Conditions.remove(iRow);
                                                            }
                                                            break;
                                                        }
                                                    }
                                                }
                                                getAlList_Conditions(0);
                                            }
                                        });

                                        String sHTTP_cmdcls_value = util.null2string(hmTempItem_sub.get(Constants.HTTP_cmdcls_value));
                                        String sHTTP_cmdcls_value_msg = util.null2string(hmTempItem_sub.get(Constants.HTTP_cmdcls_value_msg));

                                        Dlog.v("sHTTP_cmdcls_value_msg", sHTTP_cmdcls_value_msg);
                                        util.getTextView(childView, R.id.tvRowState).setText(sHTTP_cmdcls_value_msg);
                                        util.setImage(act, R.drawable.img_rule_icon_homemode, util.getImageView(childView, R.id.ivRowIcon));
                                        util.getLinearLayout(act, R.id.llConditionBody1).addView(childView);
                                    } else if ( sHTTP_item_sub_type_code.equals("21") ) {
                                        //기간
                                        util.getView(act, R.id.ivConditionBody2TitleAdd).setVisibility(View.GONE);
                                        util.getView(childView, R.id.vEmp1).setVisibility(View.VISIBLE);

                                        util.getView(childView, R.id.llRowRoot).setTag(iRow1);
                                        util.getView(childView, R.id.llRowRoot).setOnClickListener(new View.OnClickListener() {
                                            int iTag = 0;

                                            @Override public void onClick(View v) {

                                                iTag = (int) v.getTag();

                                                for ( int iRow = 0; iRow < alList_Conditions.size(); iRow++ ) {
                                                    String sHTTP_item_sub_type_code = util.null2string(alList_Conditions.get(iRow).get(Constants.HTTP_item_sub_type_code));
                                                    if ( !util.null2string(alList_Conditions.get(iRow).get(Constants.HTTP_item_sub)).equals("") ) {
                                                        ArrayList<HashMap<String, Object>> alList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Conditions.get(iRow).get(
                                                                Constants.HTTP_item_sub);
                                                        if ( sHTTP_item_sub_type_code.equals("21") ) {
                                                            String sHTTP_cmdcls_value = util.null2string(alList_Item_Sub.get(iTag).get(Constants.HTTP_cmdcls_value));

                                                            intent = new Intent(act, Rules_Date_Select.class);
                                                            intent.putExtra(Constants.EXTRA_DATE, sHTTP_cmdcls_value);
                                                            util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_DATE_SELECT);
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                        });
                                        util.getView(childView, R.id.btnRowDelete).setTag(iRow1);
                                        util.getView(childView, R.id.btnRowDelete).setOnClickListener(new View.OnClickListener() {
                                            int iTag = 0;

                                            @Override public void onClick(View v) {

                                                iTag = (int) v.getTag();
                                                for ( int iRow = 0; iRow < alList_Conditions.size(); iRow++ ) {
                                                    String sHTTP_item_sub_type_code = util.null2string(alList_Conditions.get(iRow).get(Constants.HTTP_item_sub_type_code));
                                                    ArrayList<HashMap<String, Object>> alList_Item_Sub = new ArrayList<HashMap<String, Object>>();
                                                    if ( !util.null2string(alList_Conditions.get(iRow).get(Constants.HTTP_item_sub)).equals("") ) {
                                                        alList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Conditions.get(iRow).get(Constants.HTTP_item_sub);
                                                        if ( sHTTP_item_sub_type_code.equals("21") ) {
                                                            alList_Item_Sub.remove(iTag);
                                                            if ( alList_Item_Sub.size() == 0 ) {
                                                                alList_Conditions.remove(iRow);
                                                            }
                                                            break;
                                                        }
                                                    }
                                                }
                                                getAlList_Conditions(0);
                                            }
                                        });
                                        String sHTTP_cmdcls_value = util.null2string(hmTempItem_sub.get(Constants.HTTP_cmdcls_value));
                                        String sHTTP_cmdcls_value_msg = util.null2string(hmTempItem_sub.get(Constants.HTTP_cmdcls_value_msg));

                                        util.getTextView(childView, R.id.tvRowState).setText(sHTTP_cmdcls_value);

                                        util.setImage(act, R.drawable.img_rule_icon_calendar, util.getImageView(childView, R.id.ivRowIcon));
                                        util.getLinearLayout(act, R.id.llConditionBody2).addView(childView);
                                    } else if ( sHTTP_item_sub_type_code.equals("01") ) {
                                        //장치
                                        util.getView(childView, R.id.llRowBody_Body).setVisibility(View.VISIBLE);
                                        util.getView(childView, R.id.vEmp1).setVisibility(View.GONE);
                                        util.getView(childView, R.id.llRowRoot).setTag(iRow1);
                                        util.getView(childView, R.id.llRowRoot).setOnClickListener(new View.OnClickListener() {
                                            int iTag = 0;

                                            @Override public void onClick(View v) {

                                                iTag = (int) v.getTag();

                                                for ( int iRow = 0; iRow < alList_Conditions.size(); iRow++ ) {
                                                    String sHTTP_item_sub_type_code = util.null2string(alList_Conditions.get(iRow).get(Constants.HTTP_item_sub_type_code));
                                                    if ( !util.null2string(alList_Conditions.get(iRow).get(Constants.HTTP_item_sub)).equals("") ) {
                                                        ArrayList<HashMap<String, Object>> alList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Conditions.get(iRow).get(
                                                                Constants.HTTP_item_sub);
                                                        if ( sHTTP_item_sub_type_code.equals("01") ) {
                                                            String sHTTP_source_id = util.null2string(alList_Item_Sub.get(iTag).get(Constants.HTTP_source_id));
                                                            String sHTTP_source_sub_id = util.null2string(alList_Item_Sub.get(iTag).get(Constants.HTTP_source_sub_id));
                                                            String sHTTP_available_yn = util.null2string(alList_Item_Sub.get(iTag).get(Constants.HTTP_available_yn));

                                                            if ( sHTTP_available_yn.equals("N") ) {
                                                                util.setShowToast(act, util.getString(act, R.string.MSG900), Toast.LENGTH_SHORT);
                                                                return;
                                                            } else {
                                                                intent = new Intent(act, Rules_Things_Select.class);
                                                                intent.putExtra(Constants.EXTRA_CALL_TYPE, "3");
                                                                intent.putExtra(Constants.EXTRA_TRIGGERS, alList_Triggers);
                                                                intent.putExtra(Constants.EXTRA_ACTIONS, alList_Actions);
                                                                intent.putExtra(Constants.EXTRA_CONDITIONS, alList_Conditions);
                                                                intent.putExtra(Constants.EXTRA_DEVICE_ID, sHTTP_source_id);
                                                                intent.putExtra(Constants.EXTRA_NODE_ID, sHTTP_source_sub_id);
                                                                intent.putExtra(Constants.EXTRA_POSITION, iTag);

                                                                util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_THINGS_SELECT);
                                                            }
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                        });
                                        util.getView(childView, R.id.btnRowDelete).setTag(iRow1);
                                        util.getView(childView, R.id.btnRowDelete).setOnClickListener(new View.OnClickListener() {
                                            int iTag = 0;

                                            @Override public void onClick(View v) {

                                                iTag = (int) v.getTag();
                                                for ( int iRow = 0; iRow < alList_Conditions.size(); iRow++ ) {
                                                    String sHTTP_item_sub_type_code = util.null2string(alList_Conditions.get(iRow).get(Constants.HTTP_item_sub_type_code));
                                                    ArrayList<HashMap<String, Object>> alList_Item_Sub = new ArrayList<HashMap<String, Object>>();
                                                    if ( !util.null2string(alList_Conditions.get(iRow).get(Constants.HTTP_item_sub)).equals("") ) {
                                                        alList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Conditions.get(iRow).get(Constants.HTTP_item_sub);
                                                        if ( sHTTP_item_sub_type_code.equals("01") ) {
                                                            alList_Item_Sub.remove(iTag);
                                                            if ( alList_Item_Sub.size() == 0 ) {
                                                                alList_Conditions.remove(iRow);
                                                            }
                                                            break;
                                                        }
                                                    }
                                                }
                                                getAlList_Conditions(0);
                                            }
                                        });

                                        String sHTTP_source_name = util.null2string(hmTempItem_sub.get(Constants.HTTP_source_name));
                                        String sHTTP_source_sub_name = util.null2string(hmTempItem_sub.get(Constants.HTTP_source_sub_name));
                                        String sHTTP_condition_type_code = util.null2string(hmTempItem_sub.get(Constants.HTTP_condition_type_code));
                                        String sHTTP_condition_type_code_name = util.null2string(hmTempItem_sub.get(Constants.HTTP_condition_type_code_name));
                                        String sHTTP_cmdcls_value = util.null2string(hmTempItem_sub.get(Constants.HTTP_cmdcls_value));
                                        String sHTTP_unit = util.null2string(alList_Item_Sub.get(iRow1).get(Constants.HTTP_unit));
                                        String sHTTP_cmdcls_value_msg = util.null2string(hmTempItem_sub.get(Constants.HTTP_cmdcls_value_msg));
                                        String sHTTP_image_file_name = util.null2string(hmTempItem_sub.get(Constants.HTTP_image_file_name));
                                        //                    String sDayofweek = getAlList_Conditions(1);

                                        String sHTTP_available_yn = util.null2string(hmTempItem_sub.get(Constants.HTTP_available_yn));

                                        if ( sHTTP_available_yn.equals("N") ) {
                                            util.getTextView(childView, R.id.tvRowTitle).setText(util.setSpCenterLine(act, sHTTP_source_name));
                                        } else {
                                            util.getTextView(childView, R.id.tvRowTitle).setText(sHTTP_source_name);
                                        }
                                        util.getTextView(childView, R.id.tvRowBody).setText(sHTTP_source_sub_name);
                                        util.getTextView(childView, R.id.tvRowState).setVisibility(View.VISIBLE);
                                        util.getTextView(childView, R.id.tvRowState).setText((sHTTP_cmdcls_value_msg.equals("") ? sHTTP_cmdcls_value : sHTTP_cmdcls_value_msg)
                                                                                             + sHTTP_unit
                                                                                             + (sHTTP_condition_type_code.equals("01") ? "" : sHTTP_condition_type_code_name));

                                        util.setImage(act, sHTTP_image_file_name, util.getImageView(childView, R.id.ivRowIcon));
                                        util.getLinearLayout(act, R.id.llConditionBody3).addView(childView);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return sReturn;
    }

    private void delAlList_Conditions(int iType) {

        if ( alList_Conditions.size() > 0 ) {
            if ( iType == 1 ) {
                for ( int iRow = 0; iRow < alList_Conditions.size(); iRow++ ) {
                    HashMap<String, Object> hmTemp = new HashMap<String, Object>();
                    hmTemp = alList_Conditions.get(iRow);

                    String sHTTP_item_sub_type_code = util.null2string(hmTemp.get(Constants.HTTP_item_sub_type_code));
                    if ( sHTTP_item_sub_type_code.equals("22") ) {
                        alList_Conditions.remove(iRow);
                        break;
                    }
                }
            } else {

            }
        }
    }
}
