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

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

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ntels.kneet.v3.common.AppConfig;
import com.ntels.kneet.v3.common.Constants;
import com.ntels.kneet.v3.common.c_interface.List_Interface;
import com.ntels.kneet.v3.common.util.Util;
import com.ntels.kneet.v3.R;

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

public class Fragment_Things_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List_Interface mAdapterCallback;
    private AppConfig      appConfig;
    private             Util                               util        = new Util();
    public static final int                                TYPE_HEADER = 0;
    public static final int                                TYPE_ITEM   = 1;
    public static final int                                TYPE_FOOTER = 2;
    private             ArrayList<HashMap<String, Object>> alTotalList = new ArrayList<HashMap<String, Object>>();
    private LayoutInflater inflater;
    private Context        mContext;
    private int            mListLayoutHead;
    private int            mListLayoutItem;
    private int            mListLayoutFooter;
    private              int     iType                          = 0;
    private static final int     MSG_COM_DEVICES_NODES_COMMANDS = 1;
    private              String  sHTTP_request_value            = "";
    private              String  sHTTP_device_id                = "";
    private              String  sHTTP_node_id                  = "";
    private              int     iHTTP_position                 = 0;
    private              String  sHTTP_value                    = "";
    private              Boolean bSend                          = false;
    private              Boolean bListCheck                     = false;
    private              Boolean bHeaderCheck                   = false;
    private              Boolean bFooterCheck                   = false;
    private              Boolean bFooterVisible                 = false;
    private              int     iFooterCnt                     = -1;

    public String getHTTP_Value() {

        return sHTTP_value;
    }

    public void setHTTP_Value(String sVal) {

        sHTTP_value = sVal;
    }

    public void setListCheck(boolean bVal) {

        bListCheck = bVal;
    }

    public boolean getListCheck() {

        return bListCheck;
    }

    public void setHeaderCheck(boolean bVal) {

        bHeaderCheck = bVal;
    }

    public boolean getHeaderCheck() {

        return bHeaderCheck;
    }

    public void setFooterCheck(boolean bVal) {

        bFooterCheck = bVal;
    }

    public boolean getFooterCheck() {

        return bFooterCheck;
    }

    public void setFooterVisible(boolean bVal) {

        bFooterVisible = bVal;
    }

    public boolean getFooterVisible() {

        return bFooterVisible;
    }

    @Override public int getItemCount() {

        return alTotalList.size();
    }

    public Fragment_Things_Adapter(Context tContext, int listLayoutHead, int listLayoutItem, int listLayoutFooter, ArrayList<HashMap<String, Object>> alTmp, List_Interface mAdapterCallback) {

        mContext = tContext;
        alTotalList = alTmp;
        mListLayoutHead = listLayoutHead;
        mListLayoutItem = listLayoutItem;
        mListLayoutFooter = listLayoutFooter;
        appConfig = (AppConfig) mContext.getApplicationContext();
        //		inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        try {
            this.mAdapterCallback = ((List_Interface) mAdapterCallback);
        } catch (ClassCastException e) {
            e.printStackTrace();
            //            throw new ClassCastException("Activity must implement AdapterCallback.");
        }
    }

    @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if ( viewType == TYPE_ITEM ) {
            View view = LayoutInflater.from(parent.getContext()).inflate(mListLayoutItem, parent, false);
            return new vhItem(view);
        } else if ( viewType == TYPE_HEADER ) {
            View view = LayoutInflater.from(parent.getContext()).inflate(mListLayoutHead, parent, false);
            return new vhHeder(view);
        } else if ( viewType == TYPE_FOOTER ) {
            View view = LayoutInflater.from(parent.getContext()).inflate(mListLayoutFooter, parent, false);
            return new vhFooter(view);
        }

        throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
    }

    /**
     * 뷰 재활용을 위한 viewHolder
     */
    public static class vhItem extends RecyclerView.ViewHolder {

        public LinearLayout   llRowRoot;
        public LinearLayout   llRowBody;
        public LinearLayout   llRowControl;
        public LinearLayout   llRowControlBtn;
        public LinearLayout   llRowDelete;
        public LinearLayout   llRowOffline;
        public LinearLayout   llLoading;
        public RelativeLayout rlRowBody;
        public ImageView      ivRowIcon;
        public ImageView      ivRowBullet;
        public TextView       tvRowTitle;
        public TextView       tvRowControl;
        public TextView       tvRowBody;
        public TextView       tvRowOffline;
        public ImageView      ivCheck;
        public Button         btnRowControl;
        public ImageView      ivRowDelete;

        public vhItem(View itemView) {

            super(itemView);
            llRowRoot = (LinearLayout) itemView.findViewById(R.id.llRowRoot);
            llRowBody = (LinearLayout) itemView.findViewById(R.id.llRowBody);
            llRowControl = (LinearLayout) itemView.findViewById(R.id.llRowControl);
            llRowControlBtn = (LinearLayout) itemView.findViewById(R.id.llRowControlBtn);
            llRowDelete = (LinearLayout) itemView.findViewById(R.id.llRowDelete);
            llRowOffline = (LinearLayout) itemView.findViewById(R.id.llRowOffline);
            llLoading = (LinearLayout) itemView.findViewById(R.id.llLoading);
            rlRowBody = (RelativeLayout) itemView.findViewById(R.id.rlRowBody);
            ivRowIcon = (ImageView) itemView.findViewById(R.id.ivRowIcon);
            tvRowTitle = (TextView) itemView.findViewById(R.id.tvRowTitle);
            tvRowBody = (TextView) itemView.findViewById(R.id.tvRowBody);
            tvRowControl = (TextView) itemView.findViewById(R.id.tvRowControl);
            tvRowOffline = (TextView) itemView.findViewById(R.id.tvRowOffline);
            btnRowControl = (Button) itemView.findViewById(R.id.btnRowControl);
            ivRowDelete = (ImageView) itemView.findViewById(R.id.ivRowDelete);
        }
    }

    /**
     * 뷰 재활용을 위한 viewHolder
     */
    public static class vhHeder extends RecyclerView.ViewHolder {

        public LinearLayout llRowRoot;
        public LinearLayout llRowBody;
        public ImageView    ivThingsImage;
        public TextView     tvTitle;
        public LinearLayout llContents;
        public ImageView    ivArrow;

        public vhHeder(View itemView) {

            super(itemView);

            //			llRowRoot = (LinearLayout) itemView.findViewById(R.id.llRowRoot);
            //			llRowBody = (LinearLayout) itemView.findViewById(R.id.llRowBody);
            //			ivThingsImage = (ImageView) itemView.findViewById(R.id.ivThingsImage);
            //			tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
            //			llContents = (LinearLayout) itemView.findViewById(R.id.llContents);
            //			ivArrow = (ImageView) itemView.findViewById(R.id.ivArrow);
        }
    }

    /**
     * 뷰 재활용을 위한 viewHolder
     */
    public static class vhFooter extends RecyclerView.ViewHolder {

        public LinearLayout llFooterProgress;
        public LinearLayout llFooterRoot;
        public LinearLayout llFooterBody;
        public ImageView    ivFooterAdd;

        public vhFooter(View itemView) {

            super(itemView);

            llFooterProgress = (LinearLayout) itemView.findViewById(R.id.llFooterProgress);
            llFooterRoot = (LinearLayout) itemView.findViewById(R.id.llFooterRoot);
            llFooterBody = (LinearLayout) itemView.findViewById(R.id.llFooterBody);
            ivFooterAdd = (ImageView) itemView.findViewById(R.id.ivFooterAdd);
        }
    }

    /**
     * listView getView 를 대체 넘겨 받은 데이터를 화면에 출력하는 역할
     */
    @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        try {
            HashMap<String, Object> hmItem = new HashMap<String, Object>();
            hmItem = alTotalList.get(position);
            if ( holder instanceof vhItem ) {
                vhItem vhItemView = (vhItem) holder;

                /**장치 프로파일 ID*/
                String sHTTP_device_profile_id = util.null2string(hmItem.get(Constants.HTTP_device_profile_id));
                /**이미지 파일 명*/
                String sHTTP_image_file_name = util.null2string(hmItem.get(Constants.HTTP_image_file_name));
                /**프로파일 명*/
                String sHTTP_profile_name = util.null2string(hmItem.get(Constants.HTTP_profile_name));
                /**장치 ID*/
                String sHTTP_device_id = util.null2string(hmItem.get(Constants.HTTP_device_id));
                /**장치 명*/
                String sHTTP_device_name = util.null2string(hmItem.get(Constants.HTTP_device_name));
                /**노드 ID*/
                String sHTTP_node_id = util.null2string(hmItem.get(Constants.HTTP_node_id));
                /**노드 명*/
                String sHTTP_node_name = util.null2string(hmItem.get(Constants.HTTP_node_name));
                /**(장치의) 접속 상태 (ON / OFF / none)*/
                String sHTTP_online_state = util.null2string(hmItem.get(Constants.HTTP_online_state));
                /**장치 연결 유형[NKEET_COMPATIBEL, KNEET_EXCUSIVE, FOSCAM, ZWAVE, INSTEON, COMMAX]*/
                String sHTTP_device_protocol_type = util.null2string(hmItem.get(Constants.HTTP_device_protocol_type));
                /**장치 유형
                 (ex : CONTROLLER_PRIMARY, CONTROLLER_SECONDARY, SLAVE, NODE)*/
                String sHTTP_device_type = util.null2string(hmItem.get(Constants.HTTP_device_type));
                /**장치 접속 IP*/
                String sHTTP_ip = util.null2string(hmItem.get(Constants.HTTP_ip));
                /**장치 접속 PORT*/
                String sHTTP_port = util.null2string(hmItem.get(Constants.HTTP_port));
                /**카메라 접속 ID*/
                String sHTTP_camera_id = util.null2string(hmItem.get(Constants.HTTP_camera_id));
                /**카메라 접속 Password*/
                String sHTTP_camera_password = util.null2string(hmItem.get(Constants.HTTP_camera_password));
                /**(default) 커맨드 클래스 최소 값*/
                String sHTTP_control_min_value = util.null2string(hmItem.get(Constants.HTTP_control_min_value));
                /**(default) 커맨드 클래스 최대 값*/
                String sHTTP_control_max_value = util.null2string(hmItem.get(Constants.HTTP_control_max_value));
                /**(default) 커맨드 클래스 값의 스탭치*/
                String sHTTP_control_step = util.null2string(hmItem.get(Constants.HTTP_control_step));
                /**(default) 커맨드 클래스 값의 단위*/
                String sHTTP_unit = util.null2string(hmItem.get(Constants.HTTP_unit));
                /**커맨드 클래스 값의 데이터 유형 (01 : 정수, 02 : 실수, 03 : 문자열)*/
                String sHTTP_data_type_code = util.null2string(hmItem.get(Constants.HTTP_data_type_code));
                /**(default) 커맨드클래스 유형 ID*/
                String sHTTP_cmdcls_type_id = util.null2string(hmItem.get(Constants.HTTP_cmdcls_type_id));
                int iCmdcls_Type = util.null2int(hmItem.get(Constants.HTTP_cmdcls_type_id));
                /**(default) 커맨드클래스 코드*/
                String sHTTP_cmdcls_code = util.null2string(hmItem.get(Constants.HTTP_cmdcls_code));
                /**(default) 커맨드클래스 유형 명*/
                String sHTTP_cmdcls_type_name = util.null2string(hmItem.get(Constants.HTTP_cmdcls_type_name));
                /**(default) 커맨드클래스 유형 시스템 명*/
                String sHTTP_cmdcls_type_system_name = util.null2string(hmItem.get(Constants.HTTP_cmdcls_type_system_name));
                /**(default) 커맨드클래스 장치 상태정보 값 (N-ISC 질의 결과 ,none = 정보없음)*/
                String sHTTP_content_value = util.null2string(hmItem.get(Constants.HTTP_content_value));
                /**하위 노드 개수*/
                String sHTTP_cmdcls_count = util.null2string(hmItem.get(Constants.HTTP_cmdcls_count));
                /** 커맨드클래스 ID */
                String sHTTP_cmdcls_id = util.null2string(hmItem.get(Constants.HTTP_cmdcls_id));
                /**장치 생성 일시 (포맷 : 년월일시분초)*/
                String sHTTP_create_datetime = util.null2string(hmItem.get(Constants.HTTP_create_datetime));
                String sHTTP_collect_time = util.null2string(hmItem.get(Constants.HTTP_collect_time));
                String sHTTP_request_time = util.null2string(hmItem.get(Constants.HTTP_request_time));

                util.setImage(mContext, sHTTP_image_file_name, vhItemView.ivRowIcon);

                vhItemView.llRowOffline.setVisibility(View.GONE);
                vhItemView.llRowControl.setVisibility(View.GONE);
                vhItemView.llRowDelete.setVisibility(View.GONE);

                vhItemView.tvRowTitle.setText(sHTTP_device_name);

                String sTempValue = "";
                String sSelect = util.getStringValues(mContext, "CMDCLS_TYPE_ID_" + sHTTP_cmdcls_type_id + "_BG_DEFAULT");
                if ( sHTTP_content_value.equals("none") ) {
                    sTempValue = util.getStringValues(mContext, "CMDCLS_TYPE_ID_" + sHTTP_cmdcls_type_id + "_" + sSelect);
                    sHTTP_content_value = sSelect;
                } else {
                    sTempValue = util.getStringValues(mContext, "CMDCLS_TYPE_ID_" + sHTTP_cmdcls_type_id + "_" + sHTTP_content_value);
                }

                final View vLoding = vhItemView.llLoading;
                if ( !sHTTP_request_time.equals("") ) {
                    vhItemView.llLoading.setVisibility(View.VISIBLE);
                } else {
                    vhItemView.llLoading.setVisibility(View.GONE);
                }

                vhItemView.btnRowControl.setTag(position);
                vhItemView.llRowControlBtn.setVisibility(View.GONE);
                switch (iCmdcls_Type) {
                    //SENSOR_BINARY
                    case Constants.CMDCLS_TYPE_ID_11001:
                    case Constants.CMDCLS_TYPE_ID_11002:
                    case Constants.CMDCLS_TYPE_ID_11003:
                    case Constants.CMDCLS_TYPE_ID_11004:
                    case Constants.CMDCLS_TYPE_ID_11005:
                    case Constants.CMDCLS_TYPE_ID_11006:
                    case Constants.CMDCLS_TYPE_ID_11007:
                    case Constants.CMDCLS_TYPE_ID_11008:
                    case Constants.CMDCLS_TYPE_ID_11009:
                    case Constants.CMDCLS_TYPE_ID_11010:
                    case Constants.CMDCLS_TYPE_ID_11011:
                    case Constants.CMDCLS_TYPE_ID_11012:
                    case Constants.CMDCLS_TYPE_ID_11013:
                    case Constants.CMDCLS_TYPE_ID_11014:
                    case Constants.CMDCLS_TYPE_ID_11015:
                    case Constants.CMDCLS_TYPE_ID_11016:
                    case Constants.CMDCLS_TYPE_ID_11017:
                    case Constants.CMDCLS_TYPE_ID_11018:
                        vhItemView.tvRowControl.setText(sTempValue);
                        if ( sHTTP_content_value.equals(sSelect) ) {
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_01));
                        } else {
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_02));
                        }
                        break;

                    //SWITCH_BINARY
                    case Constants.CMDCLS_TYPE_ID_17001:
                    case Constants.CMDCLS_TYPE_ID_17002:
                    case Constants.CMDCLS_TYPE_ID_17003:
                    case Constants.CMDCLS_TYPE_ID_17004:
                    case Constants.CMDCLS_TYPE_ID_17005:
                    case Constants.CMDCLS_TYPE_ID_17006:
                        vhItemView.tvRowControl.setText(sTempValue);
                        vhItemView.llRowControlBtn.setVisibility(View.VISIBLE);

                        if ( sHTTP_content_value.equals(sSelect) ) {
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_01));
                            vhItemView.btnRowControl.setBackgroundResource(R.drawable.img_things_list_btn_onoff_on);
                        } else {
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_02));
                            vhItemView.btnRowControl.setBackgroundResource(R.drawable.img_things_list_btn_onoff_off);
                        }

                        vhItemView.btnRowControl.setOnClickListener(new View.OnClickListener() {
                            int iTag = 0;
                            private Intent intent;

                            @Override public void onClick(View v) {

                                iTag = (int) v.getTag();
                                String sHTTP_cmdcls_type_id = util.null2string(alTotalList.get(iTag).get(Constants.HTTP_cmdcls_type_id));
                                String sHTTP_content_value = util.null2string(alTotalList.get(iTag).get(Constants.HTTP_content_value));
                                if ( sHTTP_content_value.equals(util.getStringValues(mContext, "CMDCLS_TYPE_ID_" + sHTTP_cmdcls_type_id + "_BG_DEFAULT")) ) {
                                    setHTTP_Value("ON");
                                } else {
                                    setHTTP_Value("OFF");
                                }
                                //                                notifyDataSetChanged();
                                vLoding.setVisibility(View.VISIBLE);
                                mAdapterCallback.onCallAction(3, iTag);
                            }
                        });
                        break;
                    case Constants.CMDCLS_TYPE_ID_36002:
                        vhItemView.tvRowControl.setText(sTempValue);
                        vhItemView.llRowControlBtn.setVisibility(View.VISIBLE);

                        if ( sHTTP_content_value.equals(sSelect) ) {
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_01));
                        } else {
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_02));
                        }

                        vhItemView.btnRowControl.setBackgroundResource(R.drawable.img_things_list_btn_lockunlock_lock);
                        vhItemView.btnRowControl.setOnClickListener(new View.OnClickListener() {
                            int iTag = 0;
                            private Intent intent;

                            @Override public void onClick(View v) {

                                iTag = (int) v.getTag();
                                setHTTP_Value("CLOSE");
                                vLoding.setVisibility(View.VISIBLE);
                                mAdapterCallback.onCallAction(3, iTag);
                            }
                        });
                        break;
                    // SENSOR_MULTILEVEL
                    case Constants.CMDCLS_TYPE_ID_12001:
                    case Constants.CMDCLS_TYPE_ID_12002:
                    case Constants.CMDCLS_TYPE_ID_12003:
                    case Constants.CMDCLS_TYPE_ID_12004:
                    case Constants.CMDCLS_TYPE_ID_12005:
                    case Constants.CMDCLS_TYPE_ID_12006:
                    case Constants.CMDCLS_TYPE_ID_12007:
                    case Constants.CMDCLS_TYPE_ID_12008:
                    case Constants.CMDCLS_TYPE_ID_12009:
                    case Constants.CMDCLS_TYPE_ID_12010:
                    case Constants.CMDCLS_TYPE_ID_12011:
                    case Constants.CMDCLS_TYPE_ID_12012:
                    case Constants.CMDCLS_TYPE_ID_12013:
                    case Constants.CMDCLS_TYPE_ID_12014:
                    case Constants.CMDCLS_TYPE_ID_12015:
                    case Constants.CMDCLS_TYPE_ID_12016:
                    case Constants.CMDCLS_TYPE_ID_12017:
                    case Constants.CMDCLS_TYPE_ID_12018:
                    case Constants.CMDCLS_TYPE_ID_12019:
                    case Constants.CMDCLS_TYPE_ID_12020:
                    case Constants.CMDCLS_TYPE_ID_12021:
                    case Constants.CMDCLS_TYPE_ID_12022:
                    case Constants.CMDCLS_TYPE_ID_12023:
                    case Constants.CMDCLS_TYPE_ID_12024:
                    case Constants.CMDCLS_TYPE_ID_12025:
                    case Constants.CMDCLS_TYPE_ID_12026:
                    case Constants.CMDCLS_TYPE_ID_12027:
                    case Constants.CMDCLS_TYPE_ID_12028:
                    case Constants.CMDCLS_TYPE_ID_12029:
                    case Constants.CMDCLS_TYPE_ID_12030:
                    case Constants.CMDCLS_TYPE_ID_12031:
                    case Constants.CMDCLS_TYPE_ID_12032:
                        if ( sHTTP_content_value.equals("none") ) {
                            vhItemView.tvRowControl.setText("-");
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_01));
                        } else {
                            vhItemView.tvRowControl.setText(sHTTP_content_value + sHTTP_unit);
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_02));
                        }
                        break;
                    //METER_CURRENT
                    case Constants.CMDCLS_TYPE_ID_15001:
                    case Constants.CMDCLS_TYPE_ID_15002:
                        if ( sHTTP_content_value.equals("none") ) {
                            vhItemView.tvRowControl.setText("-");
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_01));
                        } else {
                            vhItemView.tvRowControl.setText(sHTTP_content_value + sHTTP_unit);
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_02));
                        }
                        break;
                    //METER_TOTAL
                    case Constants.CMDCLS_TYPE_ID_16001:
                    case Constants.CMDCLS_TYPE_ID_16002:
                    case Constants.CMDCLS_TYPE_ID_16003:
                    case Constants.CMDCLS_TYPE_ID_16004:
                        if ( sHTTP_content_value.equals("none") ) {
                            vhItemView.tvRowControl.setText("-");
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_01));
                        } else {
                            vhItemView.tvRowControl.setText(sHTTP_content_value + sHTTP_unit);
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_02));
                        }
                        break;
                    //SWITCH_MULTILEVEL
                    case Constants.CMDCLS_TYPE_ID_18001:
                    case Constants.CMDCLS_TYPE_ID_18002:
                    case Constants.CMDCLS_TYPE_ID_18003:
                    case Constants.CMDCLS_TYPE_ID_18004:
                    case Constants.CMDCLS_TYPE_ID_18005:
                        if ( sHTTP_content_value.equals("none") ) {
                            vhItemView.tvRowControl.setText("-");
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_01));
                        } else {
                            vhItemView.tvRowControl.setText(sHTTP_content_value + sHTTP_unit);
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_02));
                        }
                        break;
                    //THERMOSTAT_SETPOINT
                    case Constants.CMDCLS_TYPE_ID_19001:
                        if ( sHTTP_content_value.equals("none") ) {
                            vhItemView.tvRowControl.setText("-");
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_01));
                        } else {
                            vhItemView.tvRowControl.setText(sHTTP_content_value + sHTTP_unit);
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_02));
                        }
                        break;
                    // METER
                    case Constants.CMDCLS_TYPE_ID_29001:
                    case Constants.CMDCLS_TYPE_ID_29002:
                    case Constants.CMDCLS_TYPE_ID_29003:
                    case Constants.CMDCLS_TYPE_ID_29004:
                    case Constants.CMDCLS_TYPE_ID_29005:
                    case Constants.CMDCLS_TYPE_ID_29006:
                    case Constants.CMDCLS_TYPE_ID_29007:
                    case Constants.CMDCLS_TYPE_ID_29008:
                    case Constants.CMDCLS_TYPE_ID_29009:
                    case Constants.CMDCLS_TYPE_ID_29010:
                    case Constants.CMDCLS_TYPE_ID_29011:
                    case Constants.CMDCLS_TYPE_ID_29012:
                    case Constants.CMDCLS_TYPE_ID_29013:
                    case Constants.CMDCLS_TYPE_ID_29014:
                        if ( sHTTP_content_value.equals("none") ) {
                            vhItemView.tvRowControl.setText("-");
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_01));
                        } else {
                            vhItemView.tvRowControl.setText(sHTTP_content_value + sHTTP_unit);
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_02));
                        }
                        break;
                    //BATTERY
                    case Constants.CMDCLS_TYPE_ID_30001:
                        if ( sHTTP_content_value.equals("none") ) {
                            vhItemView.tvRowControl.setText("-");
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_01));
                        } else {
                            vhItemView.tvRowControl.setText(sHTTP_content_value + sHTTP_unit);
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_02));
                        }
                        break;
                    //SYSTEM
                    case Constants.CMDCLS_TYPE_ID_34006:
                    case Constants.CMDCLS_TYPE_ID_34007:
                    case Constants.CMDCLS_TYPE_ID_34008:
                    case Constants.CMDCLS_TYPE_ID_34009:
                    case Constants.CMDCLS_TYPE_ID_34010:
                    case Constants.CMDCLS_TYPE_ID_34011:
                    case Constants.CMDCLS_TYPE_ID_34012:
                    case Constants.CMDCLS_TYPE_ID_34013:
                    case Constants.CMDCLS_TYPE_ID_34014:
                        if ( sHTTP_content_value.equals("none") ) {
                            vhItemView.tvRowControl.setText("-");
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_01));
                        } else {
                            vhItemView.tvRowControl.setText(sHTTP_content_value + sHTTP_unit);
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_02));
                        }
                        break;
                    default:
                        if ( sHTTP_content_value.equals("none") ) {
                            vhItemView.tvRowControl.setText("-");
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_01));
                        } else {
                            vhItemView.tvRowControl.setText(sHTTP_content_value + sHTTP_unit);
                            vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            vhItemView.tvRowControl.setTextColor(util.getColor(mContext, R.color.color_text_02));
                        }
                        break;
                }

                if ( bListCheck ) {
                    vhItemView.llRowControl.setVisibility(View.GONE);
                    vhItemView.llRowDelete.setVisibility(View.VISIBLE);
                    vhItemView.llRowOffline.setVisibility(View.GONE);
                    vhItemView.llLoading.setVisibility(View.GONE);
                } else {
                    if ( sHTTP_online_state.equals("OFF") || appConfig.getSelect_Homegrp_Homehub_Online_State().equals("OFF") ) {
                        vhItemView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                        vhItemView.llRowControl.setVisibility(View.GONE);
                        vhItemView.llRowDelete.setVisibility(View.GONE);
                        vhItemView.llRowOffline.setVisibility(View.VISIBLE);
                        vhItemView.llLoading.setVisibility(View.GONE);
                    } else {
                        vhItemView.llRowControl.setVisibility(View.VISIBLE);
                        vhItemView.llRowDelete.setVisibility(View.GONE);
                        vhItemView.llRowOffline.setVisibility(View.GONE);
                    }
                }

                vhItemView.llRowBody.setTag(position);
                vhItemView.llRowBody.setOnClickListener(new View.OnClickListener() {
                    int iTag = 0;
                    private Intent intent;

                    @Override public void onClick(View v) {

                        iTag = (int) v.getTag();
                        if ( bListCheck ) {
                            mAdapterCallback.onCallAction(2, iTag);
                        } else {
                            mAdapterCallback.onCallAction(1, iTag);
                        }
                    }
                });
                vhItemView.llRowDelete.setTag(position);
                vhItemView.llRowDelete.setOnClickListener(new View.OnClickListener() {
                    int iTag = 0;
                    private Intent intent;

                    @Override public void onClick(View v) {

                        iTag = (int) v.getTag();
                        if ( bListCheck ) {
                            mAdapterCallback.onCallAction(2, iTag);
                        }
                    }
                });
            } else if ( holder instanceof vhFooter ) {

                vhFooter vhItemView = (vhFooter) holder;
                String sMore_Yn = util.null2string(hmItem.get(Constants.HTTP_MORE_YN));
                if ( sMore_Yn.equals("Y") ) {
                    vhItemView.llFooterProgress.setVisibility(View.VISIBLE);
                } else {
                    vhItemView.llFooterProgress.setVisibility(View.GONE);
                    if ( bListCheck ) {
                        vhItemView.llFooterBody.setVisibility(View.GONE);
                    } else {
                        if ( bFooterVisible ) {
                            vhItemView.llFooterBody.setVisibility(View.VISIBLE);
                        } else {
                            vhItemView.llFooterBody.setVisibility(View.GONE);
                        }

                        vhItemView.llFooterBody.setOnClickListener(new View.OnClickListener() {
                            String sTag = "";
                            private Intent intent;

                            @Override public void onClick(View v) {

                                mAdapterCallback.onCallAction(0, 0);
                            }
                        });
                    }
                }
            }
            holder.itemView.setTag(hmItem);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean isPositionHeader(int position) {

        return position == 0;
    }

    @Override public int getItemViewType(int position) {

        if ( getHeaderCheck() && position == 0 ) {
            return TYPE_HEADER;
        } else if ( getFooterCheck() && position == getItemCount() - 1 ) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }
}
