package com.live.mylivewallpaper.adapter;

import android.app.Activity;
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 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.live.mylivewallpaper.App;
import com.live.mylivewallpaper.R;
import com.live.mylivewallpaper.activity.VideoActivity;
import com.live.mylivewallpaper.data.LikeData;
import com.live.mylivewallpaper.databinding.ItemMainBinding;
import com.live.mylivewallpaper.help.Common;
import com.live.mylivewallpaper.topon.AdManager;
import com.live.mylivewallpaper.topon.onActionListener;
import com.live.mylivewallpaper.viewholder.MainViewHolder;

import java.lang.ref.WeakReference;

public class LikeAdapter extends PagingDataAdapter<LikeData, MainViewHolder<ItemMainBinding>> {

    private WeakReference<Activity> contextWeakReference;
    private Activity MContext;

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

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


    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder<ItemMainBinding> holder, int position) {
        ItemMainBinding itemVb = holder.getItemVb();
        LikeData item = getItem(position);

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

        Glide.with(MContext)
                .asDrawable()
                .load(thumbStr)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(Common.dpToPx(MContext, 4))))
                .placeholder(R.mipmap.ic_launcher)
                .centerCrop()
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                        Common.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) {
                AdManager.showTopOn(MContext, new onActionListener() {
                    @Override
                    public void onAction() {
                        Intent intent = new Intent(MContext, VideoActivity.class);
                        intent.putExtra(VideoActivity.KEY_ID, item.getId());
                        intent.putExtra(VideoActivity.KEY_wallpaperType, item.getWallpapertype());
                        intent.putExtra(VideoActivity.KEY_image, item.getImage());
                        intent.putExtra(VideoActivity.KEY_describe, item.getDescription());
                        intent.putExtra(VideoActivity.KEY_thumb, item.getThumbnail());
                        MContext.startActivity(intent);
                    }
                });


            }
        });
    }


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


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

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