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

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

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

import com.ntels.kneet.v3.common.AppConfig;
import com.ntels.kneet.v3.common.Constants;
import com.ntels.kneet.v3.common.db.SQLManager;
import com.ntels.kneet.v3.common.http.HttpSend;
import com.ntels.kneet.v3.common.http.Response;
import com.ntels.kneet.v3.common.c_interface.Main_Interface;
import com.ntels.kneet.v3.common.c_interface.OnDataSendToReslut;
import com.ntels.kneet.v3.common.util.Dlog;
import com.ntels.kneet.v3.common.util.Util;
import com.ntels.kneet.v3.common.widget.CustomActionBarActivity;
import com.ntels.kneet.v3.R;
import com.ntels.kneet.v3.view.dialog.CustomDialog;
import com.ntels.kneet.v3.view.rules.Rules_Things_Select;

import org.json.JSONObject;

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

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

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

    //    private View callChildView;

    private TextView tvTempBody;

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

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

    /*공통*/
    private Activity       act;
    private AppConfig      appConfig;
    private Main_Interface mCallback;
    private Intent         intent;
    private              Util         util                        = new Util();
    private              CustomDialog customDialog                = new CustomDialog();
    private              String       sCallType                   = "";
    private              SQLManager   sqlManager                  = new SQLManager();
    private              Boolean      bAutoLogin                  = false;
    private              Boolean      bSaveId                     = false;
    private              Boolean      bRuleDeleteAll              = false;
    private              String       sMODE_ID                    = "";
    private              String       sMODE_NAME                  = "";
    private              String       sMODE_ICON1                 = "";
    private              String       sMODE_DEL_YN                = "";
    private              String       sHOMEGRP_RULE_ID            = "";
    private              String       sFileName                   = "";
    private              String       sHTTP_push_type_mode_chg_yn = "";
    private              String       sHTTP_push_type_rule_exc_yn = "";
    private              int          iAlram_Type                 = 0;
    private static final int          ALRAM_MODE_ON               = 1;
    private static final int          ALRAM_MODE_OFF              = 2;
    private static final int          ALRAM_RULE_ON               = 3;
    private static final int          ALRAM_RULE_OFF              = 4;

    /**
     * <PRE>
     * 1. MethodName: onCreate
     * 2. ClassName :
     * 3. Comment   : onCreate
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    @Override public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

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

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

        if ( savedInstanceState != null ) {
            // sTitle =
            // savedInstanceState.getString(Constants.EXTRA_SPINNER_TITLE);
            // aList1 =
            // savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST1);
            // aList2 =
            // savedInstanceState.getStringArrayList(Constants.EXTRA_SPINNER_LIST2);
            // // 0: 세로 한개 선택, 1: 가로 한개선택
            // iType = savedInstanceState.getInt(Constants.EXTRA_SPINNER_TYPE,
            // 0);
            // sBtn1 =
            // savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN1);
            // sBtn2 =
            // savedInstanceState.getString(Constants.EXTRA_SPINNER_BTN2);
            // iPosition =
            // savedInstanceState.getInt(Constants.EXTRA_SPINNER_POSITION, 0);
        } else {
            Intent intent = getIntent();
            sMODE_ID = intent.getStringExtra(Constants.EXTRA_MODE_ID);
            sMODE_ICON1 = intent.getStringExtra(Constants.EXTRA_MODE_ICON1);
            sMODE_NAME = intent.getStringExtra(Constants.EXTRA_MODE_NAME);
            sMODE_DEL_YN = intent.getStringExtra(Constants.EXTRA_MODE_DEL_YN);
            sHOMEGRP_RULE_ID = intent.getStringExtra(Constants.EXTRA_HOMEGRP_RULE_ID);
        }

        setToolbar();
        setDisp();
    }

    private void setToolbar() {

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

        util.getTextView(toolbar, R.id.tvToolbarTitle1).setText(util.getString(act, R.string.layout_mode_edit_01));
    }

    private void setDisp() {

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

        util.getTextView(act, R.id.tvTitle).setText(sMODE_NAME);
        util.setImage(act, sMODE_ICON1, util.getImageView(act, R.id.ivModeIcon));

        if ( !sHOMEGRP_RULE_ID.equals("") ) {
            ComHandler.sendMessageDelayed(ComHandler.obtainMessage(MSG_COM_G_MODE_SCENES_MODE_ID), 10);
        }
    }

    @Override public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @Override public void onClick(View v) {

        switch (v.getId()) {
            case R.id.llBtn1:
                //                if ( alList_Actions.size() == 0 ) {
                //                    util.setShowToast(act, util.getString(act, R.string.MSG832), Toast.LENGTH_SHORT);
                //                    return;
                //                }
                if ( !sMODE_DEL_YN.equals("0") ) {
                    Boolean bCheck = false;
                    if ( !bCheck ) {
                        if ( alList_Actions.size() > 0 ) {
                            for ( int iRow = 0; iRow < alList_Actions.size(); iRow++ ) {
                                ArrayList<HashMap<String, Object>> alTempList_Item_Sub = (ArrayList<HashMap<String, Object>>) alList_Actions.get(iRow).get(Constants.HTTP_item_sub);

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

                    if ( bCheck ) {
                        customDialog.MsgAlert(act, Constants.MSG875);
                        return;
                    }
                }
                if ( bRuleDeleteAll ) {
                    // 기존에 설정되있던 규칙들을 모두 삭제 시 모드 규칙 삭제 API 수행
                    ComHandler.sendMessage(ComHandler.obtainMessage(MSG_COM_D_MODES_MODE_ID_RULE));
                } else if ( alList_Actions.size() > 0 ){
                    // 모드 규칙 등록 및 수정 API 수행
                    ComHandler.sendMessage(ComHandler.obtainMessage(MSG_COM_P_MODE_SCENES_MODE_ID));
                } else {
                    // 아무 변화 없을 시
                    onBackPressed();
                }
                break;
            case R.id.llBtn2:
                onBackPressed();
                break;
            case R.id.llActionAdd:
                intent = new Intent(act, Rules_Things_Select.class);
                intent.putExtra(Constants.EXTRA_CALL_TYPE, "4");
                intent.putExtra(Constants.EXTRA_ACTIONS, alList_Actions);
                util.callActivityForResultNoAnim(act, intent, Constants.ACT_RULES_THINGS_SELECT);
                break;
            default:
                break;
        }
    }
    Handler ComHandler = new Handler(new IncomingHandlerCallBack());

    class IncomingHandlerCallBack implements Handler.Callback {

        @Override public boolean handleMessage(Message msg) {

            iType = msg.what;
            switch (msg.what) {
                case MSG_COM_P_MODE_SCENES_MODE_ID:
                    //dialog_show();
                    send_P_mode_scenes_mode_id();
                    break;
                case MSG_COM_G_MODE_SCENES_MODE_ID:
                    //dialog_show();
                    send_G_mode_scenes_mode_id();
                    break;
                case MSG_COM_D_MODES_MODE_ID_RULE:
                    send_D_modes_mode_id_rule();
                    break;

                default:
                    break;
            }
            return true;
        }
    }
    /**
     * <PRE>
     * 1. MethodName: send_G_mode_scenes_mode_id
     * 2. ClassName :
     * 3. Comment   : [Send][GET]홈그룹 모드의 멀티제어 상세 조회(해당 홈모드 멀티제어의 정보를 상세 조회한다.)
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    private void send_G_mode_scenes_mode_id() {

        try {
            HttpSend httpSend;

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

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

                    result_G_mode_scenes_mode_id(result);
                }
            });
            httpSend.G_mode_scenes_mode_id(sMODE_ID);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * <PRE>
     * 1. MethodName: result_G_mode_scenes_mode_id
     * 2. ClassName :
     * 3. Comment   : [Result][GET]홈그룹 모드의 멀티제어 상세 조회(해당 홈모드 멀티제어의 정보를 상세 조회한다.)
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    private void result_G_mode_scenes_mode_id(Response rpResult) {

        try {
            if ( rpResult.getStatusCode() == 200 ) {
                ArrayList<HashMap<String, Object>> alList = util.jsonToArrayListMap(rpResult.getContent());
                if ( alList.size() > 0 ) {
                    HashMap<String, Object> hmTempActions = new HashMap<String, Object>();
                    ArrayList<HashMap<String, Object>> alTempList_Item_Sub = new ArrayList<HashMap<String, Object>>();
                    for ( int iRow = 0; iRow < alList.size(); iRow++ ) {
                        HashMap<String, Object> hmTemp = new HashMap<String, Object>();
                        hmTemp = alList.get(iRow);
                        String sHTTP_available_yn = util.null2string(hmTemp.get(Constants.HTTP_available_yn));
                        //액션이 아예 없는 경우
                        alTempList_Item_Sub.add(hmTemp);
                    }
                    hmTempActions.put(Constants.HTTP_item_name, util.getString(act, R.string.layout_rules_detail_03));
                    hmTempActions.put(Constants.HTTP_item_sub_type_code, "01");
                    hmTempActions.put(Constants.HTTP_item_sub, alTempList_Item_Sub);
                    alList_Actions.add(hmTempActions);
                }
                setDispActions();
            } else {
                JSONObject joResult = new JSONObject(rpResult.getContent());
                Dlog.v(joResult.toString());
                String sERR_CODE = util.null2string(joResult.get(Constants.HTTP_err_code));
                String sERR_MSG = util.null2string(joResult.get(Constants.HTTP_err_msg));

                if ( sERR_CODE.equals("401") ) {
                    customDialog.MsgAlert(act, Constants.MSG998, sERR_MSG);
                } else if ( sERR_CODE.equals("401-106") ) {
                    customDialog.MsgAlert(act, Constants.MSG997, sERR_MSG);
                } else {
                    customDialog.MsgAlert(act, Constants.MSG000, sERR_MSG);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            customDialog.MsgAlert(act, Constants.MSG807);
        }
    }
    /**
     * <PRE>
     * 1. MethodName: send_P_mode_scenes_mode_id
     * 2. ClassName :
     * 3. Comment   : [Send][POST]홈그룹 모드의 멀티제어 등록&수정(멀티제어를 등록&수정한다.)
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    private void send_P_mode_scenes_mode_id() {

        try {
            HttpSend httpSend;

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

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

                    result_P_mode_scenes_mode_id(result);
                }
            });

            HashMap<String, Object> hmTemp = new HashMap<String, Object>();
            Dlog.v("alList_Actions = " + alList_Actions.size());
            hmTemp = alList_Actions.get(0);

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

            httpSend.P_mode_scenes_mode_id(sMODE_ID, util.MapToJson(hmTemp));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * <PRE>
     * 1. MethodName: result_P_mode_scenes_mode_id
     * 2. ClassName :
     * 3. Comment   : [Result][POST]홈그룹 모드의 멀티제어 등록&수정(멀티제어를 등록&수정한다.)
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    private void result_P_mode_scenes_mode_id(Response rpResult) {

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

                String sHTTP_homegrp_rule_id = util.null2string(joResult.get(Constants.HTTP_homegrp_rule_id));
                intent = new Intent();
                intent.putExtra(Constants.EXTRA_MODE_DEL_YN, sMODE_DEL_YN);
                intent.putExtra(Constants.EXTRA_HOMEGRP_RULE_ID, sHTTP_homegrp_rule_id);
                act.setResult(RESULT_OK, intent);
                finish();
            } else {
                JSONObject joResult = new JSONObject(rpResult.getContent());
                Dlog.v(joResult.toString());
                String sERR_CODE = util.null2string(joResult.get(Constants.HTTP_err_code));
                String sERR_MSG = util.null2string(joResult.get(Constants.HTTP_err_msg));

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

    /**
     * <PRE>
     * 1. MethodName: send_D_modes_mode_id_rule
     * 2. ClassName :
     * 3. Comment   : [Send][DELETE]홈그룹 모드의 규칙 삭제
     * 4. Developer : 정백호
     * 5. Created   : 2016. 06.
     * </PRE>
     */
    private void send_D_modes_mode_id_rule() {

        try {
            HttpSend httpSend;

            httpSend = new HttpSend(act);

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

                    result_D_modes_mode_id_rule(result);
                }
            });

            httpSend.D_modes_mode_id_rule(sMODE_ID);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * <PRE>
     * 1. MethodName: result_D_modes_mode_id_rule
     * 2. ClassName :
     * 3. Comment   : [Result][DELETE]홈그룹 모드의 규칙 삭제
     * 4. Developer : 정백호
     * 5. Created   : 2016. 06.
     * </PRE>
     */
    private void result_D_modes_mode_id_rule(Response rpResult) {

        try {
            if ( rpResult.getStatusCode() == 200 ) {
                intent = new Intent();
                act.setResult(RESULT_OK, intent);
                finish();
            } else {
                JSONObject joResult = new JSONObject(rpResult.getContent());
                Dlog.v(joResult.toString());
                String sERR_CODE = util.null2string(joResult.get(Constants.HTTP_err_code));
                String sERR_MSG = util.null2string(joResult.get(Constants.HTTP_err_msg));

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

    /**
     * <PRE>
     * 1. MethodName: onBackPressed
     * 2. ClassName :
     * 3. Comment   : 뒤로가기 버튼
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    @Override public void onBackPressed() {

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

    /**
     * <PRE>
     * 1. MethodName: onResume
     * 2. ClassName :
     * 3. Comment   : onResume
     * 4. Developer : 최진오
     * 5. Created   : 2015. 10.
     * </PRE>
     */
    @Override public void onResume() {

        super.onResume();
    }

    @Override public void resultCustomDialogResponse(int iMsgID, int iBtnResult) {

        switch (iMsgID) {

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

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

                            for ( int iRow1 = alTempList_Item_Sub.size() - 1; iRow1 >= 0; iRow1-- ) {
                                String sHTTP_available_yn = util.null2string(alTempList_Item_Sub.get(iRow1).get(Constants.HTTP_available_yn));
                                if ( sHTTP_available_yn.equals("N") ) {
                                    alTempList_Item_Sub.remove(iRow1);
                                    if ( alTempList_Item_Sub.size() == 0 ) {
                                        alList_Actions.remove(iRow);
                                    }
                                }
                            }
                        }
                    }
                    sMODE_DEL_YN = "0";
                    setDispActions();
                    util.getView(act, R.id.llBtn1).performClick();
                }
                break;
            default:
                break;
        }
    }

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

        super.onActivityResult(requestCode, resultCode, data);
        if ( resultCode == act.RESULT_OK ) {
            switch (requestCode) {
                case Constants.ACT_RULES_THINGS_SELECT:
                    alList_Actions = (ArrayList<HashMap<String, Object>>) data.getSerializableExtra(Constants.EXTRA_ACTIONS);
                    bRuleDeleteAll = false;
                    setDispActions();
                    break;
                default:
                    break;
            }
        } else {
            switch (requestCode) {
                default:
                    break;
            }
        }
    }

    void setDispActions() {

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

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

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

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

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

                                @Override public void onClick(View v) {

                                    iTag = (int) v.getTag();

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

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

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

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

                                @Override public void onClick(View v) {

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

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

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

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