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

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

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 java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by jinochoi on 2017. 6. 19..
 */

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

    private Util mUtil = new Util();
    private ListInterface adapterCallback;
    private ArrayList<HashMap<String, Object>> mHomehubList;

    HomehubBleStep4Adapter(ArrayList<HashMap<String, Object>> homehubList, ListInterface adapterCallback) {
        this.mHomehubList = homehubList;
        try {
            this.adapterCallback = adapterCallback;
        } catch (ClassCastException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_homehub_ssid_list, parent, false);

        return new Device(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        HashMap<String, Object> hmItem = mHomehubList.get(position);
        Device deviceView = (Device) holder;

        String ssid = mUtil.null2string(hmItem.get(Constants.KEY_SSID));
        String security = mUtil.null2string(hmItem.get(Constants.KEY_SECURITY));
        String selectYn = mUtil.null2string(hmItem.get(Constants.KEY_SELECT_YN));

        if (selectYn.equals("Y")){
            deviceView.llRoot.setSelected(true);
        } else {
            deviceView.llRoot.setSelected(false);
        }

        deviceView.tvSsidName.setText(ssid);
        if (security.endsWith("none")) {
            deviceView.ivIcon.setBackgroundResource(R.drawable.img_list_network_wifi_unlock);
        } else {
            deviceView.ivIcon.setBackgroundResource(R.drawable.img_list_network_wifi_lock);
        }

        deviceView.llRoot.setTag(position);
        deviceView.llRoot.setOnClickListener(new View.OnClickListener() {
            int tag = 0;

            @Override
            public void onClick(View view) {
                tag = (int) view.getTag();
                adapterCallback.onCallAction(1, tag);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mHomehubList.size();
    }


    /**
     * position에 해당하는 디바이스 정보를 반환한다.
     *
     * @param position 리스트 내 디바이스 index
     *
     * @return position에 해당하는 디바이스 정보
     */
    public HashMap<String, Object> getItem(int position) {
        return mHomehubList.get(position);
    }

    /**
     * Homehub register adapter.
     *
     * @author aaron
     */

    static class ViewHolder {
        TextView deviceName;
        TextView deviceAddress;
    }

    /**
     * Item view holder.
     *
     * @author aaron
     */
    public class Device extends RecyclerView.ViewHolder {

        public LinearLayout llRoot;
        public TextView tvSsidName;
        public ImageView ivIcon;

        /**
         * Device view holder 생성자.
         *
         * @param itemView Recycler에 보여줄 view
         */
        public Device(View itemView) {
            super(itemView);
            llRoot = (LinearLayout) itemView.findViewById(R.id.llRoot);
            ivIcon = (ImageView) itemView.findViewById(R.id.ivIcon);
            tvSsidName = (TextView) itemView.findViewById(R.id.tvSsidName);
        }
    }
}