package com.app.wave.keyboard.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.app.wave.keyboard.R;
import com.app.wave.keyboard.bean.DetailsBean;
import com.app.wave.keyboard.callback.OIClickCallback;
import com.app.wave.keyboard.ui.KeyboardPreviewActivity;
import com.bumptech.glide.Glide;
import com.app.wave.keyboard.utils.Comutils;

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

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

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

    private OIClickCallback mCallBack;

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

    public void setForYouList(List<DetailsBean> list) {
        this.mList = list;
        notifyDataSetChanged();
    }
    public void setClickAction(OIClickCallback callback) {
        mCallBack = callback;
    }

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

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

    @Override
    public void onBindViewHolder(@NonNull ForYouViewHolder holder, int position) {
        DetailsBean detailsBean = mList.get(position);
        String thumbGif = detailsBean.getThumbGif();
        String thumb = detailsBean.getThumbUrl();
        if (!thumbGif.isEmpty()) {
            Comutils.INSTANCE.loadWepJif(mContext, thumbGif, holder.itemImg);
        } else {
            Glide.with(mContext).load(thumb).into(holder.itemImg);
        }

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

            }
        });


    }

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