package com.free.video.wallpaper.mylist;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.paging.PagingDataAdapter;
import androidx.recyclerview.widget.DiffUtil;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.free.video.wallpaper.baseapp.MyWallpaperApp;
import com.free.video.wallpaper.R;
import com.free.video.wallpaper.baseapp.BaseViewHolder;
import com.free.video.wallpaper.databinding.ItemFavoriteBinding;
import com.free.video.wallpaper.content.FavoriteData;
import com.free.video.wallpaper.uipage.PageSetLive;
import com.free.video.wallpaper.helper.Commontool;

import java.lang.ref.WeakReference;

public class LikeAdapter extends PagingDataAdapter<FavoriteData, BaseViewHolder<ItemFavoriteBinding>> {

    private WeakReference<Activity> contextWeakReference;
    private Activity MContext;

    public LikeAdapter(Activity context) {
        super(DIFF_CALLBACK);
        contextWeakReference = new WeakReference<>(context);
        MContext = contextWeakReference.get();
    }

    @NonNull
    @Override
    public BaseViewHolder<ItemFavoriteBinding> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemFavoriteBinding inflate = ItemFavoriteBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new BaseViewHolder<>(inflate);


    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder<ItemFavoriteBinding> holder, int position) {
        ItemFavoriteBinding itemVb = holder.getItemVb();
        FavoriteData item = getItem(position);

        String thumbStr = MyWallpaperApp.getThumbStr() + item.getThumbnail();

        Glide.with(MContext)
                .asDrawable()
                .load(thumbStr)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(Commontool.dpToPx(MContext, 4))))
                .placeholder(R.drawable.im_placeholder)
                .centerCrop()
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                        Commontool.logMsg(e.getMessage());
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(itemVb.imageThumb);
        itemVb.imageThumb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MContext, PageSetLive.class);
                intent.putExtra(PageSetLive.KEY_ID, item.getId());
                intent.putExtra(PageSetLive.KEY_wallpaperType, item.getWallpapertype());
                intent.putExtra(PageSetLive.KEY_image, item.getImage());
                intent.putExtra(PageSetLive.KEY_describe, item.getDescription());
                intent.putExtra(PageSetLive.KEY_thumb, item.getThumbnail());
                MContext.startActivity(intent);



            }
        });
    }


    private static final DiffUtil.ItemCallback<FavoriteData> DIFF_CALLBACK = new DiffUtil.ItemCallback<FavoriteData>() {


        @Override
        public boolean areItemsTheSame(@NonNull FavoriteData oldItem, @NonNull FavoriteData newItem) {
            return oldItem.getId() == newItem.getId();
        }

        @Override
        public boolean areContentsTheSame(@NonNull FavoriteData oldItem, @NonNull FavoriteData newItem) {
            return oldItem.getId() == newItem.getId();
        }
    };
}
