package com.cartoon.live.wallpaper.myadapter;

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.cartoon.live.wallpaper.MyApp;
import com.cartoon.live.wallpaper.R;
import com.cartoon.live.wallpaper.ui.PreviewActivity;
import com.cartoon.live.wallpaper.mydata.ResultData;
import com.cartoon.live.wallpaper.databinding.ItemMainBinding;
import com.cartoon.live.wallpaper.utils.Com;
import com.cartoon.live.wallpaper.parent.BaseViewHolder;

import java.lang.ref.WeakReference;

public class AdapterMain extends PagingDataAdapter<ResultData, BaseViewHolder<ItemMainBinding>> {

    private WeakReference<Activity> contextWeakReference;
    private Activity MContext;

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

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


    }

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

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

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


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


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

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