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

package com.ntels.onecable.admin.view.things;

import com.ntels.onecable.admin.R;
import com.ntels.onecable.admin.common.AppConfig;
import com.ntels.onecable.admin.common.Constants;
import com.ntels.onecable.admin.common.c_interface.ListInterface;
import com.ntels.onecable.admin.common.util.Dlog;
import com.ntels.onecable.admin.common.util.Util;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

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

import static android.content.ContentValues.TAG;
import static com.ntels.onecable.admin.common.c_interface.ListInterface.ON_CALL_ACTION_EXECUTE;

/**
 * <pre>
 * 1. ClassName :   FragmentThingsAdapter
 * 2. FileName  :   FragmentThingsAdapter.java
 * 3. Package   :   com.ntels.onecable.admin.view.things
 * 4. Date      :   2016-11-28
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class ThingsDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>   {

    public static final int TYPE_HEADER = 0;
    public static final int TYPE_ITEM = 1;
    public static final int TYPE_FOOTER = 2;
    private ListInterface mAdapterCallback;
    private AppConfig mAppConfig;
    private Util mUtil = new Util();
    private ArrayList<HashMap<String, Object>> alTotalList = new ArrayList<HashMap<String, Object>>();
    private Context mContext;
    private int listLayoutHead;
    private int listLayoutItem;
    private int listLayoutFooter;
    private String httpValue = "";
    private Boolean isListCheck = false;
    private Boolean isDeleteCheck = false;
    private Boolean isMoveCheck = false;
    private Boolean isHeaderCheck = false;
    private Boolean isFooterCheck = false;
    private Boolean isFooterVisible = false;


    /**
     * 생성자.
     *
     * @param context          컨텍스트
     * @param listLayoutHead   Header layout resource id
     * @param listLayoutItem   Item layout resource id
     * @param listLayoutFooter Footer layout resource id
     * @param alTmp            adapter에 표현 할 data
     * @param adapterCallback  callback listener
     */
    public ThingsDetailAdapter(Context context, int listLayoutHead, int listLayoutItem,
                               int listLayoutFooter, ArrayList<HashMap<String, Object>> alTmp,
                               ListInterface adapterCallback) {

        this.mContext = context;
        alTotalList = alTmp;
        this.listLayoutHead = listLayoutHead;
        this.listLayoutItem = listLayoutItem;
        this.listLayoutFooter = listLayoutFooter;
        mAppConfig = (AppConfig) this.mContext.getApplicationContext();

        try {
            this.mAdapterCallback = adapterCallback;
        } catch (ClassCastException ex) {
            ex.printStackTrace();
        }
    }

    public String getHttpValue() {

        return httpValue;
    }

    public void setHttpValue(String value) {

        httpValue = value;
    }

    public boolean getListCheck() {

        return isListCheck;
    }

    public void setListCheck(boolean isCheck) {

        isListCheck = isCheck;
    }

    public boolean getMoveCheck() {

        return isMoveCheck;
    }

    public void setMoveCheck(boolean isCheck) {

        isMoveCheck = isCheck;
    }

    public boolean getDeleteCheck() {

        return isDeleteCheck;
    }

    public void setDeleteCheck(boolean isCheck) {

        isDeleteCheck = isCheck;
    }

    public boolean getHeaderCheck() {

        return isHeaderCheck;
    }

    public void setHeaderCheck(boolean isCheck) {

        isHeaderCheck = isCheck;
    }

    public boolean getFooterCheck() {

        return isFooterCheck;
    }

    public void setFooterCheck(boolean isCheck) {

        isFooterCheck = isCheck;
    }

    public boolean getFooterVisible() {

        return isFooterVisible;
    }

    public void setFooterVisible(boolean isVisible) {

        isFooterVisible = isVisible;
    }

    @Override
    public int getItemCount() {

        return alTotalList.size();
    }

    public ArrayList<HashMap<String, Object>> getItemList() {

        return alTotalList;
    }

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

        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(listLayoutItem, parent, false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_HEADER) {
            View view = LayoutInflater.from(parent.getContext()).inflate(listLayoutHead, parent, false);
            return new HeaderViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(parent.getContext()).inflate(listLayoutFooter, parent, false);
            return new FooterViewHolder(view);
        }

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

    @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 ItemViewHolder) {
                final ItemViewHolder itemViewHolerView = (ItemViewHolder) holder;


                final String deviceId = mUtil.null2string(hmItem.get(Constants.KEY_DEVICE_ID));
                String cmdclsMandatoryYn = mUtil.null2string(hmItem.get(Constants.KEY_CMDCLS_MANDATORY_YN));
                if ("Y".equals(cmdclsMandatoryYn)) {

                    String httpOnlineState = mUtil.null2string(hmItem.get(Constants.KEY_ONLINE_STATE));
                    String httpCmdclsName = mUtil.null2string(hmItem.get(Constants.KEY_CMDCLS_NAME));
                    String httpUnit = mUtil.null2string(hmItem.get(Constants.KEY_UNIT));
                    String httpCmdclsTypeId = mUtil.null2string(hmItem.get(Constants.KEY_CMDCLS_TYPE_ID));
                    String httpContentValue = mUtil.null2string(hmItem.get(Constants.KEY_CONTENT_VALUE));
                    String httpContentLastDatetime = mUtil.UTCtoLocal5(mUtil.null2string(hmItem.get(Constants.KEY_CONTENT_LAST_DATETIME)));

                    Dlog.i("KEY_content_value1 : " + httpContentValue);

                    String tempValue;
                    String select = mUtil.getStringValues(mContext, "CMDCLS_TYPE_ID_" + httpCmdclsTypeId + "_BG_DEFAULT");
                    if ("".equals(httpContentValue)) {
                        tempValue = mUtil.getStringValues(mContext, "CMDCLS_TYPE_ID_" + httpCmdclsTypeId + "_" + select);
                        httpContentValue = select;
                    } else {
                        tempValue = mUtil.getStringValues(mContext, "CMDCLS_TYPE_ID_" + httpCmdclsTypeId + "_" + httpContentValue);
                    }


                    itemViewHolerView.llRowBody1.setVisibility(View.VISIBLE);
                    itemViewHolerView.llRowBody2.setVisibility(View.GONE);
                    itemViewHolerView.llRowBody1_LastTime.setVisibility(View.GONE);
                    itemViewHolerView.tvRowBody1_Lasttime.setVisibility(View.GONE);
                    itemViewHolerView.llRowBody1_Control.setVisibility(View.GONE);
                    itemViewHolerView.btnRowBody1_Contents_On.setVisibility(View.GONE);
                    itemViewHolerView.btnRowBody1_Contents_Off.setVisibility(View.GONE);


                    final View vLoding = itemViewHolerView.llLoading1;
                    String httpRequestTime = mUtil.null2string(hmItem.get(Constants.KEY_REQUEST_TIME));

                    if (!"".equals(httpRequestTime)) {
                        Dlog.i(TAG, "httpRequestTime - " + httpRequestTime);
                        vLoding.setVisibility(View.VISIBLE);
                    } else {
                        vLoding.setVisibility(View.GONE);
                    }

                    int tempType = 0;
                    int cmdclsType = mUtil.null2int(hmItem.get(Constants.KEY_CMDCLS_TYPE_ID));
                    switch (cmdclsType) {
                        case Constants.CMDCLS_TYPE_ID_36002:
                            //가스락
                            itemViewHolerView.tvRowBody1_Title.setText(tempValue);
                            if ("OFF".equals(httpOnlineState) || mAppConfig.getSelectCtrtGroupHomehubOnlineState(deviceId).equals("OFF")) {
                                setDispOffline(itemViewHolerView, R.drawable.img_things_detail_05_00_valve_state_offline);
                            } else {
                                if (httpContentValue.equals(select)) {
                                    itemViewHolerView.tvRowBody1_Title.setTextColor(Util.getColor(mContext, R.color.color_text_01));
                                } else {
                                    itemViewHolerView.llRowBody1_Control.setVisibility(View.VISIBLE);
                                    itemViewHolerView.btnRowBody1_Contents_Off.setVisibility(View.VISIBLE);
                                    itemViewHolerView.tvRowBody1_Title.setTextColor(Util.getColor(mContext, R.color.color_text_02));
                                }
                            }
                            itemViewHolerView.btnRowBody1_Contents_Off.setTag(position);
                            itemViewHolerView.btnRowBody1_Contents_Off.setOnClickListener(new View.OnClickListener() {
                                int tag = 0;

                                @Override
                                public void onClick(View view) {

                                    tag = (int) view.getTag();
                                    setHttpValue("CLOSE");
                                    vLoding.setVisibility(View.VISIBLE);
                                    mAdapterCallback.onCallAction(ON_CALL_ACTION_EXECUTE, tag);
                                }
                            });
                            break;
                        //SWITCH_BINARY
                        case Constants.CMDCLS_TYPE_ID_17001:
                        case Constants.CMDCLS_TYPE_ID_17002:
                        case Constants.CMDCLS_TYPE_ID_17005:
                            itemViewHolerView.tvRowBody1_Title.setText(tempValue);
                            if ("OFF".equals(httpOnlineState) || mAppConfig.getSelectCtrtGroupHomehubOnlineState(deviceId).equals("OFF")) {
                                setDispOffline(itemViewHolerView, R.drawable.img_things_detail_06_00_plug_state_offline);
                            } else {
                                itemViewHolerView.llRowBody1_Control.setVisibility(View.VISIBLE);
                                if (httpContentValue.equals(select)) {
                                    itemViewHolerView.btnRowBody1_Contents_On.setVisibility(View.VISIBLE);
                                    itemViewHolerView.tvRowBody1_Title.setTextColor(Util.getColor(mContext, R.color.color_text_01));
                                } else {
                                    itemViewHolerView.btnRowBody1_Contents_Off.setVisibility(View.VISIBLE);
                                    itemViewHolerView.tvRowBody1_Title.setTextColor(Util.getColor(mContext, R.color.color_text_02));
                                }
                            }
                            itemViewHolerView.btnRowBody1_Contents_On.setTag(position);
                            itemViewHolerView.btnRowBody1_Contents_On.setOnClickListener(new View.OnClickListener() {
                                int tag = 0;

                                @Override
                                public void onClick(View view) {

                                    tag = (int) view.getTag();
                                    setHttpValue("ON");
                                    vLoding.setVisibility(View.VISIBLE);
                                    mAdapterCallback.onCallAction(ON_CALL_ACTION_EXECUTE, tag);
                                }
                            });
                            itemViewHolerView.btnRowBody1_Contents_Off.setTag(position);
                            itemViewHolerView.btnRowBody1_Contents_Off.setOnClickListener(new View.OnClickListener() {
                                int tag = 0;

                                @Override
                                public void onClick(View view) {

                                    tag = (int) view.getTag();
                                    setHttpValue("OFF");
                                    vLoding.setVisibility(View.VISIBLE);
                                    mAdapterCallback.onCallAction(ON_CALL_ACTION_EXECUTE, tag);
                                }
                            });
                            break;
                        case Constants.CMDCLS_TYPE_ID_11011:
                            //문열림 센서
                            itemViewHolerView.tvRowBody1_Title.setText(tempValue);
                            itemViewHolerView.llRowBody1_LastTime.setVisibility(View.VISIBLE);
                            itemViewHolerView.tvRowBody1_Lasttime.setVisibility(View.VISIBLE);
                            itemViewHolerView.tvRowBody1_Lasttime.setText(httpContentLastDatetime);
                            if ("OFF".equals(httpOnlineState) || mAppConfig.getSelectCtrtGroupHomehubOnlineState(deviceId).equals("OFF")) {
                                setDispOffline(itemViewHolerView, R.drawable.img_things_detail_01_00_sensor_door_offline);
                            } else {
                                if (httpContentValue.equals(select)) {
                                    itemViewHolerView.tvRowBody1_Title.setTextColor(Util.getColor(mContext, R.color.color_text_01));
                                } else {
                                    itemViewHolerView.tvRowBody1_Title.setTextColor(Util.getColor(mContext, R.color.color_text_02));
                                }
                            }
                            break;
                    }

                } else {
                    final String onlineState = mUtil.null2string(hmItem.get(Constants.KEY_ONLINE_STATE));

                    final String unit = mUtil.null2string(hmItem.get(Constants.KEY_UNIT));
                    String cmdclsTypeId = mUtil.null2string(hmItem.get(Constants.KEY_CMDCLS_TYPE_ID));

                    String contentValue = mUtil.null2string(hmItem.get(Constants.KEY_CONTENT_VALUE));

                    String tempValue = "";
                    String select = mUtil.getStringValues(mContext, "CMDCLS_TYPE_ID_" + cmdclsTypeId + "_BG_DEFAULT");
                    if ("".equals(contentValue)) {
                        tempValue = mUtil.getStringValues(mContext, "CMDCLS_TYPE_ID_" + cmdclsTypeId + "_" + select);
                        contentValue = select;
                    } else {
                        tempValue = mUtil.getStringValues(mContext, "CMDCLS_TYPE_ID_" + cmdclsTypeId + "_" + contentValue);
                    }
                    itemViewHolerView.llRowBody1.setVisibility(View.GONE);
                    itemViewHolerView.llRowBody2.setVisibility(View.VISIBLE);

                    final View vLoding = itemViewHolerView.llLoading2;
                    String requestTime = mUtil.null2string(hmItem.get(Constants.KEY_REQUEST_TIME));
                    if (!"".equals(requestTime)) {
                        vLoding.setVisibility(View.VISIBLE);
                    } else {
                        vLoding.setVisibility(View.GONE);
                    }
                    String cmdclsName = mUtil.null2string(hmItem.get(Constants.KEY_CMDCLS_NAME));
                    itemViewHolerView.tvRowBody2_Title.setText(cmdclsName);

                    itemViewHolerView.btnRowBody2_Control.setTag(position);
                    itemViewHolerView.btnRowBody2_Control.setVisibility(View.GONE);
                    int cmdclsType = mUtil.null2int(hmItem.get(Constants.KEY_CMDCLS_TYPE_ID));
                    switch (cmdclsType) {
                        //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:
                            itemViewHolerView.tvRowBody2_Body.setText(tempValue);

                            if ("OFF".equals(onlineState) || mAppConfig.getSelectCtrtGroupHomehubOnlineState(deviceId).equals("OFF")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-");
                            }
                            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:
                            itemViewHolerView.tvRowBody2_Body.setText(tempValue);
                            itemViewHolerView.btnRowBody2_Control.setVisibility(View.VISIBLE);

                            if (contentValue.equals(select)) {
                                itemViewHolerView.btnRowBody2_Control.setBackgroundResource(R.drawable.img_things_list_btn_onoff_on);
                            } else {
                                itemViewHolerView.btnRowBody2_Control.setBackgroundResource(R.drawable.img_things_list_btn_onoff_off);
                            }

                            itemViewHolerView.btnRowBody2_Control.setOnClickListener(new View.OnClickListener() {
                                int tag = 0;
                                String value = "";

                                @Override
                                public void onClick(View view) {

                                    tag = (int) view.getTag();
                                    String cmdclsTypeId = mUtil.null2string(alTotalList.get(tag).get(Constants.KEY_CMDCLS_TYPE_ID));
                                    String contentValue = mUtil.null2string(alTotalList.get(tag).get(Constants.KEY_CONTENT_VALUE));
                                    if (contentValue.equals(mUtil.getStringValues(mContext, "CMDCLS_TYPE_ID_" + cmdclsTypeId +
                                            "_BG_DEFAULT"))) {
                                        value = "ON";
                                    } else {
                                        value = "OFF";
                                    }

                                    vLoding.setVisibility(View.VISIBLE);
                                    setHttpValue(value);
                                    mAdapterCallback.onCallAction(ON_CALL_ACTION_EXECUTE, tag);
                                }
                            });
                            break;
                        case Constants.CMDCLS_TYPE_ID_17008:
                            // 미터초기화
                            itemViewHolerView.tvRowBody2_Body.setVisibility(View.GONE);
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                                    ViewGroup.LayoutParams.WRAP_CONTENT);
                            params.setMargins(0, 0, mUtil.getScaleVal(mContext, 10), 0);
                            itemViewHolerView.btnRowBody2_Control.setLayoutParams(params);
                            itemViewHolerView.btnRowBody2_Control.setVisibility(View.VISIBLE);
                            if ("OFF".equals(onlineState)
                                    || mAppConfig.getSelectCtrtGroupHomehubOnlineState(deviceId).equals("OFF")) {
                                itemViewHolerView.btnRowBody2_Control.setBackgroundResource(R.drawable.img_btn_common_disable);
                            } else {
                                itemViewHolerView.btnRowBody2_Control.setBackgroundResource(R.drawable.d_img_btn_round_bg);
                            }
                            Dlog.i(TAG, "tempValue - " + tempValue);
                            itemViewHolerView.btnRowBody2_Control.setText(tempValue);

                            // 버튼에 들어가는 default padding 값을 없앤다.
                            itemViewHolerView.btnRowBody2_Control.setMinWidth(0);
                            itemViewHolerView.btnRowBody2_Control.setMinHeight(0);
                            itemViewHolerView.btnRowBody2_Control.setMinimumWidth(0);
                            itemViewHolerView.btnRowBody2_Control.setMinimumHeight(0);

                            itemViewHolerView.btnRowBody2_Control.setPadding(30, 30, 30, 30);
                            itemViewHolerView.btnRowBody2_Control.setOnClickListener(new View.OnClickListener() {
                                int tag = 0;
                                String value = "";

                                @Override
                                public void onClick(View view) {

                                    if ("OFF".equals(onlineState) || mAppConfig.getSelectCtrtGroupHomehubOnlineState(deviceId).equals
                                            ("OFF")) {
                                        return;
                                    }

                                    tag = (int) view.getTag();
                                    value = "ON";
                                    vLoding.setVisibility(View.VISIBLE);
                                    setHttpValue(value);
                                    mAdapterCallback.onCallAction(ON_CALL_ACTION_EXECUTE, tag);
                                }
                            });

                            break;
                        case Constants.CMDCLS_TYPE_ID_36002:
                            itemViewHolerView.tvRowBody2_Body.setText(tempValue);
                            itemViewHolerView.btnRowBody2_Control.setVisibility(View.VISIBLE);

                            itemViewHolerView.btnRowBody2_Control.setBackgroundResource(R.drawable.img_things_list_btn_lockunlock_lock);
                            itemViewHolerView.btnRowBody2_Control.setOnClickListener(new View.OnClickListener() {
                                int tag = 0;

                                String value = "";

                                @Override
                                public void onClick(View view) {

                                    tag = (int) view.getTag();
                                    value = "CLOSE";
                                    vLoding.setVisibility(View.VISIBLE);
                                    setHttpValue(value);
                                    mAdapterCallback.onCallAction(ON_CALL_ACTION_EXECUTE, tag);
                                }
                            });
                            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:
                        case Constants.CMDCLS_TYPE_ID_12033:
                        case Constants.CMDCLS_TYPE_ID_12034:
                            if (contentValue.equals("none")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            } else {
                                itemViewHolerView.tvRowBody2_Body.setText(contentValue + unit);
                            }
                            if (onlineState.equals("OFF") || mAppConfig.getSelectCtrtGroupHomehubOnlineState(deviceId).equals("OFF")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            }
                            break;

                        //METER_CURRENT
                        case Constants.CMDCLS_TYPE_ID_15001:
                        case Constants.CMDCLS_TYPE_ID_15002:
                            if (contentValue.equals("none")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            } else {
                                itemViewHolerView.tvRowBody2_Body.setText(contentValue + unit);
                            }
                            if (onlineState.equals("OFF") || mAppConfig.getSelectCtrtGroupHomehubOnlineState(deviceId).equals("OFF")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            }
                            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 (contentValue.equals("none")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            } else {
                                itemViewHolerView.tvRowBody2_Body.setText(contentValue + unit);
                            }
                            if (onlineState.equals("OFF") || mAppConfig.getSelectCtrtGroupHomehubOnlineState(deviceId).equals("OFF")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            }
                            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 (contentValue.equals("none")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            } else {
                                itemViewHolerView.tvRowBody2_Body.setText(contentValue + unit);
                            }
                            if (onlineState.equals("OFF") || mAppConfig.getSelectCtrtGroupHomehubOnlineState(deviceId).equals("OFF")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            }
                            break;
                        //THERMOSTAT_SETPOINT
                        case Constants.CMDCLS_TYPE_ID_19001:
                            if (contentValue.equals("none")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            } else {
                                itemViewHolerView.tvRowBody2_Body.setText(contentValue + unit);
                            }
                            if (onlineState.equals("OFF") || mAppConfig.getSelectCtrtGroupHomehubOnlineState(deviceId).equals("OFF")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            }
                            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 (contentValue.equals("none")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            } else {
                                itemViewHolerView.tvRowBody2_Body.setText(contentValue + unit);
                            }
                            if (onlineState.equals("OFF") || mAppConfig.getSelectCtrtGroupHomehubOnlineState(deviceId).equals("OFF")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            }
                            break;
                        //BATTERY
                        case Constants.CMDCLS_TYPE_ID_30001:
                            if (contentValue.equals("none")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            } else {
                                itemViewHolerView.tvRowBody2_Body.setText(contentValue + unit);
                            }
                            if (onlineState.equals("OFF") || mAppConfig.getSelectCtrtGroupHomehubOnlineState(deviceId).equals("OFF")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            }
                            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 (contentValue.equals("none")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            } else {
                                itemViewHolerView.tvRowBody2_Body.setText(contentValue + unit);
                            }
                            if (onlineState.equals("OFF") || mAppConfig.getSelectCtrtGroupHomehubOnlineState(deviceId).equals("OFF")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            }
                            break;
                        default:
                            if (contentValue.equals("none")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            } else {
                                itemViewHolerView.tvRowBody2_Body.setText(contentValue + unit);
                            }
                            if (onlineState.equals("OFF") || mAppConfig.getSelectCtrtGroupHomehubOnlineState(deviceId).equals("OFF")) {
                                itemViewHolerView.tvRowBody2_Body.setText("-" + unit);
                            }
                            break;
                    }
                }
            } else if (holder instanceof FooterViewHolder) {

                FooterViewHolder vhItemView = (FooterViewHolder) holder;
                String moreYn = mUtil.null2string(hmItem.get(Constants.KEY_MORE_YN));
                if (moreYn.equals("Y")) {
                    vhItemView.llFooterProgress.setVisibility(View.VISIBLE);
                } else {
                    vhItemView.llFooterProgress.setVisibility(View.GONE);
                    if (isDeleteCheck || isMoveCheck) {
                        vhItemView.llFooterBody.setVisibility(View.GONE);
                    } else {
                        if (isFooterVisible) {
                            vhItemView.llFooterBody.setVisibility(View.VISIBLE);
                        } else {
                            vhItemView.llFooterBody.setVisibility(View.GONE);
                        }

                        vhItemView.llFooterBody.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View view) {

                                mAdapterCallback.onCallAction(0, 0);
                            }
                        });
                    }
                }
            } else if (holder instanceof HeaderViewHolder) {

                HeaderViewHolder vhItemView = (HeaderViewHolder) holder;

                String deviceId = mUtil.null2string(hmItem.get(Constants.KEY_DEVICE_ID));
                String nodeId = mUtil.null2string(hmItem.get(Constants.KEY_NODE_ID));
                String nodeName = mUtil.null2string(hmItem.get(Constants.KEY_NODE_NAME));

                vhItemView.edtName.setText(nodeName);

                vhItemView.edtName.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        if (v.getId() == R.id.edtName && actionId == EditorInfo.IME_ACTION_DONE) { // 뷰의 id를 식별, 키보드의 완료 키 입력 검출

                            //이 부분에 원하는 이벤트를 작성합니다
                            //이 부분에 원하는 이벤트를 작성합니다
                            //이 부분에 원하는 이벤트를 작성합니다

                        }

                        return false;
                    }

                });
                vhItemView.edtName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (!hasFocus) {
                            // code to execute when EditText loses focus
                            Dlog.v("v.gettext", ((EditText) v).getText());

                        }
                    }
                });
            }
            holder.itemView.setTag(hmItem);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void setDispOffline(ItemViewHolder view, int imageId) {

//        mUtil.getView(view, R.id.ivRowBody1_Icon).setBackgroundResource(imageId);
        view.tvRowBody1_Title.setTextColor(Util.getColor(mContext, R.color.color_text_01));
        view.tvRowBody1_Title.setText(mUtil.getString(mContext, R.string.layout_offline));
        view.llRowBody1_Control.setVisibility(View.GONE);
    }

    @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;
        }
    }


    /**
     * 1. ClassName :   ItemViewHolder
     * 2. FileName  :   FragmentThingsAdapter.java
     * 3. Package   :   com.ntels.onecable.admin.view.things
     * 4. Date      :   2016-11-28
     * 5. Author    :   aaron
     * 6. Comment   :   Initialize
     */
    public static class ItemViewHolder extends RecyclerView.ViewHolder {

        public LinearLayout llRowRoot;
        public LinearLayout llRowBody;
        public LinearLayout llRowBody1;
        public LinearLayout llRowBody1_Contents;
        public TextView tvRowBody1_Title;
        public RelativeLayout rlRowBody1_Control;
        public LinearLayout llRowBody1_Control;
        public Button btnRowBody1_Contents_On;
        public Button btnRowBody1_Contents_Off;
        public LinearLayout llLoading1;
        public LinearLayout llRowBody1_LastTime;
        public TextView tvRowBody1_Lasttime;
        public LinearLayout llRowBody2;
        public TextView tvRowBody2_Title;
        public RelativeLayout rlRowBody2_Body;
        public LinearLayout llRowBody2_Body;
        public TextView tvRowBody2_Body;
        public Button btnRowBody2_Control;
        public LinearLayout llLoading2;

        /**
         * 생성자.
         *
         * @param itemView Item view holder에 사용 할 view.
         */
        public ItemViewHolder(View itemView) {

            super(itemView);
            llRowRoot = (LinearLayout) itemView.findViewById(R.id.llRowRoot);
            llRowBody = (LinearLayout) itemView.findViewById(R.id.llRowBody);
            llRowBody1 = (LinearLayout) itemView.findViewById(R.id.llRowBody1);
            llRowBody1_Contents = (LinearLayout) itemView.findViewById(R.id.llRowBody1_Contents);
            tvRowBody1_Title = (TextView) itemView.findViewById(R.id.tvRowBody1_Title);
            rlRowBody1_Control = (RelativeLayout) itemView.findViewById(R.id.rlRowBody1_Control);
            llRowBody1_Control = (LinearLayout) itemView.findViewById(R.id.llRowBody1_Control);
            btnRowBody1_Contents_On = (Button) itemView.findViewById(R.id.btnRowBody1_Contents_On);
            btnRowBody1_Contents_Off = (Button) itemView.findViewById(R.id.btnRowBody1_Contents_Off);
            llLoading1 = (LinearLayout) itemView.findViewById(R.id.llLoading1);
            llRowBody1_LastTime = (LinearLayout) itemView.findViewById(R.id.llRowBody1_LastTime);
            tvRowBody1_Lasttime = (TextView) itemView.findViewById(R.id.tvRowBody1_Lasttime);
            llRowBody2 = (LinearLayout) itemView.findViewById(R.id.llRowBody2);
            tvRowBody2_Title = (TextView) itemView.findViewById(R.id.tvRowBody2_Title);
            rlRowBody2_Body = (RelativeLayout) itemView.findViewById(R.id.rlRowBody2_Body);
            llRowBody2_Body = (LinearLayout) itemView.findViewById(R.id.llRowBody2_Body);
            tvRowBody2_Body = (TextView) itemView.findViewById(R.id.tvRowBody2_Body);
            btnRowBody2_Control = (Button) itemView.findViewById(R.id.btnRowBody2_Control);
            llLoading2 = (LinearLayout) itemView.findViewById(R.id.llLoading2);
        }
    }

    /**
     * <pre>
     * 1. ClassName :   HeaderViewHolder
     * 2. FileName  :   FragmentThingsAdapter.java
     * 3. Package   :   com.ntels.onecable.admin.view.things
     * 4. Date      :   2016-11-28
     * 5. Author    :   aaron
     * 6. Comment   :   Initialize
     * </pre>
     */
    public static class HeaderViewHolder extends RecyclerView.ViewHolder {

        public LinearLayout llHeaderRoot;
        public LinearLayout llHeaderBody;
        public EditText edtName;

        public HeaderViewHolder(View itemView) {

            super(itemView);
            llHeaderRoot = (LinearLayout) itemView.findViewById(R.id.llHeaderRoot);
            llHeaderBody = (LinearLayout) itemView.findViewById(R.id.llHeaderBody);
            edtName = (EditText) itemView.findViewById(R.id.edtName);
        }
    }

    /**
     * <pre>
     * 1. ClassName :   FooterViewHolder
     * 2. FileName  :   FragmentThingsAdapter.java
     * 3. Package   :   com.ntels.onecable.admin.view.things
     * 4. Date      :   2016-11-28
     * 5. Author    :   aaron
     * 6. Comment   :   Initialize
     * </pre>
     */
    public static class FooterViewHolder extends RecyclerView.ViewHolder {

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

        /**
         * 생성자.
         *
         * @param itemView Footer view holder에 사용할 view
         */
        public FooterViewHolder(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);
        }
    }

}
