package com.light.prank.sound.app.allui;

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.light.prank.sound.app.dataListener.OnClickItemListener;
import com.light.prank.sound.app.dataListener.PrankBean;
import com.light.prank.sound.app.utils.MyUtils;
import com.prank.tool.sounds.app.databinding.CategoryListAdapterBinding;

import java.util.List;

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

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

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

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(CategoryListAdapterBinding.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]));
        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 CategoryListAdapterBinding vb;

        public ViewHolder(CategoryListAdapterBinding 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);
        }
    }
}