package com.keyboard.keyboardtheme.skin.adapter;


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.keyboard.keyboardtheme.skin.R;
import com.keyboard.keyboardtheme.skin.databinding.AdaChildBinding;
import com.keyboard.keyboardtheme.skin.datamanager.localdata.Child;
import com.keyboard.keyboardtheme.skin.act.ActDownload;
import com.keyboard.keyboardtheme.skin.utils.Common;

import java.util.List;

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

    private Context mContext;
    private List<Child> mList;

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




    @NonNull
    @Override
    public ChildViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        AdaChildBinding inflate = AdaChildBinding.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()) {
            Common.INSTANCE.loadWepJif(mContext, thumbGif, holder.binding.imageView);
        } else {
            Glide.with(mContext).load(thumb)
                    .placeholder(R.drawable.image_def_placeholder)
                    .into(holder.binding.imageView);
        }


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


            }
        });


    }

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

    public static class ChildViewHolder extends RecyclerView.ViewHolder {

        private AdaChildBinding binding;


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


        }

    }


}
