package com.haircut.pranks.app.recyadapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.haircut.pranks.app.data.ParentData;
import com.haircut.pranks.app.databinding.ItemHomeBinding;
import com.haircut.pranks.app.manager.Tools;
import com.haircut.pranks.app.uiview.ActChild;

import java.util.List;


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

    private final List<ParentData> mValues;

    private Context mCon;


    public AdapterHome(Context context, List<ParentData> items) {
        mValues = items;
        this.mCon = context;
    }

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

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

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        int i = position % 5;
        holder.vb.relative.setBackground(ContextCompat.getDrawable(mCon, Tools.bgs[i]));
        ParentData parentData = mValues.get(position);
        String covert = parentData.getCovert();
        Glide.with(mCon).load(covert).into(holder.vb.image);
        holder.vb.name.setText(parentData.getName());
        holder.vb.relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mCon, ActChild.class);
                intent.putExtra(Tools.KEY_LIST_DATA, parentData);
                intent.putExtra(Tools.KEY_LIST_COLOR_POS, i);
                mCon.startActivity(intent);
            }
        });

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ItemHomeBinding vb;

        public ViewHolder(ItemHomeBinding binding) {
            super(binding.getRoot());
            vb = binding;

        }
    }
}