package com.jld.kotilntest.room.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
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.MusicByMidBean
import com.jld.vod.utils.LogUtils


/**
 * @author ZhaoFuXin
 * @Email：18276061387@163.com
 * @description:
 * @date :2019/11/19 13:42
 */
class MusicListAdapter :
    PagingDataAdapter<MusicByMidBean, MusicListAdapter.MusicViewHolder>(DIFF_CALLBACK) {

    private var itemClickListener: IMusicItemClickListener? = null

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

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

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

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

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

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

        LayoutInflater.from(parent.context).inflate(
            R.layout.item_music_list_recyclerview, parent, false)) {
        var img = itemView.findViewById<ImageView>(R.id.aciv_item_music_pic)
        var tv = itemView.findViewById<TextView>(R.id.tv_music_title)
        val ll = itemView.findViewById<LinearLayout>(R.id.ll_item_music)

        fun bindTo(moviet: MusicByMidBean?) {
            Glide.with(itemView).load(com.jld.vod.config.Config.ApiBaseUrl+
                    "/synchronization/previewImg?imgUrl="+moviet!!.musicImgUrl)
                .skipMemoryCache(true).diskCacheStrategy(
                DiskCacheStrategy.ALL).into(img)
            tv.text = moviet?.musicName


            ll.setOnClickListener {
                 itemClickListener!!.onItemClickListener(position, moviet.cid,moviet.mid)
            }
        }
    }
    fun setOnMusicClickListener(itemClickListener: IMusicItemClickListener) {
        this.itemClickListener = itemClickListener
    }

    interface IMusicItemClickListener {
        fun onItemClickListener(
            id: Int?,
            cid: Long?,
            mid: Long?
        )
    }
}