package com.rdiot.yx485.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.rdiot.yx485.bean.DeviceRoomListBean;
import com.rdiot.yx485.databinding.RoomItemEditBinding;

import java.util.List;

public class RoomEditAdapter extends RecyclerView.Adapter<RoomEditAdapter.ViewHolder> {
    private final OnDeleteClickListener mDeleteClickListener;
    private final List<DeviceRoomListBean.RoomBean> mData;

    public RoomEditAdapter(List<DeviceRoomListBean.RoomBean> data, OnDeleteClickListener listener) {
        mData = data;
        mDeleteClickListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RoomItemEditBinding binding = RoomItemEditBinding.inflate(
                LayoutInflater.from(parent.getContext()),
                parent,
                false
        );
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DeviceRoomListBean.RoomBean room = mData.get(position);
        holder.binding.roomName.setText(room.name);

        holder.binding.roomIcon.setOnClickListener(v -> {
            if (mDeleteClickListener != null) {
                mDeleteClickListener.onDeleteClick(room.record_id);
            }
        });

        int count = room.devices == null ? 0 : room.devices.size();
        String deviceCount = String.format("%s个设备", count);
        holder.binding.deviceCount.setText(deviceCount);
//        holder.binding.deviceCount.setOnClickListener(view -> {
//            Intent intent = new Intent(holder.binding.deviceCount.getContext(), RoomSetDeviceActivity.class);
//            intent.putExtra("roomid", room.record_id);
//            intent.putExtra("number", deviceCount);
//            intent.putExtra("name", room.name);
//            holder.binding.deviceCount.getContext().startActivity(intent);
//        });
        holder.binding.bottomLine.setVisibility(position == mData.size() - 1 ? View.INVISIBLE : View.VISIBLE);
    }

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

    public List<DeviceRoomListBean.RoomBean> getData() {
        return mData;
    }

    public interface OnDeleteClickListener {
        void onDeleteClick(String roomId);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        RoomItemEditBinding binding;

        ViewHolder(RoomItemEditBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}