package com.app.input.breeze.board.listutils;


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.app.input.breeze.board.R;
import com.app.input.breeze.board.bean.KeyboardTheme;
import com.app.input.breeze.board.databinding.ThemeGridItemBinding;
import com.app.input.breeze.board.uiactivity.ThemeDetailActivity;
import com.app.input.breeze.board.utils.AppUtils;
import com.bumptech.glide.Glide;

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

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

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


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


    @NonNull
    @Override
    public ChildViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ThemeGridItemBinding inflate = ThemeGridItemBinding.inflate(LayoutInflater.from(parent.getContext()));


        return new ChildViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ChildViewHolder holder, int position) {
        KeyboardTheme KeyboardTheme = mList.get(position);


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

        // 设置标题
        holder.binding.titleText.setText(KeyboardTheme.getTitleName());

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


            }
        });


    }

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

    public static class ChildViewHolder extends RecyclerView.ViewHolder {

        private ThemeGridItemBinding binding;


        public ChildViewHolder(@NonNull ThemeGridItemBinding 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);
//    }
}
