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.R;
import com.prank.sound.app.databinding.ItemFavoriteBinding;
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 FavoriteAdapter extends RecyclerView.Adapter<FavoriteAdapter.ViewHolder> {

    private List<ChildData> mValues;
    private Context mCOn;

    public FavoriteAdapter(Context context, List<ChildData> items) {
        mValues = items;
        mCOn = context;
    }

    public void updateData(List<ChildData> items){
        mValues = items;
        notifyDataSetChanged();
    }

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

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

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        ChildData childData = mValues.get(position);
        holder.vb.ttvName.setText(childData.getName());
        int i = position % 5;
        holder.vb.frameIm.setBackground(ContextCompat.getDrawable(mCOn, MyValues.bgs[i]));
        if(childData.isCustomization()){
            Glide.with(mCOn).load(R.drawable.import_audio).into(holder.vb.image);
        }else {
            Glide.with(mCOn).load(childData.getCovert()).into(holder.vb.image);
        }


        holder.vb.frameIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mCOn, PlaySoundsActivity.class);
                intent.putExtra(MyValues.KEY_PLAY_DATA, childData);
                mCOn.startActivity(intent);
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        public ItemFavoriteBinding vb;

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

        }

    }
}