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

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

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.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

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

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

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

    private List_Interface mAdapterCallback;

    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              Boolean bSend                   = false;
    private              String  sHTTP_dvcgrp_id         = "";
    private              String  sHTTP_device_id         = "";
    private              int     iType                   = 0;
    private              int     iSetType                = 0;
    private static final int     MSG_COM_GROUPS_DEVICES2 = 1;
    private              Boolean bListCheck              = false;
    private              Boolean bHeaderCheck            = false;
    private              Boolean bFooterCheck            = false;
    private              int     iFooterCnt              = -1;

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

    @Override public int getItemCount() {

        return alTotalList.size();
    }

    public Rules_Things_Select_Adapter(Context tContext, int headerLayoutItem, int listLayoutItem, int footerLayoutItem, ArrayList<HashMap<String, Object>> alTmp, List_Interface mAdapterCallback) {

        Dlog.v("Rules_Things_Select_Adapter ");
        mContext = tContext;
        alTotalList = alTmp;
        mListLayoutHead = headerLayoutItem;
        mListLayoutItem = listLayoutItem;
        mListLayoutFooter = footerLayoutItem;
        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) {

        Dlog.v("onCreateViewHolder ");
        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 vhHeder extends RecyclerView.ViewHolder {

        public LinearLayout llHeaderRoot;
        public LinearLayout llHeaderBody;

        public vhHeder(View itemView) {

            super(itemView);

            try {
//                llHeaderRoot = (LinearLayout) itemView.findViewById(R.id.llHeaderRoot);
//                llHeaderBody = (LinearLayout) itemView.findViewById(R.id.llHeaderBody);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static class vhItem extends RecyclerView.ViewHolder {

        public LinearLayout llRowRoot;
        public LinearLayout llRowBody;
        public LinearLayout llRowBody_Body;
        public TextView     tvRowTitle;
        public ImageView    ivRowCheck;

        public vhItem(View itemView) {

            super(itemView);
            try {
                llRowRoot = (LinearLayout) itemView.findViewById(R.id.llRowRoot);
                llRowBody = (LinearLayout) itemView.findViewById(R.id.llRowBody);
                llRowBody_Body = (LinearLayout) itemView.findViewById(R.id.llRowBody_Body);
                tvRowTitle = (TextView) itemView.findViewById(R.id.tvRowTitle);
                ivRowCheck = (ImageView) itemView.findViewById(R.id.ivRowCheck);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

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

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

        public vhFooter(View itemView) {

            super(itemView);

            try {
                llFooterProgress = (LinearLayout) itemView.findViewById(R.id.llFooterProgress);
                llFooterRoot = (LinearLayout) itemView.findViewById(R.id.llFooterRoot);
                llFooterBody = (LinearLayout) itemView.findViewById(R.id.llFooterBody);
                tvFooterTitle = (TextView) itemView.findViewById(R.id.tvFooterTitle);
                ivFooterAdd = (ImageView) itemView.findViewById(R.id.ivFooterAdd);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        Dlog.v("onBindViewHolder ");
        try {
            HashMap<String, Object> hmItem = new HashMap<String, Object>();
            hmItem = alTotalList.get(position);

            Dlog.v("jinoo", position + "");
            if ( holder instanceof vhItem ) {
                vhItem vhItemView = (vhItem) holder;
                //                String sHTTP_push_hist_id = util.null2string(hmItem.get(Constants.HTTP_push_hist_id));
                //                String sHTTP_content = util.null2string(hmItem.get(Constants.HTTP_content));
                //                String sHTTP_create_datetime = util.null2string(hmItem.get(Constants.HTTP_create_datetime));
                //                String sHTTP_read_datetime = util.null2string(hmItem.get(Constants.HTTP_read_datetime));
                //                String sHTTP_homegrp_invitation_yn = util.null2string(hmItem.get(Constants.HTTP_homegrp_invitation_yn));
                Dlog.v("jinoo", position + "");
                String sHTTP_source_sub_id = util.null2string(hmItem.get(Constants.HTTP_source_sub_id));
                String sHTTP_source_sub_name = util.null2string(hmItem.get(Constants.HTTP_source_sub_name));
                String sHTTP_control_min_value = util.null2string(hmItem.get(Constants.HTTP_control_min_value));
                String sHTTP_control_max_value = util.null2string(hmItem.get(Constants.HTTP_control_max_value));
                String sHTTP_control_step = util.null2string(hmItem.get(Constants.HTTP_control_step));
                String sHTTP_unit = util.null2string(hmItem.get(Constants.HTTP_unit));
                String sHTTP_data_type_code = util.null2string(hmItem.get(Constants.HTTP_data_type_code));
                String sHTTP_cmdcls_type_id = util.null2string(hmItem.get(Constants.HTTP_cmdcls_type_id));
                String sHTTP_cmdcls_code = util.null2string(hmItem.get(Constants.HTTP_cmdcls_code));
                String sHTTP_cmdcls_type_name = util.null2string(hmItem.get(Constants.HTTP_cmdcls_type_name));
                String sHTTP_cmdcls_type_system_name = util.null2string(hmItem.get(Constants.HTTP_cmdcls_type_system_name));
                String sHTTP_cmdcls_value_list = util.null2string(hmItem.get(Constants.HTTP_cmdcls_value_list));
                String sHTTP_cmdcls_value = util.null2string(hmItem.get(Constants.HTTP_cmdcls_value));
                String sHTTP_condition_type_code = util.null2string(hmItem.get(Constants.HTTP_condition_type_code));
                String sHTTP_condition_type_code_name = util.null2string(hmItem.get(Constants.HTTP_condition_type_code_name));
                String sHTTP_SELECT_YN = util.null2string(hmItem.get(Constants.HTTP_SELECT_YN));
                String sHTTP_DISABLE_YN = util.null2string(hmItem.get(Constants.HTTP_DISABLE_YN));

                vhItemView.tvRowTitle.setText(sHTTP_source_sub_name);

                vhItemView.llRowBody.setTag(position);
                if ( sHTTP_DISABLE_YN.equals("Y") ) {
                    vhItemView.llRowBody.setEnabled(false);
                    vhItemView.llRowBody.setSelected(false);
                } else {
                    vhItemView.llRowBody.setEnabled(true);

                    if ( sHTTP_SELECT_YN.equals("Y") ) {
                        vhItemView.llRowBody.setSelected(true);
                    } else {
                        vhItemView.llRowBody.setSelected(false);
                    }

                    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();
                            Dlog.v("alTotalList.size() = " + alTotalList.size());
                            for ( int iRow = 0; iRow < alTotalList.size(); iRow++ ) {
                                alTotalList.get(iRow).put(Constants.HTTP_SELECT_YN, "N");
                            }
                            alTotalList.get(iTag).put(Constants.HTTP_SELECT_YN, "Y");
                            notifyDataSetChanged();
                        }
                    });
                }
                vhItemView.llRowBody_Body.removeAllViews();
                // 미분류 대상THERMOSTAT_MODE, LOCK, SPEED_CONTROL, AV_CONTROL_PLAYBACK, PRESENCE, SYSTEM, COLOR, METER_CURRENT, METER_TOTAL, THERMOSTAT_SETPOINT, SYSTEM
                // 분류 해야 함
                if ( sHTTP_cmdcls_code.equals("SWITCH_BINARY") || sHTTP_cmdcls_code.equals("SENSOR_BINARY") || sHTTP_cmdcls_code.equals("VALVE") ) {

                    //                    util.getGridLayout(childView, R.id.glRowBody_Body).setColumnCount(2);
                    if ( !sHTTP_cmdcls_value_list.equals("") ) {
                        ArrayList<HashMap<String, Object>> alList_Temp = new ArrayList<HashMap<String, Object>>();
                        alList_Temp = (ArrayList<HashMap<String, Object>>) hmItem.get(Constants.HTTP_cmdcls_value_list);

                        for ( int iRow2 = 0; iRow2 < alList_Temp.size(); iRow2++ ) {
                            LinearLayout childView1 = (LinearLayout) View.inflate(mContext, R.layout.row_rules_things_select_button, null);

                            String sOptionHTTP_cmdcls_value = util.null2string(alList_Temp.get(iRow2).get(Constants.HTTP_cmdcls_value));
                            String sOptionHTTP_cmdcls_value_msg = util.null2string(alList_Temp.get(iRow2).get(Constants.HTTP_cmdcls_value_msg));

                            util.getTextView(childView1, R.id.tvRulesCmd).setText(sOptionHTTP_cmdcls_value_msg);
                            util.getTextView(childView1, R.id.tvRulesCmd).setTag(sOptionHTTP_cmdcls_value);
                            util.getView(childView1, R.id.llRulesCmd).setEnabled(false);

                            if ( sHTTP_cmdcls_value.equals(sOptionHTTP_cmdcls_value) ) {
                                util.getView(childView1, R.id.llRulesCmd).setSelected(true);
                            } else {
                                util.getView(childView1, R.id.llRulesCmd).setSelected(false);
                            }
                            util.getView(childView1, R.id.llRulesCmd).setTag(position);
                            if ( sHTTP_SELECT_YN.equals("Y") ) {
                                util.getView(childView1, R.id.llRulesCmd).setEnabled(true);
                                util.getView(childView1, R.id.llRulesCmd).setOnClickListener(new View.OnClickListener() {
                                    int iTag = 0;

                                    @Override public void onClick(View v) {

                                        iTag = (int) v.getTag();
                                        String sValue = util.null2string(util.getTextView(v, R.id.tvRulesCmd).getTag());
                                        String sTitle = util.null2string(util.getTextViewVal(v, R.id.tvRulesCmd));
                                        Dlog.v("sTitle = " + sTitle);

                                        alTotalList.get(iTag).put(Constants.HTTP_cmdcls_value, sValue);
                                        alTotalList.get(iTag).put(Constants.HTTP_cmdcls_value_msg, sTitle);
                                        alTotalList.get(iTag).put(Constants.HTTP_condition_type_code, "01");
                                        alTotalList.get(iTag).put(Constants.HTTP_condition_type_code_name, util.getString(mContext, R.string.layout_equals));

                                        notifyDataSetChanged();
                                    }
                                });
                            }

                            vhItemView.llRowBody_Body.addView(childView1);
                        }
                    }
                } else if ( sHTTP_cmdcls_code.equals("SENSOR_MULTILEVEL") || sHTTP_cmdcls_code.equals("SWITCH_MULTILEVEL") || sHTTP_cmdcls_code.equals("METER") || sHTTP_cmdcls_code.equals(
                        "BATTERY") ) {
                    //                    if ( !sHTTP_cmdcls_value_list.equals("") ) {
                    //                    util.getGridLayout(childView, R.id.glRowBody_Body).setColumnCount(1);
                    LinearLayout childView1 = (LinearLayout) View.inflate(mContext, R.layout.row_rules_things_select_value, null);
                    util.getTextView(childView1, R.id.tvRulesCmd).setText(sHTTP_cmdcls_value + " " + (sHTTP_cmdcls_value.equals("") ? "" : sHTTP_unit) + " " + (sHTTP_cmdcls_value.equals("")
                                                                                                                                                                ? ""
                                                                                                                                                                : sHTTP_condition_type_code_name));
                    util.getView(childView1, R.id.llRulesCmd).setEnabled(false);
                    if (sHTTP_cmdcls_value.equals("")){
                        util.getView(childView1, R.id.llRulesCmd).setSelected(false);
                    } else {
                        util.getView(childView1, R.id.llRulesCmd).setSelected(true);
                    }
                    util.getView(childView1, R.id.llRulesCmd).setTag(position);
                    if ( sHTTP_SELECT_YN.equals("Y") ) {
                        util.getView(childView1, R.id.llRulesCmd).setEnabled(true);
                        util.getView(childView1, R.id.llRulesCmd).setOnClickListener(new View.OnClickListener() {
                            int iTag = 0;

                            @Override public void onClick(View v) {

                                iTag = (int) v.getTag();

                                mAdapterCallback.onCallAction(1, iTag);
                            }
                        });
                    }

                    vhItemView.llRowBody_Body.addView(childView1);
                }
            } else if ( holder instanceof vhHeder ) {

                vhHeder vhItemView = (vhHeder) holder;
            } else if ( holder instanceof vhFooter ) {

                vhFooter vhItemView = (vhFooter) holder;

                String sMore_Yn = util.null2string(hmItem.get(Constants.HTTP_MORE_YN));

                vhItemView.llFooterBody.setVisibility(View.GONE);
                if ( sMore_Yn.equals("Y") ) {
                    vhItemView.llFooterProgress.setVisibility(View.VISIBLE);
                    vhItemView.llFooterRoot.setVisibility(View.VISIBLE);
                } else {
                    vhItemView.llFooterProgress.setVisibility(View.GONE);
                    vhItemView.llFooterRoot.setVisibility(View.GONE);
                }
            }
            holder.itemView.setTag(hmItem);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private boolean isPositionHeader(int position) {

        return position == 0;
    }

    @Override public int getItemViewType(int position) {

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

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

        return alTotalList;
    }
}
