package com.rich.keyboard.skin.listadapter;


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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.rich.keyboard.skin.R;
import com.rich.keyboard.skin.localdata.Child;
import com.rich.keyboard.skin.uiactivity.ApplyUi;
import com.rich.keyboard.skin.databinding.ItemChildBinding;
import com.rich.keyboard.skin.utils.MyCommon;

import java.util.List;

public class MainChildList extends RecyclerView.Adapter<MainChildList.ChildViewHolder> {

    private Context mContext;
    private List<Child> mList;

    public MainChildList(Context context, List<Child> list) {
        mContext = context;
        this.mList = list;
    }




    @NonNull
    @Override
    public ChildViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemChildBinding inflate = ItemChildBinding.inflate(LayoutInflater.from(parent.getContext()));


        return new ChildViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ChildViewHolder holder, int position) {
        Child child = mList.get(position);


        String thumbGif = child.getThumbGif();
        String thumb = child.getThumbUrl();
        if (!thumbGif.isEmpty()) {
            MyCommon.INSTANCE.loadWepJif(mContext, thumbGif, holder.binding.imageView);
        } else {
            Glide.with(mContext).load(thumb)
                    .placeholder(R.drawable.def_placeholder)
                    .into(holder.binding.imageView);
        }


        holder.binding.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentApply = new Intent(mContext, ApplyUi.class);
                intentApply.putExtra(ApplyUi.SOURCE_KEY, child);
                mContext.startActivity(intentApply);


            }
        });


    }

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

    public static class ChildViewHolder extends RecyclerView.ViewHolder {

        private ItemChildBinding binding;


        public ChildViewHolder(@NonNull ItemChildBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;


        }

    }


}
