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

package com.ntels.onecable.view.things;

import com.ntels.onecable.R;
import com.ntels.onecable.common.AppConfig;
import com.ntels.onecable.common.Constants;
import com.ntels.onecable.common.c_interface.ListInterface;
import com.ntels.onecable.common.util.Util;
import com.ntels.onecable.common.util.recyclerview.ItemTouchHelperAdapter;
import com.ntels.onecable.common.util.recyclerview.OnStartDragListener;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
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 java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import static android.view.View.GONE;

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

    public static final int TYPE_HEADER = 0;
    public static final int TYPE_ITEM = 1;
    public static final int TYPE_FOOTER = 2;
    private ListInterface adapterCallback;
    private AppConfig appConfig;
    private Util util = new Util();
    private ArrayList<HashMap<String, Object>> alTotalList = new ArrayList<HashMap<String, Object>>();
    private Context context;
    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;
    private final OnStartDragListener mDragStartListener;


    /**
     * 생성자.
     *
     * @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 FragmentThingsAdapter(Context context, int listLayoutHead, int listLayoutItem,
                                 int listLayoutFooter, ArrayList<HashMap<String, Object>> alTmp,
                                 ListInterface adapterCallback, OnStartDragListener dragStartListener) {

        this.context = context;
        alTotalList = alTmp;
        this.listLayoutHead = listLayoutHead;
        this.listLayoutItem = listLayoutItem;
        this.listLayoutFooter = listLayoutFooter;
        appConfig = (AppConfig) this.context.getApplicationContext();
        mDragStartListener = dragStartListener;

        try {
            this.adapterCallback = 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();
    }

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

                // (장치의) 접속 상태 (ON / OFF / none)
                String onlineState = util.null2string(hmItem.get(Constants.KEY_DEVICE_ONLINE));
                // (default) 커맨드 클래스 값의 단위
                String unit = util.null2string(hmItem.get(Constants.KEY_UNIT));
                // (default) 커맨드클래스 유형 ID
//                String cmdclsTypeId = util.null2string(hmItem.get(Constants.KEY_CMDCLS_TYPE_ID));
                // 디바이스클래스 타입 ID
                String deviceclassTypeId = util.null2string(hmItem.get(Constants.KEY_DEVICECLASS_TYPE_ID));

                // 2016.08.03 노드 이미지 로컬에 있는 이미지를 사용하는걸로 변경.
                itemViewHolerView.ivRowIcon.setImageResource(util.getImageId(context, "img_" + deviceclassTypeId));

//                itemViewHolerView.llRowOffline.setVisibility(View.GONE);
                itemViewHolerView.btnRowControl.setBackgroundResource(0);
                itemViewHolerView.btnRowControl.setVisibility(GONE);
                itemViewHolerView.ivRowDelete.setVisibility(GONE);
                itemViewHolerView.ivRowMove.setVisibility(GONE);

                // 장치명
                String deviceName = util.null2string(hmItem.get(Constants.KEY_PRD_USER_NAME));
                if ("".equals(deviceName)) {
                    deviceName = util.null2string(hmItem.get(Constants.KEY_PRD_NAME));
                }
                itemViewHolerView.tvRowTitle.setText(deviceName);

                // (default) 커맨드클래스 유형 ID number
                int cmdclsTypeIdNum = util.null2int(hmItem.get(Constants.KEY_CMDCLS_TYPE_ID));
                // (default) 커맨드클래스 장치 상태정보 값 (N-ISC 질의 결과 ,none = 정보없음)
                String contentValue = util.null2string(hmItem.get(Constants.KEY_CONTENT_VALUE));
                String tempValue = "";
                String select = util.getStringValues(context, "CMDCLS_TYPE_ID_" + cmdclsTypeIdNum + "_BG_DEFAULT");
                if (contentValue.equals("none")) {
                    tempValue = util.getStringValues(context, "CMDCLS_TYPE_ID_" + cmdclsTypeIdNum + "_" + select);
                    contentValue = select;
                } else {
                    tempValue = util.getStringValues(context, "CMDCLS_TYPE_ID_" + cmdclsTypeIdNum + "_" + contentValue);
                }

                final View vLoding = itemViewHolerView.llLoading;
                String requestTime = util.null2string(hmItem.get(Constants.KEY_REQUEST_TIME));
                if (!requestTime.equals("")) {
                    itemViewHolerView.llLoading.setVisibility(View.VISIBLE);
                } else {
                    itemViewHolerView.llLoading.setVisibility(GONE);
                }

                itemViewHolerView.btnRowControl.setTag(position);
                switch (cmdclsTypeIdNum) {
                    //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.tvRowControl.setText(tempValue);
                        if (contentValue.equals(select)) {
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_01));
                        } else {
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, 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:
                        itemViewHolerView.tvRowControl.setText(tempValue);
                        itemViewHolerView.btnRowControl.setVisibility(View.VISIBLE);

                        if (contentValue.equals(select)) {
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_01));
                            itemViewHolerView.btnRowControl.setBackgroundResource(R.drawable.img_things_list_btn_onoff_on);
                        } else {
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_02));
                            itemViewHolerView.btnRowControl.setBackgroundResource(R.drawable.img_things_list_btn_onoff_off);
                        }

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

                            @Override
                            public void onClick(View view) {

                                if (alTotalList == null || alTotalList.isEmpty()) {
                                    return;
                                }

                                tag = (int) view.getTag();
                                String cmdclsTypeId = util.null2string(alTotalList.get(tag).get(Constants.KEY_CMDCLS_TYPE_ID));
                                String contentValue = util.null2string(alTotalList.get(tag).get(Constants.KEY_CONTENT_VALUE));
                                if (contentValue.equals(util.getStringValues(context, "CMDCLS_TYPE_ID_" + cmdclsTypeId + "_BG_DEFAULT"))) {
                                    setHttpValue("ON");
                                } else {
                                    setHttpValue("OFF");
                                }
                                vLoding.setVisibility(View.VISIBLE);
                                adapterCallback.onCallAction(3, tag);
                            }
                        });
                        break;
                    case Constants.CMDCLS_TYPE_ID_36002:
                        itemViewHolerView.tvRowControl.setText(tempValue);
                        itemViewHolerView.btnRowControl.setVisibility(View.VISIBLE);

                        if (contentValue.equals(select)) {
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_01));
                        } else {
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_02));
                        }

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

                            @Override
                            public void onClick(View view) {

                                tag = (int) view.getTag();
                                setHttpValue("CLOSE");
                                vLoding.setVisibility(View.VISIBLE);
                                adapterCallback.onCallAction(3, 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.tvRowControl.setText("-");
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_01));
                        } else {
                            itemViewHolerView.tvRowControl.setText(contentValue + unit);
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_02));
                        }
                        break;
                    //METER_CURRENT
                    case Constants.CMDCLS_TYPE_ID_15001:
                    case Constants.CMDCLS_TYPE_ID_15002:
                        if (contentValue.equals("none")) {
                            itemViewHolerView.tvRowControl.setText("-");
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_01));
                        } else {
                            itemViewHolerView.tvRowControl.setText(contentValue + unit);
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, 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 (contentValue.equals("none")) {
                            itemViewHolerView.tvRowControl.setText("-");
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_01));
                        } else {
                            itemViewHolerView.tvRowControl.setText(contentValue + unit);
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, 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 (contentValue.equals("none")) {
                            itemViewHolerView.tvRowControl.setText("-");
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_01));
                        } else {
                            itemViewHolerView.tvRowControl.setText(contentValue + unit);
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_02));
                        }
                        break;
                    //THERMOSTAT_SETPOINT
                    case Constants.CMDCLS_TYPE_ID_19001:
                        if (contentValue.equals("none")) {
                            itemViewHolerView.tvRowControl.setText("-");
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_01));
                        } else {
                            itemViewHolerView.tvRowControl.setText(contentValue + unit);
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, 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 (contentValue.equals("none")) {
                            itemViewHolerView.tvRowControl.setText("-");
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_01));
                        } else {
                            itemViewHolerView.tvRowControl.setText(contentValue + unit);
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_02));
                        }
                        break;
                    //BATTERY
                    case Constants.CMDCLS_TYPE_ID_30001:
                        if (contentValue.equals("none")) {
                            itemViewHolerView.tvRowControl.setText("-");
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_01));
                        } else {
                            itemViewHolerView.tvRowControl.setText(contentValue + unit);
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, 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 (contentValue.equals("none")) {
                            itemViewHolerView.tvRowControl.setText("-");
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_01));
                        } else {
                            itemViewHolerView.tvRowControl.setText(contentValue + unit);
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_02));
                        }
                        break;
                    case Constants.CMDCLS_TYPE_ID_38001:
                        itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                        itemViewHolerView.tvRowControl.setText("Online");
                        itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_02));
                        break;

                    case Constants.CMDCLS_TYPE_ID_38003:
                        itemViewHolerView.tvRowControl.setText(tempValue);

                        if (contentValue.equals(select)) {
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_01));
                        } else {
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_02));
                        }
                        break;

                    default:
                        if (contentValue.equals("none")) {
                            itemViewHolerView.tvRowControl.setText("-");
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_01));
                        } else {
                            itemViewHolerView.tvRowControl.setText(contentValue + unit);
                            itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_active);
                            itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_02));
                        }
                        break;
                }

                if (isDeleteCheck) {
                    itemViewHolerView.btnRowControl.setVisibility(GONE);
                    itemViewHolerView.ivRowDelete.setVisibility(View.VISIBLE);
//                    itemViewHolerView.llRowOffline.setVisibility(GONE);
                    itemViewHolerView.llLoading.setVisibility(GONE);
                    if (onlineState.equals("OFF") || appConfig.getSelect_Homegrp_Homehub_Online_State().equals("OFF")) {
                        itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                    }
                } else if (isMoveCheck) {
                    itemViewHolerView.btnRowControl.setVisibility(GONE);
                    itemViewHolerView.ivRowMove.setVisibility(View.VISIBLE);
//                    itemViewHolerView.llRowOffline.setVisibility(GONE);
                    itemViewHolerView.llLoading.setVisibility(GONE);
                    if (onlineState.equals("OFF") || appConfig.getSelect_Homegrp_Homehub_Online_State().equals("OFF")) {
                        itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
                    }
                } else {
                    if (onlineState.equals("OFF") || appConfig.getSelect_Homegrp_Homehub_Online_State().equals("OFF")) {
                        itemViewHolerView.ivRowIcon.setBackgroundResource(R.drawable.img_thing_icon_bg_default);
//                        itemViewHolerView.llRowControl.setVisibility(GONE);
//                        itemViewHolerView.llRowDelete.setVisibility(GONE);
//                        itemViewHolerView.llRowOffline.setVisibility(View.VISIBLE);

                        itemViewHolerView.tvRowControl.setText(util.getString(context, R.string.layout_offline));
                        itemViewHolerView.tvRowControl.setTextColor(util.getColor(context, R.color.color_text_01));
                        itemViewHolerView.llLoading.setVisibility(GONE);

                    } else {
                        itemViewHolerView.btnRowControl.setVisibility(View.VISIBLE);
//                        itemViewHolerView.llRowDelete.setVisibility(GONE);
//                        itemViewHolerView.llRowOffline.setVisibility(GONE);

                    }
                }

                itemViewHolerView.rlRowBody.setTag(position);
                itemViewHolerView.rlRowBody.setOnClickListener(new View.OnClickListener() {
                    int tag = 0;
                    private Intent intent;

                    @Override
                    public void onClick(View view) {

                        tag = (int) view.getTag();
                        if (isListCheck) {
                            adapterCallback.onCallAction(2, tag);
                        } else {
                            adapterCallback.onCallAction(1, tag);
                        }
                    }
                });
                itemViewHolerView.ivRowDelete.setTag(position);
                itemViewHolerView.ivRowDelete.setOnClickListener(new View.OnClickListener() {
                    int tag = 0;
                    private Intent intent;

                    @Override
                    public void onClick(View view) {

                        tag = (int) view.getTag();
                        if (isDeleteCheck) {
                            adapterCallback.onCallAction(2, tag);
                        }
                    }
                });

                // Start a drag whenever the handle view it touched
                itemViewHolerView.ivRowMove.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                            mDragStartListener.onStartDrag(itemViewHolerView);
                        }
                        return false;
                    }
                });
            } else if (holder instanceof FooterViewHolder) {

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

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

                            @Override
                            public void onClick(View view) {

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

    @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.view.things
     * 4. Date      :   2016-11-28
     * 5. Author    :   aaron
     * 6. Comment   :   Initialize
     */
    public static class ItemViewHolder extends RecyclerView.ViewHolder {

        public LinearLayout llRowRoot;
        public RelativeLayout rlRowBody;
        public ImageView ivRowIcon;
        public TextView tvRowTitle;
        public TextView tvRowControl;
        public Button btnRowControl;
        public ImageView ivRowMove;
        public ImageView ivRowDelete;
        public LinearLayout llLoading;


//        public LinearLayout llRowBody;
//        public LinearLayout llRowControl;
//        public LinearLayout llRowControlBtn;
//        public LinearLayout llRowDelete;
//        public LinearLayout llRowOffline;
//        public TextView tvRowBody;
//        public TextView tvRowOffline;

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

            super(itemView);
            llRowRoot = (LinearLayout) itemView.findViewById(R.id.llRowRoot);
            rlRowBody = (RelativeLayout) itemView.findViewById(R.id.rlRowBody);
            ivRowIcon = (ImageView) itemView.findViewById(R.id.ivRowIcon);
            tvRowTitle = (TextView) itemView.findViewById(R.id.tvRowTitle);
            tvRowControl = (TextView) itemView.findViewById(R.id.tvRowControl);
            btnRowControl = (Button) itemView.findViewById(R.id.btnRowControl);
            ivRowMove = (ImageView) itemView.findViewById(R.id.ivRowMove);
            ivRowDelete = (ImageView) itemView.findViewById(R.id.ivRowDelete);
            llLoading = (LinearLayout) itemView.findViewById(R.id.llLoading);

//            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);
//            tvRowBody = (TextView) itemView.findViewById(R.id.tvRowBody);
//            tvRowOffline = (TextView) itemView.findViewById(R.id.tvRowOffline);
        }
    }

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

        public LinearLayout llRowRoot;
        public LinearLayout llRowBody;
        public TextView tvTitle;

        public HeaderViewHolder(View itemView) {

            super(itemView);
        }
    }

    /**
     * <pre>
     * 1. ClassName :   FooterViewHolder
     * 2. FileName  :   FragmentThingsAdapter.java
     * 3. Package   :   com.ntels.onecable.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);
        }
    }

    @Override
    public void onItemDismiss(int position) {
        alTotalList.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        Collections.swap(alTotalList, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
        return true;
    }
}
