package com.prank.sounds.fart.myadapter;

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

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

import com.bumptech.glide.Glide;
import com.prank.sounds.fart.bean.Info;
import com.prank.sounds.fart.databinding.RecyclerViewAllBinding;
import com.prank.sounds.fart.callback.OnClickSounds;
import com.prank.sounds.fart.tools.MyUtils;

import java.util.List;
public class AdapterViewAll extends RecyclerView.Adapter<AdapterViewAll.ViewHolder> {
    private final List<Info> mValues;
    private Context mCon;
    private int type;
    private OnClickSounds mListener;
    private int colorIndex;
    public void setColorIndex(int colorIndex) {
        this.colorIndex = colorIndex;
    }
    public AdapterViewAll(Context context, List<Info> items, int mType, OnClickSounds listener) {
        mValues = items;
        this.mCon = context;
        type = mType;
        mListener = listener;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(RecyclerViewAllBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.vb.relative.setBackground(ContextCompat.getDrawable(mCon, MyUtils.childBgs[colorIndex]));
        Info info = mValues.get(position);
        String covert = info.getCovert();
        Glide.with(mCon).load(covert).into(holder.vb.image);
        holder.vb.name.setText(info.getName());
        holder.vb.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onClickSoundsListener(info);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        private RecyclerViewAllBinding vb;
        public ViewHolder(RecyclerViewAllBinding binding) {
            super(binding.getRoot());
            vb = binding;
            ViewGroup.LayoutParams layoutParams = vb.relative.getLayoutParams();
            if(type == 0){
                layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
                vb.name.setMaxLines(2);
            }else {
                layoutParams.width = (int) MyUtils.dpToPx(130);
                vb.name.setMaxLines(1);
            }
            vb.relative.setLayoutParams(layoutParams);
        }
    }
}