package com.prank.tool.sounds.app.adapter;

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.tool.sounds.app.common.MyUtils;
import com.prank.tool.sounds.app.data.detail;
import com.prank.tool.sounds.app.data.OnSounds;
import com.prank.tool.sounds.app.databinding.AdaListBinding;

import java.util.List;

public class AdaList extends RecyclerView.Adapter<AdaList.ViewHolder> {
    private final List<detail> mValues;
    private Context mCon;
    private int type;
    private OnSounds mListener;
    private int colorIndex;

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

    public AdaList(Context context, List<detail> items, int mType, OnSounds listener) {
        mValues = items;
        this.mCon = context;
        type = mType;
        mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(AdaListBinding.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]));
        detail detail = mValues.get(position);
        String covert = detail.getCovert();
        Glide.with(mCon).load(covert).into(holder.vb.image);
        holder.vb.name.setText(detail.getName());
        holder.vb.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onClickSoundsListener(detail);
            }
        });
    }

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

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

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