package com.hd.live.wall.wallpapers.adapter;

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.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.hd.live.wall.wallpapers.LiveApp;
import com.hd.live.wall.wallpapers.R;
import com.hd.live.wall.wallpapers.act.ActivitySetWallpaper;
import com.hd.live.wall.wallpapers.base.BaseVH;
import com.hd.live.wall.wallpapers.databinding.AdaLikeBinding;
import com.hd.live.wall.wallpapers.dbData.Collection;
import com.hd.live.wall.wallpapers.utils.helper;

import java.lang.ref.WeakReference;

public class AdaLike extends PagingDataAdapter<Collection, BaseVH<AdaLikeBinding>> {

    private WeakReference<Activity> contextWeakReference;
    private Activity MContext;

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

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


    }

    @Override
    public void onBindViewHolder(@NonNull BaseVH<AdaLikeBinding> holder, int position) {
        AdaLikeBinding itemVb = holder.getItemVb();
        Collection item = getItem(position);

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

        Glide.with(MContext)
                .asDrawable()
                .load(thumbStr)
                .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) {
                        helper.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, ActivitySetWallpaper.class);
                intent.putExtra(ActivitySetWallpaper.KEY_ID, item.getId());
                intent.putExtra(ActivitySetWallpaper.KEY_wallpaperType, item.getWallpapertype());
                intent.putExtra(ActivitySetWallpaper.KEY_image, item.getImage());
                intent.putExtra(ActivitySetWallpaper.KEY_describe, item.getDescription());
                intent.putExtra(ActivitySetWallpaper.KEY_thumb, item.getThumbnail());
                MContext.startActivity(intent);



            }
        });
    }


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


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

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