package com.hd.live.wallpapers.alladapter;

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.hd.live.wallpapers.LiveApp;
import com.hd.live.wallpapers.R;
import com.hd.live.wallpapers.databinding.ItemLikeBinding;
import com.hd.live.wallpapers.bean.FavoriteData;
import com.hd.live.wallpapers.actfrag.ActPreView;
import com.hd.live.wallpapers.others.ComUtils;
import com.hd.live.wallpapers.base.BaseViewHolder;

import java.lang.ref.WeakReference;

public class AdaLike extends PagingDataAdapter<FavoriteData, BaseViewHolder<ItemLikeBinding>> {

    private WeakReference<Activity> contextWeakReference;
    private Activity MContext;

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

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


    }

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

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

        Glide.with(MContext)
                .asDrawable()
                .load(thumbStr)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(ComUtils.dpToPx(MContext, 4))))
                .placeholder(R.drawable.png_place_hlder)
                .centerCrop()
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                        ComUtils.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, ActPreView.class);
                intent.putExtra(ActPreView.KEY_ID, item.getId());
                intent.putExtra(ActPreView.KEY_wallpaperType, item.getWallpapertype());
                intent.putExtra(ActPreView.KEY_image, item.getImage());
                intent.putExtra(ActPreView.KEY_describe, item.getDescription());
                intent.putExtra(ActPreView.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();
        }
    };
}
