package com.rdiot.yx485.adapter;

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

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

import com.rdiot.yx485.R;
import com.rdiot.yx485.bean.DeviceRoomListBean;
import com.rdiot.yx485.room.RoomSetDeviceActivity;

import java.util.List;

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

    private List<DeviceRoomListBean.RoomBean> roomList;

    public RoomAdapter(List<DeviceRoomListBean.RoomBean> roomList) {
        this.roomList = roomList;
    }

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

    @Override
    public void onBindViewHolder(@NonNull RoomViewHolder holder, int position) {
        DeviceRoomListBean.RoomBean room = roomList.get(position);
        if (!TextUtils.isEmpty(room.name)) {
            holder.roomName.setText(room.name);
        }
        int deviceCount = room.devices == null ? 0 : room.devices.size();
        holder.deviceCount.setText(deviceCount + "个设备");
        holder.deviceCount.setOnClickListener(v -> {
            Intent intent = new Intent(holder.deviceCount.getContext(), RoomSetDeviceActivity.class);
            intent.putExtra("roomid", room.record_id);
            intent.putExtra("number", deviceCount);
            intent.putExtra("name", room.name);
            holder.deviceCount.getContext().startActivity(intent);
        });

        holder.bottomLine.setVisibility(position == roomList.size() - 1 ? View.INVISIBLE : View.VISIBLE);
    }

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

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

        View bottomLine;

        RoomViewHolder(View itemView) {
            super(itemView);
            roomName = itemView.findViewById(R.id.room_name);
            deviceCount = itemView.findViewById(R.id.device_count);
            bottomLine = itemView.findViewById(R.id.bottom_line);
        }
    }
}