package com.input.board.appskey.keyboard.recyAdapter;


import static com.bumptech.glide.integration.webp.decoder.WebpDrawable.LOOP_FOREVER;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

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

import com.input.board.appskey.keyboard.R;
import com.input.board.appskey.keyboard.interf.onClickCallback;
import com.input.board.appskey.keyboard.jsonBean.ChildBean;
import com.input.board.appskey.keyboard.viewUi.ApplyActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.integration.webp.decoder.WebpDrawable;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import java.util.List;

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

    private Context mContext;
    private List<ChildBean> mList;

    private onClickCallback mCallBack;

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

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


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

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

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

    @Override
    public void onBindViewHolder(@NonNull ForYouViewHolder holder, int position) {
        ChildBean childBean = mList.get(position);
        String thumbGif = childBean.getThumbGif();
        String thumb = childBean.getThumbUrl();
        if(!thumbGif.isEmpty()){
            loadWepJif(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, ApplyActivity.class);
                intentApply.putExtra(ApplyActivity.DISPLAY_URL_KEY,childBean.getImgPath());
                intentApply.putExtra(ApplyActivity.ZIP_URL_KEY,childBean.getZipPath());
                intentApply.putExtra(ApplyActivity.NAME_KEY,childBean.getTitleName());
                intentApply.putExtra(ApplyActivity.GIF_KEY,childBean.getImgGif());
                mContext.startActivity(intentApply);
                if(mCallBack != null){
                    mCallBack.onClickCall();
                }

            }
        });


    }

    @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.item_image);

        }

    }

    @SuppressLint("CheckResult")
    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);
    }
}
