package com.jld.kotilntest.room.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.paging.PagedListAdapter
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.jld.vod.R
import com.jld.vod.model.bean.BrowserBean


/**
 * @author ZhaoFuXin
 * @Email：18276061387@163.com
 * @description:
 * @date :2019/11/19 13:42
 */
class BrowserAdapter :
    PagingDataAdapter<BrowserBean, BrowserAdapter.LightSwitchViewHolder>(DIFF_CALLBACK) {
    private var itemClickListener: IBrowserItemClickListener? = null

    override fun onBindViewHolder(holder: LightSwitchViewHolder, position: Int) {
        val concert: BrowserBean? = getItem(position)

        // Note that "concert" is a placeholder if it'rank_mark null.
        holder.bindTo(concert)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LightSwitchViewHolder {
        return LightSwitchViewHolder(parent)
    }

    companion object {
        private val DIFF_CALLBACK = object :
            DiffUtil.ItemCallback<BrowserBean>() {
            //是否为同一个Item
            override fun areContentsTheSame(oldItem: BrowserBean, newItem: BrowserBean): Boolean {
                // Log.e("tag","areContentsTheSame"+Thread.currentThread().name+ oldItem.titleName +"  new :"+newItem.titleName)
                return oldItem == newItem
            }

            //数据内容是否发生变化
            override fun areItemsTheSame(oldItem: BrowserBean, newItem: BrowserBean): Boolean {
                // Log.e("tag","areItemsTheSame"+ oldItem.titleName +"  new :"+newItem.titleName)
                return oldItem == newItem
            }
        }
    }

    inner class LightSwitchViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(

        LayoutInflater.from(parent.context).inflate(
            R.layout.item_browser_recyclerview, parent, false)) {
        var img = itemView.findViewById<AppCompatImageView>(R.id.aciv_item_browser_pic)
        var tv = itemView.findViewById<TextView>(R.id.tv_browser_title)
        var ll = itemView.findViewById<LinearLayout>(R.id.ll_browser)


        fun bindTo(browserBean: BrowserBean?) {
            Glide.with(itemView).load(com.jld.vod.config.Config.ApiBaseUrl+"/synchronization/previewImg?imgUrl="
                    +browserBean!!.imgUrl)
                .skipMemoryCache(true).diskCacheStrategy(
                DiskCacheStrategy.ALL).into(img)
            tv.text = browserBean?.nName
            ll.setOnClickListener {
                itemClickListener!!.onItemClickListener(browserBean!!.nUrl)
            }
        }
    }

    fun setOnBrowserClickListener(itemClickListener: IBrowserItemClickListener) {
        this.itemClickListener = itemClickListener
    }

    interface IBrowserItemClickListener {
        fun onItemClickListener(url: String)
    }
}