package com.rdiot.yx485.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.rdiot.yx485.R;
import com.rdiot.yx485.bean.DefHomeRoomBean;

import java.util.List;

public class HomeAddRoomtAdapter extends RecyclerView.Adapter<HomeAddRoomtAdapter.RoomViewHolder> {

    private List<DefHomeRoomBean.DataBean.CityInfo> roomList;
    private OnItemClickListener listener;
    public HomeAddRoomtAdapter(List<DefHomeRoomBean.DataBean.CityInfo> roomList, OnItemClickListener mlistener) {
        this.roomList = roomList;
        listener=mlistener;
    }

    @NonNull
    @Override
    public RoomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_room_add, parent, false);
        return new RoomViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RoomViewHolder holder, int position) {
        DefHomeRoomBean.DataBean.CityInfo room = roomList.get(position);
        if(!TextUtils.isEmpty(room.name)){
            holder.roomName.setText(room.name);

        }
        holder.room_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onItemClick(position);
                }
            }
        });
    }

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

    public List<DefHomeRoomBean.DataBean.CityInfo> getdate() {
        return roomList;
    }
    public void setdate(List<DefHomeRoomBean.DataBean.CityInfo> list) {
         roomList=list;
    }

    static class RoomViewHolder extends RecyclerView.ViewHolder {
        TextView roomName;
        TextView deviceCount;
        ImageView room_icon;

        RoomViewHolder(View itemView) {
            super(itemView);
            roomName = itemView.findViewById(R.id.room_name);
            room_icon = itemView.findViewById(R.id.room_icon);
            deviceCount = itemView.findViewById(R.id.device_count);
        }
    }
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}