package com.datarecovery.master.module.homepage;

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

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

import com.datarecovery.master.databinding.ItemHomePageOtherFunctionBinding;

import java.util.List;

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


    @NonNull
    private final List<FunctionBean> beanList;

    private onItemClick onItemClick;

    public OtherFunctionAdapter(@NonNull List<FunctionBean> beanList) {
        this.beanList = beanList;
    }

    public void setOnItemClick(OtherFunctionAdapter.onItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

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

    @Override
    public void onBindViewHolder(@NonNull OtherFunctionAdapter.ViewHolder holder, int position) {
        holder.bind(beanList.get(position));
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        ItemHomePageOtherFunctionBinding binding;

        public ViewHolder(@NonNull ItemHomePageOtherFunctionBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            binding.getRoot().setOnClickListener(v -> {
                if (onItemClick != null) onItemClick.onClick(binding.getBean());
            });
        }

        public void bind(FunctionBean bean) {
            binding.setBean(bean);
        }
    }

    public interface onItemClick {
        void onClick(FunctionBean bean);
    }
}
