package com.fun.wave.pranksounds.main;

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.fun.wave.pranksounds.databinding.ListAdapterBinding;
import com.fun.wave.pranksounds.listener.ClickSoundsListener;
import com.fun.wave.pranksounds.listener.PrankBean;
import com.fun.wave.pranksounds.utils.Common;

import java.util.List;

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
    private final List<PrankBean> mValues;
    private Context mCon;
    private int type;
    private ClickSoundsListener mListener;
    private int colorIndex;

    public void setColorIndex(int colorIndex) {
        this.colorIndex = colorIndex;
    }

    public ListAdapter(Context context, List<PrankBean> items, int mType, ClickSoundsListener listener) {
        mValues = items;
        this.mCon = context;
        type = mType;
        mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(ListAdapterBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }



    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.vb.relative.setBackground(ContextCompat.getDrawable(mCon, Common.childBgs[colorIndex]));
        PrankBean PrankBean = mValues.get(position);
        String covert = PrankBean.getCovert();
        Glide.with(mCon).load(covert).into(holder.vb.image);
        holder.vb.name.setText(PrankBean.getName());
        holder.vb.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onClickSoundsListener(PrankBean);
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ListAdapterBinding vb;

        public ViewHolder(ListAdapterBinding 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) Common.dpToPx(130);
                vb.name.setMaxLines(1);
            }
            vb.relative.setLayoutParams(layoutParams);
        }
    }
}