package com.keyboard.skinning.cool.adpter;


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.skinning.cool.activity.ApplySkinsActivity;
import com.keyboard.skinning.cool.data.ChildData;
import com.keyboard.skinning.cool.listener.ItemClickListener;
import com.keyboard.skinning.cool.databinding.ItemChildBinding;
import com.keyboard.skinning.cool.tools.Helper;

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

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

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

    private ItemClickListener mCallBack;


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


    public void setClickAction(ItemClickListener 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()) {
            Helper.INSTANCE.loadWepJif(mContext, thumbGif, holder.binding.imageView);
        } else {
            Glide.with(mContext).load(thumb)
//                    .apply(Helper.INSTANCE.getTransform())
                    .into(holder.binding.imageView);
        }

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

            }
        });


    }

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


        }

    }

//    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);
//    }
}
