package com.prank.tool.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.R;
import com.prank.tool.databinding.ItemInfoBinding;
import com.prank.tool.interf.OnSoundsListener;
import com.prank.tool.jbean.Info;
import com.prank.tool.utils.Tools;

import java.util.List;


public class AdapterChild extends RecyclerView.Adapter<AdapterChild.ViewHolder> {

    private final List<Info> mValues;

    private Context mCon;
    private int type;
    private OnSoundsListener mListener;
    private int colorIndex;

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

    public AdapterChild(Context context, List<Info> items, int mType, OnSoundsListener listener) {
        mValues = items;
        this.mCon = context;
        type = mType;
        mListener = listener;

    }

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

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

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        holder.vb.relative.setBackground(ContextCompat.getDrawable(mCon, Tools.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 ItemInfoBinding vb;

        public ViewHolder(ItemInfoBinding binding) {
            super(binding.getRoot());
            vb = binding;
            ViewGroup.LayoutParams layoutParams = vb.relative.getLayoutParams();
            if(type == 0){
                layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
//                vb.relative.setBackground(ContextCompat.getDrawable(mCon, R.drawable.rect_main_gradient));
                vb.name.setMaxLines(2);
            }else {
                layoutParams.width = (int) Tools.dpToPx(130);
                vb.relative.setBackground(ContextCompat.getDrawable(mCon, R.drawable.rect_more));
                vb.name.setMaxLines(1);
            }
            vb.relative.setLayoutParams(layoutParams);





        }

    }
}