package com.prank.sound.funnyapp.myadapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.prank.sound.funnyapp.bean.MyInfo;
import com.prank.sound.funnyapp.myinterface.OnClickItem;
import com.prank.sound.funnyapp.databinding.AdapterViewAllBinding;

import java.util.List;
public class AdapterViewAll extends RecyclerView.Adapter<AdapterViewAll.ViewHolder> {
    private final List<MyInfo> mValues;
    private Context mCon;
    private int type;
    private OnClickItem mListener;
    private int colorIndex;
    public void setColorIndex(int colorIndex) {
        this.colorIndex = colorIndex;
    }
    public AdapterViewAll(Context context, List<MyInfo> items, int mType, OnClickItem listener) {
        mValues = items;
        this.mCon = context;
        type = mType;
        mListener = listener;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(AdapterViewAllBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
//        holder.vb.relative.setBackground(ContextCompat.getDrawable(mCon, MyUtils.bgs[colorIndex]));

        if (holder.vb.relative.getTag() == null) {
            holder.vb.relative.setTag(true);

            holder.vb.relative.post(() -> {
                int width = holder.vb.relative.getWidth();
                ViewGroup.LayoutParams params = holder.vb.relative.getLayoutParams();
                params.height = width;
                holder.vb.relative.setLayoutParams(params);
                Log.d("------","------layoutParams.width="+width);
            });
        }

        MyInfo 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 AdapterViewAllBinding vb;
        public ViewHolder(AdapterViewAllBinding binding) {
            super(binding.getRoot());
            vb = binding;
            ViewGroup.LayoutParams layoutParams = vb.relative.getLayoutParams();
            Log.d("------","------layoutParams.width="+layoutParams.width);

//            if(type == 0){
//                layoutParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
//                vb.name.setMaxLines(2);
//            }else {
//                layoutParams.width = (int) MyUtils.dpToPx(130);
//                vb.name.setMaxLines(1);
//            }
            vb.relative.setLayoutParams(layoutParams);
        }
    }
}