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 android.widget.ImageView;

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

import com.keyboard.keyboardtheme.skin.R;
import com.keyboard.keyboardtheme.skin.datamanager.localdata.Child;
import com.keyboard.keyboardtheme.skin.utils.OnClick;
import com.keyboard.keyboardtheme.skin.act.ActDownload;
import com.bumptech.glide.Glide;
import com.keyboard.keyboardtheme.skin.utils.Common;

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

public class AdaList extends RecyclerView.Adapter<AdaList.ForYouViewHolder> {

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

    private OnClick mCallBack;

    public AdaList(Context context) {
        mContext = context;
    }

    public void setForYouList(List<Child> list) {
        this.mList = list;
        notifyDataSetChanged();
    }


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

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

        View view = LayoutInflater.from(mContext).inflate(R.layout.ada_list, parent, false);
        return new ForYouViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ForYouViewHolder 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.itemImg);
        } else {
            Glide.with(mContext)
                    .load(thumb)
                    .placeholder(R.drawable.image_def_placeholder)
                    .into(holder.itemImg);
        }

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

            }
        });


    }

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

    public static class ForYouViewHolder extends RecyclerView.ViewHolder {

        private CardView cardView;
        private ImageView itemImg;

        public ForYouViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.card_view);
            itemImg = itemView.findViewById(R.id.imPreview);

        }

    }

//    private void loadWepJif(String webpGifUrl,ImageView view){
//        Glide.with(mContext).load(webpGifUrl).addListener(new RequestListener<Drawable>() {
//            @Override
//            public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
//                return false;
//            }
//
//            @Override
//            public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
//               if(resource instanceof WebpDrawable){
//                   WebpDrawable webpDrawable = (WebpDrawable) resource;
//                   webpDrawable.setLoopCount(LOOP_FOREVER);
//               }
//
//                return false;
//            }
//        }).into(view);
//    }
}
