package com.color.keyboard.black.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.color.keyboard.black.R;
import com.color.keyboard.black.activity.ApplyActivity;
import com.color.keyboard.black.bean.ChildData;
import com.color.keyboard.black.databinding.ItemChildBinding;
import com.color.keyboard.black.manager.Common;
import com.color.keyboard.black.myapi.OnItemApi;

import java.util.ArrayList;
import java.util.List;

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

    private Context mContext;
    private List<ChildData> mList = new ArrayList<>();

    private OnItemApi mCallBack;


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


    public void setClickAction(OnItemApi callback) {
        mCallBack = callback;
    }

    @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) {
        ChildData childData = mList.get(position);


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

        holder.binding.fragme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentApply = new Intent(mContext, ApplyActivity.class);
                intentApply.putExtra(ApplyActivity.SOURCE_KEY, childData);
//                intentApply.putExtra(ApplyActivity.DISPLAY_URL_KEY, childData.getImgPath());
//                intentApply.putExtra(ApplyActivity.ZIP_URL_KEY, childData.getZipPath());
//                intentApply.putExtra(ApplyActivity.NAME_KEY, childData.getTitleName());
//                intentApply.putExtra(ApplyActivity.GIF_KEY, childData.getImgGif());
                String intent_thumb;
                if (!thumbGif.isEmpty()) {
                    intent_thumb = thumbGif;
                } else {
                    intent_thumb = thumb;
                }
//                intentApply.putExtra(ApplyActivity.THUMB_KEY, intent_thumb);
                mContext.startActivity(intentApply);
                if (mCallBack != null) {
                    mCallBack.onItemClick();
                }

            }
        });


    }

    @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;


        }

    }


}
