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

package com.ntels.onecable.view.rules;

import com.ntels.onecable.R;
import com.ntels.onecable.common.AppConfig;
import com.ntels.onecable.common.Constants;
import com.ntels.onecable.common.c_interface.OnDataSendToReslut;
import com.ntels.onecable.common.http.HttpSend;
import com.ntels.onecable.common.http.Response;
import com.ntels.onecable.common.util.Dlog;
import com.ntels.onecable.common.util.LogUtil;
import com.ntels.onecable.common.util.Util;
import com.ntels.onecable.common.widget.CustomActionBarActivity;
import com.ntels.onecable.view.dialog.CustomDialog;
import com.ntels.onecable.view.dialog.DialogSpinner;

import org.json.JSONObject;

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.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.Toast;

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

/**
 * <pre>
 * 1. ClassName :   RulesEdit
 * 2. FileName  :   RulesEdit.java
 * 3. Package   :   com.ntels.onecable.view.rules
 * 4. Date      :   2016-11-29
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class RulesEdit extends CustomActionBarActivity implements View.OnClickListener,
        CustomDialog.OnMsgAlterToResult {

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

    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;
    Handler comHandler = new Handler(new IncomingHandlerCallBack());
    private DecimalFormat decimalFormat = new DecimalFormat("00");
    private String homegrpRuleId = "";
    private String ruleUseYn = "N";
    private String ruleName = "";
    private ArrayList<HashMap<String, Object>> conditionList = new ArrayList<>();
    private ArrayList<HashMap<String, Object>> actionList = new ArrayList<>();
    private ArrayList<HashMap<String, Object>> triggerList = new ArrayList<>();
    private String triggers;
    private String actions;
    private String conditions;
    private String customJson = "";
    private ArrayList<String> modeIds = new ArrayList<>();
    private ArrayList<String> modeTitles = new ArrayList<>();
    private ArrayList<String> modeResults = new ArrayList<>();
    private String mode = "";
    private String modeCode = "";
    private int modePosition = -1;
    private int modeType = 2;
    /*통신 관련*/
    private int type = 0;
    /*공통*/
    private Activity act;
    private AppConfig appConfig;
    private Intent intent;
    private Util util = new Util();
    private CustomDialog customDialog = new CustomDialog();
    private String callType = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

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

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

        if (null != getIntent()) {
            Intent intent = getIntent();
            //EXTRA_CALL_TYPE = 1:규칙 만들기 2:규칙수정
            callType = intent.getStringExtra(Constants.EXTRA_CALL_TYPE);
            homegrpRuleId = intent.getStringExtra(Constants.EXTRA_MEMBER_RULE_ID);
        }

        setToolbar();
        setDisp();
    }

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

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

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

    /**
     * UI display.
     */
    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 (callType.equals("2")) {
            comHandler.sendMessageDelayed(comHandler.obtainMessage(MSG_COM_G_RULES_HOMEGRP_RULE_ID), 10);
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onClick(View view) {

        switch (view.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 row = 0; row < actionList.size(); row++) {
                    Dlog.v("222222");
                    String itemSubTypeCode = util.null2string(actionList.get(row)
                            .get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                    if (itemSubTypeCode.equals("03")) {
                        Dlog.v("333333");
                        if (!util.null2string(actionList.get(row).get(Constants.KEY_ITEM_SUB)).equals("")) {
                            Dlog.v("444444");
                            ArrayList<HashMap<String, Object>> itemSubList =
                                    (ArrayList<HashMap<String, Object>>) actionList.get(row)
                                            .get(Constants.KEY_ITEM_SUB);
                            for (int row1 = 0; row1 < itemSubList.size(); row1++) {
                                Dlog.v("555555");
                                String cmdclsValue = util.null2string(itemSubList.get(row1)
                                        .get(Constants.KEY_CMDCLS_VALUE));
                                if (cmdclsValue.equals("")) {
                                    itemSubList.remove(row1);
                                    if (itemSubList.size() == 0) {
                                        actionList.remove(row);
                                    }
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }

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

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

                if (ruleUseYn.equals("X")) {
                    Boolean isCheck = false;
                    if (!isCheck) {
                        if (triggerList.size() > 0) {
                            for (int row = 0; row < triggerList.size(); row++) {
                                ArrayList<HashMap<String, Object>> tempItemSubList =
                                        (ArrayList<HashMap<String, Object>>) triggerList.get(row)
                                                .get(Constants.KEY_ITEM_SUB);

                                for (int row1 = 0; row1 < tempItemSubList.size(); row1++) {
                                    String deleteYn = util.null2string(tempItemSubList.get(row1)
                                            .get(Constants.KEY_DELETE_YN));
                                    if (deleteYn.equals("Y")) {
                                        isCheck = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if (!isCheck) {
                        if (actionList.size() > 0) {
                            for (int row = 0; row < actionList.size(); row++) {
                                ArrayList<HashMap<String, Object>> tempItemSubList =
                                        (ArrayList<HashMap<String, Object>>) actionList.get(row)
                                                .get(Constants.KEY_ITEM_SUB);

                                for (int row1 = 0; row1 < tempItemSubList.size(); row1++) {
                                    String deleteYn = util.null2string(tempItemSubList.get(row1)
                                            .get(Constants.KEY_DELETE_YN));
                                    if (deleteYn.equals("Y")) {
                                        isCheck = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if (!isCheck) {
                        if (conditionList.size() > 0) {
                            for (int row = 0; row < conditionList.size(); row++) {
                                ArrayList<HashMap<String, Object>> tempItemSubList =
                                        (ArrayList<HashMap<String, Object>>) conditionList.get(row)
                                                .get(Constants.KEY_ITEM_SUB);

                                for (int row1 = 0; row1 < tempItemSubList.size(); row1++) {
                                    String deleteYn = util.null2string(tempItemSubList.get(row1)
                                            .get(Constants.KEY_DELETE_YN));
                                    if (deleteYn.equals("Y")) {
                                        isCheck = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    if (isCheck) {
                        customDialog.msgAlert(act, Constants.MSG875);
                        return;
                    }
                }

                if (callType.equals("1")) {
                    comHandler.sendMessage(comHandler.obtainMessage(MSG_COM_P_RULES));
                } else if (callType.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 (!view.isSelected()) {
                    Intent intent = new Intent(act, RulesTriggersSelect.class);
                    intent.putExtra(Constants.EXTRA_TRIGGERS, triggerList);
                    intent.putExtra(Constants.EXTRA_CONDITIONS, conditionList);
                    util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_TRIGGERS_SELECT);
                }
                break;
            case R.id.llActionTitle:
                util.callBtnGoogleAnalytics(act, "Set_Action", "");
                intent = new Intent(act, RulesThingsSelect.class);
                intent.putExtra(Constants.EXTRA_CALL_TYPE, "2");
                intent.putExtra(Constants.EXTRA_TRIGGERS, triggerList);
                intent.putExtra(Constants.EXTRA_ACTIONS, actionList);
                intent.putExtra(Constants.EXTRA_CONDITIONS, conditionList);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_THINGS_SELECT);
                //                }
                break;
            case R.id.llAlramTitle:
                if (!view.isSelected()) {
                    util.callBtnGoogleAnalytics(act, "Set_Msg", "");

                    HashMap<String, Object> itemSubMap = new HashMap<>();
                    String sourceId = "";
                    itemSubMap.put(Constants.KEY_SOURCE_ID, sourceId);
                    String sourceName = "";
                    itemSubMap.put(Constants.KEY_SOURCE_NAME, sourceName);
                    String sourceSubId = "";
                    itemSubMap.put(Constants.KEY_SOURCE_SUB_ID, sourceSubId);
                    String sourceSubName = "";
                    itemSubMap.put(Constants.KEY_SOURCE_SUB_NAME, sourceSubName);
                    String conditionsTypeCode = "02";
                    itemSubMap.put(Constants.KEY_CONDITION_TYPE_CODE, conditionsTypeCode);
                    String conditionTypeCodeName = "";
                    itemSubMap.put(Constants.KEY_CONDITION_TYPE_CODE_NAME, conditionTypeCodeName);
                    String cmdclsValue = "";
                    itemSubMap.put(Constants.KEY_CMDCLS_VALUE, cmdclsValue);
                    String cmdclsValueMsg = "";
                    itemSubMap.put(Constants.KEY_CMDCLS_VALUE_MSG, cmdclsValueMsg);
                    String unit = "";
                    itemSubMap.put(Constants.KEY_UNIT, unit);
                    String dataTypeCode = "";
                    itemSubMap.put(Constants.KEY_DATA_TYPE_CODE, dataTypeCode);
                    String dayOfWeek = "";
                    itemSubMap.put(Constants.KEY_DAYOFWEEK, dayOfWeek);
                    String repeatable = "";
                    itemSubMap.put(Constants.KEY_REPEATABLE, repeatable);
                    String hour = "";
                    itemSubMap.put(Constants.KEY_HOUR, hour);
                    String minute = "";
                    itemSubMap.put(Constants.KEY_MINUTE, minute);
                    String imageFileName = "";
                    itemSubMap.put(Constants.KEY_IMAGE_FILE_NAME, imageFileName);
                    String deleteYn = "";
                    itemSubMap.put(Constants.KEY_DELETE_YN, deleteYn);
                    itemSubMap.put(Constants.KEY_ACTION_TYPE_CODE, "00");

                    ArrayList<HashMap<String, Object>> tempItemSubList = new ArrayList<>();
                    HashMap<String, Object> hmTempActions = new HashMap<>();
                    //액션이 아예 없는 경우
                    tempItemSubList.add(itemSubMap);
                    hmTempActions.put(Constants.KEY_ITEM_NAME,
                            util.getString(act, R.string.layout_rules_detail_04));
                    hmTempActions.put(Constants.KEY_ITEM_SUB_TYPE_CODE, "03");
                    hmTempActions.put(Constants.KEY_ITEM_SUB, tempItemSubList);
                    actionList.add(hmTempActions);

                    setDispActions();
                }
                break;
            case R.id.llConditionTitle:
                if (!view.isSelected()) {
                    view.setSelected(true);
                    util.getView(act, R.id.llConditionBody).setVisibility(View.VISIBLE);
                } else {
                    view.setSelected(false);
                    util.getView(act, R.id.llConditionBody).setVisibility(View.GONE);
                }
                break;
            case R.id.llConditionBody1Title:
                if (!view.isSelected()) {
                    util.callBtnGoogleAnalytics(act, "Set_Condition", "Condi_Homemode");
                    mode = "";
                    modeCode = "";
                    modePosition = -1;
                    modeResults.clear();
                    callModeSpinner();
                }
                break;
            case R.id.llConditionBody2Title:
                if (!view.isSelected()) {
                    util.callBtnGoogleAnalytics(act, "Set_Condition", "Condi_Period");
                    intent = new Intent(act, RulesDateSelect.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, RulesThingsSelect.class);
                intent.putExtra(Constants.EXTRA_CALL_TYPE, "3");
                intent.putExtra(Constants.EXTRA_TRIGGERS, triggerList);
                intent.putExtra(Constants.EXTRA_ACTIONS, actionList);
                intent.putExtra(Constants.EXTRA_CONDITIONS, conditionList);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_THINGS_SELECT);
                break;
            default:
                break;
        }
    }

    /**
     * Spinner display.
     */
    void callModeSpinner() {

        if (modeIds.size() > 0) {
            if (!modeCode.equals("")) {
                modeResults.clear();
                String[] strArray = modeCode.split("\\,", 100);
                for (String s : strArray) {
                    for (int row1 = 0; row1 < modeIds.size(); row1++) {
                        if (s.equals(modeIds.get(row1))) {
                            modeResults.add("" + row1);
                            break;
                        }
                    }
                }
            } else {
                for (int row1 = 0; row1 < modeIds.size(); row1++) {
                    modeResults.add("" + row1);
                }
            }

            intent = new Intent();
            intent.setClass(act, DialogSpinner.class);
            intent.putExtra(Constants.EXTRA_SPINNER_LIST1, modeTitles);
            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, modeType);
            if (modeType == 2) {
                intent.putExtra(Constants.EXTRA_SPINNER_POSITION_LIST, modeResults);
            } else {
                intent.putExtra(Constants.EXTRA_SPINNER_POSITION, modePosition);
            }
            util.callActivityForResultNoAnim(act, intent, Constants.ACT_SPINNER);
        } else {
            comHandler.sendMessage(comHandler.obtainMessage(MSG_COM_G_MODES));
        }
    }

    /**
     * 홈 규칙 등록(규칙을 등록한다) 요청.
     */
    private void sendPostRules() {
        LogUtil.i(TAG, "actionList : " + actionList.toString());

        try {
            HttpSend httpSend;

            httpSend = new HttpSend(act);

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

                    resultPostRules(result);
                }
            });

            ruleName = util.getEditTextVal(act, R.id.edtRulesName);
            HashMap<String, Object> hmTemp = new HashMap<>();
            hmTemp.put(Constants.KEY_RULE_NAME, ruleName);
            hmTemp.put(Constants.KEY_USE_YN, "Y");
            hmTemp.put(Constants.KEY_TRIGGERS, triggerList);
            hmTemp.put(Constants.KEY_ACTIONS, actionList);
            hmTemp.put(Constants.KEY_CONDITIONS, conditionList);

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

    /**
     * 홈 규칙 등록(규칙을 등록한다) 요청 결과.
     *
     * @param result 결과 data
     */
    private void resultPostRules(Response result) {

        try {
            if (result.getStatusCode() == 200) {
                String itemSubTypeCode = util.null2string(triggerList.get(0)
                        .get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                intent = new Intent(act, RulesAddSucess.class);
                intent.putExtra(Constants.EXTRA_CALL_TYPE, itemSubTypeCode);
                intent.putExtra(Constants.EXTRA_RULE_NAME, ruleName);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_ADD_SUCESS);
            } else {
                JSONObject joResult = new JSONObject(result.getContent());
                Dlog.v(joResult.toString());
                String errorCode = util.null2string(joResult.get(Constants.KEY_ERR_CODE));
                String errorMsg = util.null2string(joResult.get(Constants.KEY_ERR_MSG));

                switch (errorCode) {
                    case "401":
                        customDialog.msgAlert(act, Constants.MSG998, errorMsg);
                        break;
                    case "401-106":
                        customDialog.msgAlert(act, Constants.MSG997, errorMsg);
                        break;
                    default:
                        customDialog.msgAlert(act, Constants.MSG000, errorMsg);
                        break;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.msgAlert(act, Constants.MSG807);
        }
    }

    /**
     * 홈 규칙 수정(해당 규칙을 수정한다) 요청.
     */
    private void sendPutRules() {

        try {
            HttpSend httpSend;

            httpSend = new HttpSend(act);

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

                    resultPutRules(result);
                }
            });

            ruleName = util.getEditTextVal(act, R.id.edtRulesName);
            HashMap<String, Object> hmTemp = new HashMap<>();
            hmTemp.put(Constants.KEY_RULE_NAME, ruleName);
            hmTemp.put(Constants.KEY_USE_YN, "Y");
            hmTemp.put(Constants.KEY_TRIGGERS, triggerList);
            hmTemp.put(Constants.KEY_ACTIONS, actionList);
            hmTemp.put(Constants.KEY_CONDITIONS, conditionList);

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

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

    /**
     * 홈 규칙 수정(해당 규칙을 수정한다) 요청 결과.
     *
     * @param result 결과 data
     */
    private void resultPutRules(Response result) {

        try {
            if (result.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(result.getContent());
                Dlog.v(joResult.toString());
                String errorCode = util.null2string(joResult.get(Constants.KEY_ERR_CODE));
                String errorMsg = util.null2string(joResult.get(Constants.KEY_ERR_MSG));

                switch (errorCode) {
                    case "401":
                        customDialog.msgAlert(act, Constants.MSG998, errorMsg);
                        break;
                    case "401-106":
                        customDialog.msgAlert(act, Constants.MSG997, errorMsg);
                        break;
                    default:
                        customDialog.msgAlert(act, Constants.MSG000, errorMsg);
                        break;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.msgAlert(act, Constants.MSG807);
        }
    }

    /**
     * 홈모드 조회(홈그룹의 홈모드 목록을 불러온다) 요청.
     */
    private void sendGetModes() {

        try {
            HttpSend httpSend;

            httpSend = new HttpSend(act);

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

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

    /**
     * 홈모드 조회(홈그룹의 홈모드 목록을 불러온다) 요청 결과.
     *
     * @param result 결과 data
     */
    private void resultGetModes(Response result) {

        try {
            if (result.getStatusCode() == 200) {
                ArrayList<HashMap<String, Object>> alList = util.jsonToArrayListMap(result.getContent());
                if (alList.size() > 0) {
                    for (int row = 0; row < alList.size(); row++) {
                        HashMap<String, Object> hmTemp;
                        hmTemp = alList.get(row);
                        String modeId = util.null2string(hmTemp.get(Constants.KEY_MODE_ID));
                        String modeName = util.null2string(hmTemp.get(Constants.KEY_MODE_NAME));
                        modeIds.add(modeId);
                        modeTitles.add(modeName);
                    }
                }
                callModeSpinner();
            } else {
                JSONObject joResult = new JSONObject(result.getContent());
                Dlog.v(joResult.toString());
                String errorCode = util.null2string(joResult.get(Constants.KEY_ERR_CODE));
                String errorMsg = util.null2string(joResult.get(Constants.KEY_ERR_MSG));

                switch (errorCode) {
                    case "401":
                        customDialog.msgAlert(act, Constants.MSG998, errorMsg);
                        break;
                    case "401-106":
                        customDialog.msgAlert(act, Constants.MSG997, errorMsg);
                        break;
                    default:
                        customDialog.msgAlert(act, Constants.MSG000, errorMsg);
                        break;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.msgAlert(act, Constants.MSG807);
        }
    }

    /**
     * 홈 규칙 상세 조회(해당 규칙의 정보를 상세 조회한다) 요청.
     */
    private void sendGetRulesDetail() {

        try {
            HttpSend httpSend;

            httpSend = new HttpSend(act);

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

                    resultGetRulesDetail(result);
                }
            });
            httpSend.G_rules_homegrp_rule_id(homegrpRuleId);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 홈 규칙 상세 조회(해당 규칙의 정보를 상세 조회한다) 요청 결과.
     *
     * @param result 결과 data
     */
    private void resultGetRulesDetail(Response result) {

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

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

                switch (errorCode) {
                    case "401":
                        customDialog.msgAlert(act, Constants.MSG998, errorMsg);
                        break;
                    case "401-106":
                        customDialog.msgAlert(act, Constants.MSG997, errorMsg);
                        break;
                    default:
                        customDialog.msgAlert(act, Constants.MSG000, errorMsg);
                        break;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.msgAlert(act, Constants.MSG807);
        }
    }

    @Override
    public void onBackPressed() {

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

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

        switch (msgId) {

            case Constants.MSG998:
                util.callApp_Start(act, "LOGOUT");
                break;
            case Constants.MSG997:
                util.callApp_Start(act, "HOME_RELOAD");
                break;
            case Constants.MSG807:
                if (btnResult == 1) {
                    comHandler.sendMessageDelayed(comHandler.obtainMessage(type), 10);
                }
                break;
            case Constants.MSG875:
                if (btnResult == 1) {

                    if (triggerList.size() > 0) {
                        for (int row = 0; row < triggerList.size(); row++) {
                            ArrayList<HashMap<String, Object>> tempItemSubList =
                                    (ArrayList<HashMap<String, Object>>) triggerList.get(row)
                                            .get(Constants.KEY_ITEM_SUB);

                            for (int row1 = tempItemSubList.size() - 1; row1 >= 0; row1--) {
                                String deleteYn = util.null2string(tempItemSubList.get(row1)
                                        .get(Constants.KEY_DELETE_YN));
                                if (deleteYn.equals("Y")) {
                                    tempItemSubList.remove(row1);
                                    if (tempItemSubList.size() == 0) {
                                        triggerList.remove(row);
                                    }
                                }
                            }
                        }
                    }
                    if (actionList.size() > 0) {
                        for (int row = 0; row < actionList.size(); row++) {
                            ArrayList<HashMap<String, Object>> tempItemSubList =
                                    (ArrayList<HashMap<String, Object>>) actionList.get(row)
                                            .get(Constants.KEY_ITEM_SUB);

                            for (int row1 = tempItemSubList.size() - 1; row1 >= 0; row1--) {
                                String deleteYn = util.null2string(tempItemSubList.get(row1)
                                        .get(Constants.KEY_DELETE_YN));
                                if (deleteYn.equals("Y")) {
                                    tempItemSubList.remove(row1);
                                    if (tempItemSubList.size() == 0) {
                                        actionList.remove(row);
                                    }
                                }
                            }
                        }
                    }
                    if (conditionList.size() > 0) {
                        for (int row = 0; row < conditionList.size(); row++) {
                            ArrayList<HashMap<String, Object>> tempItemSubList =
                                    (ArrayList<HashMap<String, Object>>) conditionList.get(row)
                                            .get(Constants.KEY_ITEM_SUB);

                            for (int row1 = tempItemSubList.size() - 1; row1 >= 0; row1--) {
                                String deleteYn = util.null2string(tempItemSubList.get(row1)
                                        .get(Constants.KEY_DELETE_YN));
                                if (deleteYn.equals("Y")) {
                                    tempItemSubList.remove(row1);
                                    if (tempItemSubList.size() == 0) {
                                        conditionList.remove(row);
                                    }
                                }
                            }
                        }
                    }
                    ruleUseYn = "N";

                    setDispTriggers();
                    setDispActions();
                    getAlList_Conditions(0);
                    util.getView(act, R.id.llBtn1).performClick();
                }
                break;
            default:
                break;
        }
    }

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

        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Constants.ACT_RULES_ADD_SUCESS:
                    act.setResult(RESULT_OK, data);
                    finish();
                    break;
                case Constants.ACT_RULES_TRIGGERS_SELECT:
                    triggerList = (ArrayList<HashMap<String, Object>>) data.getSerializableExtra(
                            Constants.EXTRA_TRIGGERS);
                    conditionList = (ArrayList<HashMap<String, Object>>) data.getSerializableExtra(
                            Constants.EXTRA_CONDITIONS);

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

                    setDispActions();
                    getAlList_Conditions(0);
                    break;
                case Constants.ACT_SPINNER:
                    mode = "";
                    modeCode = "";
                    if (modePosition != data.getIntExtra(Constants.EXTRA_SPINNER_RESULT, -1)) {
                        modePosition = data.getIntExtra(Constants.EXTRA_SPINNER_RESULT, 0);
                        mode = util.null2string(modeTitles.get(modePosition));
                        modeCode = util.null2string(modeIds.get(modePosition));
                    } else {
                        modeResults = data.getStringArrayListExtra(Constants.EXTRA_SPINNER_RESULT_LIST);
                        if (modeResults != null) {
                            if (modeResults.size() > 0) {
                                for (int row = 0; row < modeResults.size(); row++) {
                                    if (row > 0) {
                                        mode += ",";
                                        modeCode += ",";
                                    }
                                    mode += util.null2empty(modeTitles.get(util.str2int(modeResults.get(row))));
                                    modeCode += util.null2empty(modeIds.get(util.str2int(modeResults.get(row))));
                                }
                            }
                        }
                    }

                    if (!mode.equals("")) {
                        Dlog.v("mode", mode);
                        Dlog.v("modeCode", modeCode);

                        HashMap<String, Object> itemSubMap = new HashMap<>();
                        String sourceId = "";
                        itemSubMap.put(Constants.KEY_SOURCE_ID, sourceId);
                        String sourceName = "";
                        itemSubMap.put(Constants.KEY_SOURCE_NAME, sourceName);
                        String sourceSubId = "";
                        itemSubMap.put(Constants.KEY_SOURCE_SUB_ID, sourceSubId);
                        String sourceSubName = "";
                        itemSubMap.put(Constants.KEY_SOURCE_SUB_NAME, sourceSubName);
                        String conditionTypeCode = "";
                        itemSubMap.put(Constants.KEY_CONDITION_TYPE_CODE, conditionTypeCode);
                        String conditionTypeCodeName = "";
                        itemSubMap.put(Constants.KEY_CONDITION_TYPE_CODE_NAME, conditionTypeCodeName);
                        String cmdclsValue = modeCode;
                        itemSubMap.put(Constants.KEY_CMDCLS_VALUE, cmdclsValue);
                        String cmdclsValueMsg = mode;
                        itemSubMap.put(Constants.KEY_CMDCLS_VALUE_MSG, cmdclsValueMsg);
                        String unit = "";
                        itemSubMap.put(Constants.KEY_UNIT, unit);
                        String dataTypeCode = "";
                        itemSubMap.put(Constants.KEY_DATA_TYPE_CODE, dataTypeCode);
                        String dayOfWeek = "";
                        itemSubMap.put(Constants.KEY_DAYOFWEEK, dayOfWeek);
                        String repeatable = "";
                        itemSubMap.put(Constants.KEY_REPEATABLE, repeatable);
                        String hour = "";
                        itemSubMap.put(Constants.KEY_HOUR, hour);
                        String minute = "";
                        itemSubMap.put(Constants.KEY_MINUTE, minute);
                        String imageFileName = "";
                        itemSubMap.put(Constants.KEY_IMAGE_FILE_NAME, imageFileName);
                        String deleteYn = "";
                        itemSubMap.put(Constants.KEY_DELETE_YN, deleteYn);

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

                        HashMap<String, Object> hmTempConditions = new HashMap<>();
                        Boolean isCheck = false;
                        if (conditionList.size() > 0) {
                            for (int row = 0; row < conditionList.size(); row++) {
                                String itemSubTypeCode = util.null2string(conditionList.get(row)
                                        .get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                                if (itemSubTypeCode.equals("02")) {
                                    itemSubList = (ArrayList<HashMap<String, Object>>) conditionList.get(row)
                                            .get(Constants.KEY_ITEM_SUB);
                                    itemSubList.clear();
                                    itemSubList.add(itemSubMap);
                                    isCheck = true;
                                    break;
                                }
                            }
                        }
                        if (!isCheck) {
                            itemSubList.add(itemSubMap);
                            //컨디션은 있지만 모드가 없는경우 추가하는경우
                            hmTempConditions.put(Constants.KEY_ITEM_NAME,
                                    util.getString(act, R.string.layout_rules_edit_07));
                            hmTempConditions.put(Constants.KEY_ITEM_SUB_TYPE_CODE, "02");
                            hmTempConditions.put(Constants.KEY_ITEM_SUB, itemSubList);
                            conditionList.add(hmTempConditions);
                        }
                        getAlList_Conditions(0);
                    }
                    break;
                case Constants.ACT_RULES_DATE_SELECT:
                    String date = data.getStringExtra(Constants.EXTRA_DATE);
                    if (!date.equals("")) {
                        Dlog.v("date", date);

                        HashMap<String, Object> itemSubMap = new HashMap<>();
                        String sourceId = "";
                        itemSubMap.put(Constants.KEY_SOURCE_ID, sourceId);
                        String sourceName = "";
                        itemSubMap.put(Constants.KEY_SOURCE_NAME, sourceName);
                        String sourceSubId = "";
                        itemSubMap.put(Constants.KEY_SOURCE_SUB_ID, sourceSubId);
                        String sourceSubName = "";
                        itemSubMap.put(Constants.KEY_SOURCE_SUB_NAME, sourceSubName);
                        String conditionTypeCode = "";
                        itemSubMap.put(Constants.KEY_CONDITION_TYPE_CODE, conditionTypeCode);
                        String conditionTypeCodeName = "";
                        itemSubMap.put(Constants.KEY_CONDITION_TYPE_CODE_NAME, conditionTypeCodeName);
                        itemSubMap.put(Constants.KEY_CMDCLS_VALUE, date);
                        String cmdclsValueMsg = "";
                        itemSubMap.put(Constants.KEY_CMDCLS_VALUE_MSG, cmdclsValueMsg);
                        String unit = "";
                        itemSubMap.put(Constants.KEY_UNIT, unit);
                        String dataTypeCode = "";
                        itemSubMap.put(Constants.KEY_DATA_TYPE_CODE, dataTypeCode);
                        String dayOfWeek = "";
                        itemSubMap.put(Constants.KEY_DAYOFWEEK, dayOfWeek);
                        String repeatable = "";
                        itemSubMap.put(Constants.KEY_REPEATABLE, repeatable);
                        String hour = "";
                        itemSubMap.put(Constants.KEY_HOUR, hour);
                        String minute = "";
                        itemSubMap.put(Constants.KEY_MINUTE, minute);
                        String imageFileName = "";
                        itemSubMap.put(Constants.KEY_IMAGE_FILE_NAME, imageFileName);
                        String deleteYn = "";
                        itemSubMap.put(Constants.KEY_DELETE_YN, deleteYn);

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

                        HashMap<String, Object> hmTempConditions = new HashMap<>();
                        Boolean isCheck = false;
                        if (conditionList.size() > 0) {
                            for (int row = 0; row < conditionList.size(); row++) {
                                String itemSubTypeCode = util.null2string(conditionList.get(row)
                                        .get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                                if (itemSubTypeCode.equals("21")) {
                                    tempItemSubList = (ArrayList<HashMap<String, Object>>) conditionList.get(row)
                                            .get(Constants.KEY_ITEM_SUB);
                                    tempItemSubList.clear();
                                    tempItemSubList.add(itemSubMap);
                                    isCheck = true;
                                    break;
                                }
                            }
                        }
                        if (!isCheck) {
                            tempItemSubList.add(itemSubMap);
                            //컨디션은 있지만 날짜가 없는경우 추가하는경우
                            hmTempConditions.put(Constants.KEY_ITEM_NAME,
                                    util.getString(act, R.string.layout_rules_edit_08));
                            hmTempConditions.put(Constants.KEY_ITEM_SUB_TYPE_CODE, "21");
                            hmTempConditions.put(Constants.KEY_ITEM_SUB, tempItemSubList);
                            conditionList.add(hmTempConditions);
                        }
                        getAlList_Conditions(0);
                    }
                    break;
                default:
                    break;
            }
        } else {
            switch (requestCode) {
                default:
                    break;
            }
        }
    }

    /**
     * Rule json data parsing & set.
     *
     * @param content parsing할 json string
     */
    void setRulesJsonInit(String content) {

        try {
            JSONObject joResult = new JSONObject(content);

            homegrpRuleId = util.null2string(joResult.get(Constants.KEY_HOMEGRP_RULE_ID));
            ruleName = util.null2string(joResult.get(Constants.KEY_RULE_NAME));
            util.getEditText(act, R.id.edtRulesName).setText(ruleName);

            ruleUseYn = util.null2string(joResult.get(Constants.KEY_USE_YN));

            conditions = util.null2empty(joResult.get(Constants.KEY_CONDITIONS)).toString();
            triggers = util.null2empty(joResult.get(Constants.KEY_TRIGGERS)).toString();
            actions = util.null2empty(joResult.get(Constants.KEY_ACTIONS)).toString();

            setDispConditionsInit();
            setDispTriggersInit();
            setDispActionsInit();
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.msgAlert(act, Constants.MSG807);
        }
    }

    /**
     * Triggers data initialize.
     */
    void setDispTriggersInit() {

        Dlog.v("triggers=", triggers);
        triggerList.clear();
        triggerList = util.jsonToArrayListMap(triggers);
        setDispTriggers();
    }

    /**
     * Triggers display.
     */
    @SuppressWarnings("unchecked")
    void setDispTriggers() {

        Dlog.v("alList_Triggers1 = ", triggerList.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 (triggerList.size() > 0) {
            //현재로써는 트리거는 단일임 나중에 멀티일때 풀어야 함
            //            for ( int row = 0; row < triggerList.size(); row++ ) {
            int row = 0;
            HashMap<String, Object> hmTemp;
            hmTemp = triggerList.get(row);
            LinearLayout childView = (LinearLayout) View.inflate(
                    this, R.layout.row_rules_triggers_list, null);
            util.getView(childView, R.id.btnRowDelete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    deleteConditions(1);
                    triggers = customJson;
                    setDispTriggersInit();
                }
            });

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

                    Intent intent = new Intent(act, RulesTriggersSelect.class);
                    intent.putExtra(Constants.EXTRA_TRIGGERS, triggerList);
                    intent.putExtra(Constants.EXTRA_CONDITIONS, conditionList);
                    util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_TRIGGERS_SELECT);
                }
            });

            String itemName = "";//util.null2string(hmTemp.get(Constants.KEY_item_name));
            String itemSubTypeCode = util.null2string(hmTemp.get(Constants.KEY_ITEM_SUB_TYPE_CODE));

            util.getView(act, R.id.llTriggerTitle).setSelected(true);
            util.getView(act, R.id.ivTriggerAdd).setVisibility(View.GONE);
            ArrayList<HashMap<String, Object>> itemSubList;
            if (!util.null2string(hmTemp.get(Constants.KEY_ITEM_SUB)).equals("")) {
                itemSubList = (ArrayList<HashMap<String, Object>>) hmTemp.get(Constants.KEY_ITEM_SUB);
                int row1 = 0;
                switch (itemSubTypeCode) {
                    case "05": {
                        //time
                        itemName = util.getString(act, R.string.layout_rules_triggers_select_02);
                        String httpHour = util.null2string(itemSubList.get(row1).get(Constants.KEY_HOUR));
                        String minute = util.null2string(itemSubList.get(row1).get(Constants.KEY_MINUTE));
                        String[] saTime = util.UTCtoLocal6(httpHour + minute);
                        String ampm = (util.str2int(saTime[0]) >= 12
                                ? util.getString(act, R.string.layout_pm)
                                : util.getString(act, R.string.layout_am));
                        String hour = (util.str2int(saTime[0]) >= 12
                                ? decimalFormat.format((util.str2int(saTime[0]) - 12)) : saTime[0]);
                        String dayOfWeek = getAlList_Conditions(1);

                        if (appConfig.getLanguage_Id().equals("ko")) {
                            util.getTextView(childView, R.id.tvRowTitle).setText(ampm + " " + hour
                                    + util.getString(act, R.string.layout_hour) + " " + saTime[1]
                                    + util.getString(act, R.string.layout_minute));
                        } else {
                            util.getTextView(childView, R.id.tvRowTitle).setText(hour
                                    + util.getString(act, R.string.layout_hour)
                                    + saTime[1]
                                    + util.getString(act, R.string.layout_minute)
                                    + " "
                                    + ampm);
                        }

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

                        util.setImage(R.drawable.img_rule_item_sub_type_code_05,
                                util.getImageView(childView, R.id.ivRowIcon));
                        break;
                    }
                    case "32": {
                        //sun
                        itemName = util.getString(act, R.string.layout_rules_triggers_select_03);
                        String sourceName = util.null2string(itemSubList.get(row1)
                                .get(Constants.KEY_SOURCE_NAME));
                        String sourceSubName = util.null2string(itemSubList.get(row1)
                                .get(Constants.KEY_SOURCE_SUB_NAME));
                        String dayOfWeek = getAlList_Conditions(1);

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

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

                        util.setImage(R.drawable.img_rule_item_sub_type_code_32,
                                util.getImageView(childView, R.id.ivRowIcon));
                        break;
                    }
                    case "31": {
                        //더울 때 /추울때
                        itemName = util.getString(act, R.string.layout_rules_triggers_select_04);
                        String sourceName = util.null2string(itemSubList.get(row1)
                                .get(Constants.KEY_SOURCE_NAME));
                        String conditionTypeCode = util.null2string(itemSubList.get(row1)
                                .get(Constants.KEY_CONDITION_TYPE_CODE));
                        String cmdclsValue = util.null2string(itemSubList.get(row1)
                                .get(Constants.KEY_CMDCLS_VALUE));
                        String dayOfWeek = getAlList_Conditions(1);

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

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

                        util.getTextView(childView, R.id.tvRowBody)
                                .setText(sourceName + "/" + cmdclsValue
                                        + util.getString(act, R.string.layout_temperature1) + "/" + dayOfWeek);
                        util.getTextView(childView, R.id.tvRowState).setVisibility(View.GONE);

                        util.setImage(R.drawable.img_rule_item_sub_type_code_31,
                                util.getImageView(childView, R.id.ivRowIcon));
                        break;
                    }
                    case "01": {
                        //상태가 변할때
                        itemName = util.getString(act, R.string.layout_rules_triggers_select_05);

                        String sourceName = util.null2string(itemSubList.get(row1)
                                .get(Constants.KEY_SOURCE_NAME));
                        String deleteYn = util.null2string(itemSubList.get(row1)
                                .get(Constants.KEY_DELETE_YN));
                        if (deleteYn.equals("Y")) {
                            util.getTextView(childView, R.id.tvRowTitle)
                                    .setText(util.setSpCenterLine(act, sourceName));
                            // 삭제된 장치일 경우 트리거 셀렉트화면으로 이동하지 않는다.
                            util.getView(childView, R.id.llRowRoot)
                                    .setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {

                                            util.setShowToast(act, util.getString(act, R.string.MSG900),
                                                    Toast.LENGTH_SHORT);
                                        }
                                    });
                        } else {
                            util.getTextView(childView, R.id.tvRowTitle).setText(sourceName);
                        }
                        String sourceSubName = util.null2string(itemSubList.get(row1)
                                .get(Constants.KEY_SOURCE_SUB_NAME));
                        util.getTextView(childView, R.id.tvRowBody).setText(sourceSubName);
                        util.getTextView(childView, R.id.tvRowState).setVisibility(View.VISIBLE);
                        String conditionTypeCode = util.null2string(itemSubList.get(row)
                                .get(Constants.KEY_CONDITION_TYPE_CODE));
                        String conditionTypeCodeName = util.null2string(itemSubList.get(row)
                                .get(Constants.KEY_CONDITION_TYPE_CODE_NAME));
                        String cmdclsValue = util.null2string(itemSubList.get(row1)
                                .get(Constants.KEY_CMDCLS_VALUE));
                        String cmdclsValueMsg = util.null2string(itemSubList.get(row1)
                                .get(Constants.KEY_CMDCLS_VALUE_MSG));
                        String unit = util.null2string(itemSubList.get(row1)
                                .get(Constants.KEY_UNIT));
                        util.getTextView(childView, R.id.tvRowState).setText((cmdclsValueMsg.equals("")
                                ? cmdclsValue : cmdclsValueMsg) + unit + (
                                conditionTypeCode.equals("01")
                                        ? ""
                                        : conditionTypeCodeName));
                        String imageFileName = util.null2string(itemSubList.get(row1)
                                .get(Constants.KEY_IMAGE_FILE_NAME));
                        util.setImage(act, imageFileName, util.getImageView(childView, R.id.ivRowIcon));
                        break;
                    }
                    default:
                        break;
                }
            }
            util.getTextView(act, R.id.tvTriggerTitle).setText(itemName);
            util.getLinearLayout(act, R.id.llTriggerBody).addView(childView);
        }
    }

    /**
     * Actions data initialize.
     */
    void setDispActionsInit() {

        Dlog.v("actions=", actions);
        actionList.clear();
        actionList = util.jsonToArrayListMap(actions);
        setDispActions();
    }

    /**
     * Actions display.
     */
    @SuppressWarnings("unchecked")
    void setDispActions() {

        Dlog.v("alList_Actions1 = ", actionList.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 (actionList.size() > 0) {
            for (int row = 0; row < actionList.size(); row++) {
                HashMap<String, Object> hmTemp;
                hmTemp = actionList.get(row);

                String itemSubTypeCode = util.null2string(hmTemp.get(Constants.KEY_ITEM_SUB_TYPE_CODE));

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

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

                                        @Override
                                        public void onClick(View view) {

                                            tag = (int) view.getTag();

                                            for (int row = 0; row < actionList.size(); row++) {
                                                String itemSubTypeCode = util.null2string(actionList.get(row)
                                                        .get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                                                if (!util.null2string(actionList.get(row)
                                                        .get(Constants.KEY_ITEM_SUB)).equals("")) {
                                                    ArrayList<HashMap<String, Object>> itemSubList =
                                                            (ArrayList<HashMap<String, Object>>) actionList.get(row)
                                                                    .get(Constants.KEY_ITEM_SUB);
                                                    if (itemSubTypeCode.equals("01")) {
                                                        String sourceId = util.null2string(itemSubList.get(tag)
                                                                .get(Constants.KEY_SOURCE_ID));
                                                        String sourceSubId = util.null2string(itemSubList.get(tag)
                                                                .get(Constants.KEY_SOURCE_SUB_ID));
                                                        String deleteYn = util.null2string(itemSubList.get(tag)
                                                                .get(Constants.KEY_DELETE_YN));

                                                        if (deleteYn.equals("Y")) {
                                                            util.setShowToast(act, util.getString(act, R.string.MSG900),
                                                                    Toast.LENGTH_SHORT);
                                                            return;
                                                        } else {
                                                            intent = new Intent(act, RulesThingsSelect.class);
                                                            intent.putExtra(Constants.EXTRA_CALL_TYPE, "2");
                                                            intent.putExtra(Constants.EXTRA_TRIGGERS, triggerList);
                                                            intent.putExtra(Constants.EXTRA_ACTIONS, actionList);
                                                            intent.putExtra(Constants.EXTRA_CONDITIONS, conditionList);
                                                            intent.putExtra(Constants.EXTRA_DEVICE_ID, sourceId);
                                                            intent.putExtra(Constants.EXTRA_NODE_ID, sourceSubId);
                                                            intent.putExtra(Constants.EXTRA_POSITION, tag);

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

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

                                        @Override
                                        public void onClick(View view) {

                                            tag = (int) view.getTag();
                                            for (int row = 0; row < actionList.size(); row++) {
                                                String itemSubTypeCode = util.null2string(actionList.get(row)
                                                        .get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                                                ArrayList<HashMap<String, Object>> itemSubList;
                                                if (!util.null2string(actionList.get(row)
                                                        .get(Constants.KEY_ITEM_SUB)).equals("")) {
                                                    itemSubList = (ArrayList<HashMap<String, Object>>) actionList.get(row)
                                                            .get(Constants.KEY_ITEM_SUB);
                                                    if (itemSubTypeCode.equals("01")) {
                                                        itemSubList.remove(tag);
                                                        if (itemSubList.size() == 0) {
                                                            actionList.remove(row);
                                                        }
                                                        break;
                                                    }
                                                }
                                            }
                                            setDispActions();
                                        }
                                    });

                            String deleteYn = util.null2string(itemSubList.get(row1)
                                    .get(Constants.KEY_DELETE_YN));
                            String sourceName = util.null2string(itemSubList.get(row1)
                                    .get(Constants.KEY_SOURCE_NAME));
                            if (deleteYn.equals("Y")) {
                                util.getTextView(childView, R.id.tvRowTitle)
                                        .setText(util.setSpCenterLine(act, sourceName));
                            } else {
                                util.getTextView(childView, R.id.tvRowTitle)
                                        .setText(sourceName);
                            }
                            String sourceSubName = util.null2string(itemSubList.get(row1)
                                    .get(Constants.KEY_SOURCE_SUB_NAME));
                            util.getTextView(childView, R.id.tvRowBody).setText(sourceSubName);
                            util.getTextView(childView, R.id.tvRowState).setVisibility(View.VISIBLE);
                            String conditionTypeCode = util.null2string(itemSubList.get(row1)
                                    .get(Constants.KEY_CONDITION_TYPE_CODE));
                            String conditionTypeCodeName = util.null2string(itemSubList.get(row1)
                                    .get(Constants.KEY_CONDITION_TYPE_CODE_NAME));
                            String cmdclsValue = util.null2string(itemSubList.get(row1)
                                    .get(Constants.KEY_CMDCLS_VALUE));
                            String unit = util.null2string(itemSubList.get(row1).get(Constants.KEY_UNIT));
                            String cmdclsValueMsg = util.null2string(itemSubList.get(row1)
                                    .get(Constants.KEY_CMDCLS_VALUE_MSG));
                            util.getTextView(childView, R.id.tvRowState).setText((cmdclsValueMsg.equals("")
                                    ? cmdclsValue : cmdclsValueMsg) + unit + (
                                    conditionTypeCode.equals("01")
                                            ? ""
                                            : conditionTypeCodeName));

                            String imageFileName = util.null2string(itemSubList.get(row1)
                                    .get(Constants.KEY_IMAGE_FILE_NAME));
                            util.setImage(act, imageFileName, util.getImageView(childView, R.id.ivRowIcon));
                            util.getLinearLayout(act, R.id.llActionBody).addView(childView);
                        }
                    } else if (itemSubTypeCode.equals("03")) {
                        util.getView(act, R.id.llAlramTitle).setSelected(true);
                        util.getView(act, R.id.ivAlramAdd).setVisibility(View.GONE);
                        int row1 = 0;
                        LinearLayout childView = (LinearLayout) View.inflate(
                                this, R.layout.row_rules_actions_alram_list1, null);

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

                                    @Override
                                    public void onClick(View view) {

                                        tag = (int) view.getTag();
                                        for (int row = 0; row < actionList.size(); row++) {
                                            String itemSubTypeCode = util.null2string(actionList.get(row)
                                                    .get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                                            ArrayList<HashMap<String, Object>> itemSubList;
                                            if (!util.null2string(actionList.get(row)
                                                    .get(Constants.KEY_ITEM_SUB)).equals("")) {
                                                itemSubList = (ArrayList<HashMap<String, Object>>) actionList.get(row)
                                                        .get(Constants.KEY_ITEM_SUB);
                                                if (itemSubTypeCode.equals("03")) {
                                                    itemSubList.remove(tag);
                                                    if (itemSubList.size() == 0) {
                                                        actionList.remove(row);
                                                    }
                                                    util.getView(act, R.id.llAlramTitle).setSelected(false);
                                                    break;
                                                }
                                            }
                                        }
                                        setDispActions();
                                    }
                                });
                        String cmdclsValue = util.null2string(itemSubList.get(row1)
                                .get(Constants.KEY_CMDCLS_VALUE));

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

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

                            }

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

                                previousString = chars.toString();
                            }

                            @Override
                            public void afterTextChanged(Editable editable) {

                                previousString = editable.toString();
                                int tag = (int) edtBody.getTag();

                                for (int row = 0; row < actionList.size(); row++) {
                                    String itemSubTypeCode = util.null2string(actionList.get(row)
                                            .get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                                    if (!util.null2string(actionList.get(row)
                                            .get(Constants.KEY_ITEM_SUB)).equals("")) {
                                        ArrayList<HashMap<String, Object>> itemSubList =
                                                (ArrayList<HashMap<String, Object>>) actionList.get(row)
                                                        .get(Constants.KEY_ITEM_SUB);
                                        if (itemSubTypeCode.equals("03")) {
                                            itemSubList.get(tag).put(Constants.KEY_CMDCLS_VALUE, previousString);
                                            break;
                                        }
                                    }
                                }
                            }
                        });
                        util.getLinearLayout(act, R.id.llAlramBody).addView(childView);
                    }
                }
            }
        }
    }

    /**
     * Conditions data initialize.
     */
    void setDispConditionsInit() {

        Dlog.v("actions=", actions);
        conditionList.clear();
        conditionList = util.jsonToArrayListMap(conditions);
        getAlList_Conditions(0);
    }

    @SuppressWarnings("unchecked")
    private String getAlList_Conditions(int type) {
        //        type   = 1: 요일 담아주기
        String retValue = "";
        if (type == 1) {
            if (conditionList.size() > 0) {
                for (int row = 0; row < conditionList.size(); row++) {
                    HashMap<String, Object> hmTemp;
                    hmTemp = conditionList.get(row);

                    String itemSubTypeCode = util.null2string(hmTemp.get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                    if (itemSubTypeCode.equals("22")) {
                        //요일
                        ArrayList<HashMap<String, Object>> itemSubList;
                        if (!util.null2string(hmTemp.get(Constants.KEY_ITEM_SUB)).equals("")) {
                            itemSubList = (ArrayList<HashMap<String, Object>>) hmTemp
                                    .get(Constants.KEY_ITEM_SUB);
                            if (itemSubList.size() > 0) {
                                HashMap<String, Object> tempItemSubMap;
                                tempItemSubMap = itemSubList.get(0);

                                retValue = util.null2string(tempItemSubMap
                                        .get(Constants.KEY_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 (conditionList.size() > 0) {
                //수정필요
                for (int row = 0; row < conditionList.size(); row++) {
                    HashMap<String, Object> hmTemp;
                    hmTemp = conditionList.get(row);

                    String itemSubTypeCode = util.null2string(hmTemp.get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                    if (!itemSubTypeCode.equals("22")) {
                        util.getView(act, R.id.llConditionBody).setVisibility(View.VISIBLE);
                        util.getView(act, R.id.llConditionTitle).setSelected(true);
                        ArrayList<HashMap<String, Object>> itemSubList;
                        if (!util.null2string(hmTemp.get(Constants.KEY_ITEM_SUB)).equals("")) {
                            itemSubList = (ArrayList<HashMap<String, Object>>) hmTemp.get(
                                    Constants.KEY_ITEM_SUB);
                            if (itemSubList.size() > 0) {
                                for (int row1 = 0; row1 < itemSubList.size(); row1++) {
                                    HashMap<String, Object> tempItemSubMap;
                                    tempItemSubMap = itemSubList.get(row1);

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

                                    switch (itemSubTypeCode) {
                                        case "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(row1);
                                            util.getView(childView, R.id.llRowRoot).setOnClickListener(
                                                    new View.OnClickListener() {
                                                        int tag = 0;

                                                        @Override
                                                        public void onClick(View view) {

                                                            tag = (int) view.getTag();

                                                            for (int row = 0; row < conditionList.size(); row++) {
                                                                String itemSubTypeCode = util.null2string(conditionList.get(row)
                                                                        .get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                                                                if (!util.null2string(conditionList.get(row)
                                                                        .get(Constants.KEY_ITEM_SUB)).equals("")) {
                                                                    ArrayList<HashMap<String, Object>> itemSubList =
                                                                            (ArrayList<HashMap<String, Object>>) conditionList.get(row)
                                                                                    .get(Constants.KEY_ITEM_SUB);
                                                                    if (itemSubTypeCode.equals("02")) {
                                                                        modeCode = util.null2string(itemSubList.get(tag)
                                                                                .get(Constants.KEY_CMDCLS_VALUE));
                                                                        mode = util.null2string(itemSubList.get(tag)
                                                                                .get(Constants.KEY_SOURCE_SUB_NAME));

                                                                        callModeSpinner();
                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    });
                                            util.getView(childView, R.id.btnRowDelete).setTag(row1);
                                            util.getView(childView, R.id.btnRowDelete)
                                                    .setOnClickListener(new View.OnClickListener() {
                                                        int tag = 0;

                                                        @Override
                                                        public void onClick(View view) {

                                                            tag = (int) view.getTag();
                                                            for (int row = 0; row < conditionList.size(); row++) {
                                                                String itemSubTypeCode = util.null2string(conditionList.get(row)
                                                                        .get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                                                                ArrayList<HashMap<String, Object>> itemSubList;
                                                                if (!util.null2string(conditionList.get(row)
                                                                        .get(Constants.KEY_ITEM_SUB)).equals("")) {
                                                                    itemSubList = (ArrayList<HashMap<String, Object>>)
                                                                            conditionList.get(row).get(Constants.KEY_ITEM_SUB);
                                                                    if (itemSubTypeCode.equals("02")) {
                                                                        itemSubList.remove(tag);
                                                                        if (itemSubList.size() == 0) {
                                                                            conditionList.remove(row);
                                                                        }
                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                            getAlList_Conditions(0);
                                                        }
                                                    });

                                            String cmdclsValueMsg = util.null2string(tempItemSubMap
                                                    .get(Constants.KEY_CMDCLS_VALUE_MSG));

                                            Dlog.v("cmdclsValueMsg", cmdclsValueMsg);
                                            util.getTextView(childView, R.id.tvRowState).setText(cmdclsValueMsg);
                                            util.setImage(R.drawable.img_rule_icon_homemode,
                                                    util.getImageView(childView, R.id.ivRowIcon));
                                            util.getLinearLayout(act, R.id.llConditionBody1).addView(childView);
                                            break;
                                        }
                                        case "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(row1);
                                            util.getView(childView, R.id.llRowRoot)
                                                    .setOnClickListener(new View.OnClickListener() {
                                                        int tag = 0;

                                                        @Override
                                                        public void onClick(View view) {

                                                            tag = (int) view.getTag();

                                                            for (int row = 0; row < conditionList.size(); row++) {
                                                                String itemSubTypeCode = util.null2string(conditionList.get(row)
                                                                        .get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                                                                if (!util.null2string(conditionList.get(row)
                                                                        .get(Constants.KEY_ITEM_SUB)).equals("")) {
                                                                    ArrayList<HashMap<String, Object>> itemSubList =
                                                                            (ArrayList<HashMap<String, Object>>) conditionList.get(row)
                                                                                    .get(Constants.KEY_ITEM_SUB);
                                                                    if (itemSubTypeCode.equals("21")) {
                                                                        String cmdclsValue = util.null2string(itemSubList.get(tag)
                                                                                .get(Constants.KEY_CMDCLS_VALUE));

                                                                        intent = new Intent(act, RulesDateSelect.class);
                                                                        intent.putExtra(Constants.EXTRA_DATE, cmdclsValue);
                                                                        util.callActivityForResultNoAnim(act, intent,
                                                                                Constants.ACT_RULES_DATE_SELECT);
                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    });
                                            util.getView(childView, R.id.btnRowDelete).setTag(row1);
                                            util.getView(childView, R.id.btnRowDelete)
                                                    .setOnClickListener(new View.OnClickListener() {
                                                        int tag = 0;

                                                        @Override
                                                        public void onClick(View view) {

                                                            tag = (int) view.getTag();
                                                            for (int row = 0; row < conditionList.size(); row++) {
                                                                String itemSubTypeCode = util.null2string(conditionList.get(row)
                                                                        .get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                                                                ArrayList<HashMap<String, Object>> itemSubList;
                                                                if (!util.null2string(conditionList.get(row)
                                                                        .get(Constants.KEY_ITEM_SUB)).equals("")) {
                                                                    itemSubList = (ArrayList<HashMap<String, Object>>)
                                                                            conditionList.get(row).get(Constants.KEY_ITEM_SUB);
                                                                    if (itemSubTypeCode.equals("21")) {
                                                                        itemSubList.remove(tag);
                                                                        if (itemSubList.size() == 0) {
                                                                            conditionList.remove(row);
                                                                        }
                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                            getAlList_Conditions(0);
                                                        }
                                                    });
                                            String cmdclsValue = util.null2string(tempItemSubMap
                                                    .get(Constants.KEY_CMDCLS_VALUE));

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

                                            util.setImage(R.drawable.img_rule_icon_calendar,
                                                    util.getImageView(childView, R.id.ivRowIcon));
                                            util.getLinearLayout(act, R.id.llConditionBody2).addView(childView);
                                            break;
                                        }
                                        case "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(row1);
                                            util.getView(childView, R.id.llRowRoot)
                                                    .setOnClickListener(new View.OnClickListener() {
                                                        int tag = 0;

                                                        @Override
                                                        public void onClick(View view) {

                                                            tag = (int) view.getTag();

                                                            for (int row = 0; row < conditionList.size(); row++) {
                                                                String itemSubTypeCode = util.null2string(conditionList.get(row)
                                                                        .get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                                                                if (!util.null2string(conditionList.get(row)
                                                                        .get(Constants.KEY_ITEM_SUB)).equals("")) {
                                                                    ArrayList<HashMap<String, Object>> itemSubList =
                                                                            (ArrayList<HashMap<String, Object>>) conditionList.get(row)
                                                                                    .get(Constants.KEY_ITEM_SUB);
                                                                    if (itemSubTypeCode.equals("01")) {
                                                                        String sourceId = util.null2string(itemSubList.get(tag)
                                                                                .get(Constants.KEY_SOURCE_ID));
                                                                        String sourceSubId = util.null2string(itemSubList.get(tag)
                                                                                .get(Constants.KEY_SOURCE_SUB_ID));
                                                                        String deleteYn = util.null2string(itemSubList.get(tag)
                                                                                .get(Constants.KEY_DELETE_YN));

                                                                        if (deleteYn.equals("Y")) {
                                                                            util.setShowToast(act, util.getString(act, R.string.MSG900),
                                                                                    Toast.LENGTH_SHORT);
                                                                            return;
                                                                        } else {
                                                                            intent = new Intent(act, RulesThingsSelect.class);
                                                                            intent.putExtra(Constants.EXTRA_CALL_TYPE, "3");
                                                                            intent.putExtra(Constants.EXTRA_TRIGGERS, triggerList);
                                                                            intent.putExtra(Constants.EXTRA_ACTIONS, actionList);
                                                                            intent.putExtra(Constants.EXTRA_CONDITIONS, conditionList);
                                                                            intent.putExtra(Constants.EXTRA_DEVICE_ID, sourceId);
                                                                            intent.putExtra(Constants.EXTRA_NODE_ID, sourceSubId);
                                                                            intent.putExtra(Constants.EXTRA_POSITION, tag);

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

                                                        @Override
                                                        public void onClick(View view) {

                                                            tag = (int) view.getTag();
                                                            for (int row = 0; row < conditionList.size(); row++) {
                                                                String itemSubTypeCode = util.null2string(conditionList.get(row)
                                                                        .get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                                                                ArrayList<HashMap<String, Object>> itemSubList;
                                                                if (!util.null2string(conditionList.get(row)
                                                                        .get(Constants.KEY_ITEM_SUB)).equals("")) {
                                                                    itemSubList = (ArrayList<HashMap<String, Object>>)
                                                                            conditionList.get(row).get(Constants.KEY_ITEM_SUB);
                                                                    if (itemSubTypeCode.equals("01")) {
                                                                        itemSubList.remove(tag);
                                                                        if (itemSubList.size() == 0) {
                                                                            conditionList.remove(row);
                                                                        }
                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                            getAlList_Conditions(0);
                                                        }
                                                    });

                                            String sourceName = util.null2string(tempItemSubMap
                                                    .get(Constants.KEY_SOURCE_NAME));
                                            String deleteYn = util.null2string(tempItemSubMap
                                                    .get(Constants.KEY_DELETE_YN));
                                            if (deleteYn.equals("Y")) {
                                                util.getTextView(childView, R.id.tvRowTitle)
                                                        .setText(util.setSpCenterLine(act, sourceName));
                                            } else {
                                                util.getTextView(childView, R.id.tvRowTitle).setText(sourceName);
                                            }
                                            String sourceSubName = util.null2string(tempItemSubMap
                                                    .get(Constants.KEY_SOURCE_SUB_NAME));
                                            util.getTextView(childView, R.id.tvRowBody).setText(sourceSubName);
                                            util.getTextView(childView, R.id.tvRowState).setVisibility(View.VISIBLE);
                                            String conditionTypeCode = util.null2string(tempItemSubMap.get(
                                                    Constants.KEY_CONDITION_TYPE_CODE));
                                            String conditionTypeCodeName = util.null2string(tempItemSubMap.get(
                                                    Constants.KEY_CONDITION_TYPE_CODE_NAME));
                                            String cmdclsValue = util.null2string(tempItemSubMap
                                                    .get(Constants.KEY_CMDCLS_VALUE));
                                            String unit = util.null2string(itemSubList.get(row1)
                                                    .get(Constants.KEY_UNIT));
                                            String cmdclsValueMsg = util.null2string(tempItemSubMap
                                                    .get(Constants.KEY_CMDCLS_VALUE_MSG));
                                            util.getTextView(childView, R.id.tvRowState)
                                                    .setText((cmdclsValueMsg.equals("") ? cmdclsValue : cmdclsValueMsg)
                                                            + unit
                                                            + (conditionTypeCode.equals("01") ? "" : conditionTypeCodeName));

                                            String imageFileName = util.null2string(tempItemSubMap
                                                    .get(Constants.KEY_IMAGE_FILE_NAME));
                                            util.setImage(act, imageFileName,
                                                    util.getImageView(childView, R.id.ivRowIcon));
                                            util.getLinearLayout(act, R.id.llConditionBody3).addView(childView);
                                            break;
                                        }
                                        default:
                                            break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return retValue;
    }

    /**
     * condition delete.
     *
     * @param type 삭제 할 condition type
     */
    private void deleteConditions(int type) {

        if (conditionList.size() > 0) {
            if (type == 1) {
                for (int row = 0; row < conditionList.size(); row++) {
                    HashMap<String, Object> hmTemp;
                    hmTemp = conditionList.get(row);

                    String itemSubtypeCode = util.null2string(hmTemp.get(Constants.KEY_ITEM_SUB_TYPE_CODE));
                    if (itemSubtypeCode.equals("22")) {
                        conditionList.remove(row);
                        break;
                    }
                }
            }
        }
    }

    class IncomingHandlerCallBack implements Handler.Callback {

        @Override
        public boolean handleMessage(Message msg) {

            type = msg.what;
            switch (msg.what) {
                case MSG_COM_G_RULES_HOMEGRP_RULE_ID:
                    sendGetRulesDetail();
                    break;
                case MSG_COM_G_MODES:
                    sendGetModes();
                    break;
                case MSG_COM_P_RULES:
                    sendPostRules();
                    break;
                case MSG_COM_P_RULES_HOMEGRP_RULE_ID:
                    sendPutRules();
                    break;

                default:
                    break;
            }
            return true;
        }
    }
}
