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.ItemInfoBinding;
import com.prank.sound.app.mydata.ChildData;
import com.prank.sound.app.pageview.PlaySoundsActivity;
import com.prank.sound.app.myutils.MyValues;

import java.util.List;


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

    private final List<ChildData> mValues;

    private Context mCon;
    private int i;

    public InfoAdapter(Context context, List<ChildData> items, int colorIndex) {
        mValues = items;
        this.mCon = context;
        i = colorIndex;
    }

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

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

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        ChildData childData = mValues.get(position);
        String covert = childData.getCovert();
        Glide.with(mCon).load(covert).into(holder.vb.image);
        holder.vb.name.setText(childData.getName());
        holder.vb.frameIm.setBackground(ContextCompat.getDrawable(mCon, MyValues.bgs[i]));
        holder.vb.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mCon, PlaySoundsActivity.class);
                intent.putExtra(MyValues.KEY_PLAY_DATA, childData);
                intent.putExtra(MyValues.KEY_PLAY_COLOR_POS, i);
                mCon.startActivity(intent);
            }
        });

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ItemInfoBinding vb;

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

        }

    }
}