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 android.widget.ImageView
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
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.request.RequestListener
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.ItemHomeBinding
import com.wall.photography.wallpaper.manager.Other


class AdapterHomePaing(
    private var isLove: Boolean = false,
    private var mContext: Context,
    var clickAction: (data: Bean) -> Unit
) : PagingDataAdapter<Bean, AdapterHomePaing.VH>(diff) {
//    private var data: List<Bean> = emptyList()

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


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



    override fun onViewRecycled(holder: VH) {
        super.onViewRecycled(holder)
        Glide.with(MyApp.application).clear(holder.vb.wallpaper)
        Log.d(MyApp.TAG, "----------onViewRecycled")

    }

    override fun onBindViewHolder(holder: VH, position: Int) {
        Log.d(MyApp.TAG, "----------onBindViewHolder----  position=${position}")
        val item = getItem(position)
        item?.run {
            var curheight: Int
            if (!isLove) {
                if (position <= 1) {
                    curheight = Other.dpToPx(200, mContext)
                } else if (position <= 3) {
                    curheight = Other.dpToPx(120, mContext)
                } else if (position % 3 == 0) {
                    curheight = Other.dpToPx(240, mContext)
                } else {
                    curheight = Other.dpToPx(200, mContext)
                }
                holder.vb.wallpaper.run {
                    layoutParams = layoutParams.apply {
                        height = curheight
                        width = RecyclerView.LayoutParams.MATCH_PARENT
                    }
                }
            }
            Log.d(
                MyApp.TAG,
                "----------onLoad-----  urls.small=${urls.small}"
            )
            Glide.with(MyApp.application)
                .asDrawable()
                .load(urls.small)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .thumbnail(0.2f)
                .error(R.drawable.placeholder)
                .skipMemoryCache(true)
                .centerCrop()
                .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,
                            "----------onLoadFailed-----urls.thumb=${urls.regular}  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.wallpaper)

            holder.vb.cardView.setOnClickListener {
                clickAction.invoke(this)
            }
        }


    }

    companion object {
        private val diff = object : DiffUtil.ItemCallback<Bean>() {
            override fun areItemsTheSame(oldItem: Bean, newItem: Bean): Boolean {

                return oldItem.id == newItem.id
            }

            override fun areContentsTheSame(oldItem: Bean, newItem: Bean): Boolean {

                return oldItem.love == newItem.love
            }
        }
    }


}