package com.hesiii.i_hear.Adapters;

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.hesiii.i_hear.Interfaces.CategoryEvents;
import com.hesiii.i_hear.Models.Category;
import com.hesiii.i_hear.R;
import com.hesiii.i_hear.databinding.CategoryViewBinding;

import java.util.List;

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.CategoryViewHolder> {

    private Context context;
    private List<Category>categoryList;
    private CategoryEvents categoryEvents;

    public CategoryAdapter(Context context, List<Category> categoryList, CategoryEvents categoryEvents) {
        this.context = context;
        this.categoryList = categoryList;
        this.categoryEvents = categoryEvents;
    }

    @NonNull
    @Override
    public CategoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CategoryViewHolder(LayoutInflater.from(context).inflate(R.layout.category_view, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CategoryViewHolder holder, int position) {

        Category category = categoryList.get(position);

        holder.viewBinding.categoryImage.setImageResource(category.getIcon());
        holder.viewBinding.categoryText.setText(category.getCategoryTitle());

    }

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

    public class CategoryViewHolder extends RecyclerView.ViewHolder{

        public CategoryViewBinding viewBinding;

        public CategoryViewHolder(@NonNull View itemView) {
            super(itemView);

            viewBinding = CategoryViewBinding.bind(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    categoryEvents.onCategoryClick(getAdapterPosition());

                }
            });

        }
    }


}
