package com.prank.sound.app.adapter;

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

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.prank.sound.app.databinding.ItemHomeBinding;
import com.prank.sound.app.mydata.Category;
import com.prank.sound.app.pageview.InfoActivity;
import com.prank.sound.app.myutils.MyValues;

import java.util.List;


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

    private final List<Category> mValues;

    private Context mCon;


    public CategoryAdapter(Context context, List<Category> items) {
        mValues = items;
        this.mCon = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new ViewHolder(ItemHomeBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        int i = position % 5;
        holder.vb.relative.setBackground(ContextCompat.getDrawable(mCon, MyValues.bgs[i]));
        Category category = mValues.get(position);
        String covert = category.getCovert();
        Glide.with(mCon).load(covert).into(holder.vb.image);
        holder.vb.name.setText(category.getName());
        holder.vb.relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mCon, InfoActivity.class);
                intent.putExtra(MyValues.KEY_LIST_DATA, category);
                intent.putExtra(MyValues.KEY_LIST_COLOR_POS, i);
                mCon.startActivity(intent);
            }
        });

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ItemHomeBinding vb;

        public ViewHolder(ItemHomeBinding binding) {
            super(binding.getRoot());
            vb = binding;

        }

    }
}