package com.wall.photography.wallpaper.adapter

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
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.wall.photography.wallpaper.MyApp
import com.wall.photography.wallpaper.R
import com.wall.photography.wallpaper.data.Bean
import com.wall.photography.wallpaper.databinding.ItemCategoryBinding
import com.wall.photography.wallpaper.databinding.ItemHomeBinding
import com.wall.photography.wallpaper.databinding.ItemLoveBinding
import com.wall.photography.wallpaper.manager.DealData
import com.wall.photography.wallpaper.manager.Other

class AdapterLoves(private var mContext: Context, private var goList:(item:Bean)->Unit) : RecyclerView.Adapter<AdapterLoves.VH>() {
    private var data: List<Bean> = emptyList()

    inner class VH(var vb: ItemLoveBinding) : RecyclerView.ViewHolder(vb.root)

    fun updateData(newData: List<Bean>) {
        val diffCallback = DiffCallback(data, newData)
        val calculateDiff = DiffUtil.calculateDiff(diffCallback)
        calculateDiff.dispatchUpdatesTo(this)
        data = newData
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val inflate = ItemLoveBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return VH(inflate)
    }

    override fun getItemCount(): Int = data.size

    override fun onBindViewHolder(holder: VH, position: Int) {
        data[position].run {
            holder.vb.cardView.setOnClickListener {
                goList.invoke(this)
            }
            Log.d(MyApp.TAG, "--------LoveAdapter-----urls.regular=${urls.regular}")
            Glide.with(MyApp.application)
                .asDrawable()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .load(urls.regular)
                .thumbnail(0.2f)
                .error(R.drawable.placeholder)
                .placeholder(R.drawable.placeholder)
                .listener(object : RequestListener<Drawable> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Drawable>,
                        isFirstResource: Boolean
                    ): Boolean {
                        Log.d(MyApp.TAG, "----------LoveAdapter-onLoadFailed----urls.thumb=${urls.thumb}  e=${e?.message}")
                        return false
                    }

                    override fun onResourceReady(
                        resource: Drawable,
                        model: Any,
                        target: Target<Drawable>?,
                        dataSource: DataSource,
                        isFirstResource: Boolean
                    ): Boolean {


                        return false
                    }

                })
                .into(holder.vb.loveWallpaper)
        }


    }

    inner class DiffCallback(var old: List<Bean>, var new: List<Bean>) : DiffUtil.Callback() {
        override fun getOldListSize(): Int = old.size

        override fun getNewListSize(): Int = new.size

        override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            val thumb = old[oldItemPosition].urls.thumb
            val newThumb = new[newItemPosition].urls.thumb
            return thumb == newThumb
        }

        override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            val love = old[oldItemPosition].love
            val newLove = new[newItemPosition].love
            return love == newLove
        }

    }
}