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.Config
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.MovieByMidBean
import com.jld.vod.utils.LogUtils


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

    private var itemClickListener: IMovieItemClickListener? = null


    override fun onBindViewHolder(holder: LightSwitchViewHolder, position: Int) {
        val concert: MovieByMidBean? = 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<MovieByMidBean>() {
            //是否为同一个Item
            override fun areContentsTheSame(oldItem: MovieByMidBean, newItem: MovieByMidBean): Boolean {
                // Log.e("tag","areContentsTheSame"+Thread.currentThread().name+ oldItem.titleName +"  new :"+newItem.titleName)
                return oldItem == newItem
            }

            //数据内容是否发生变化
            override fun areItemsTheSame(oldItem: MovieByMidBean, newItem: MovieByMidBean): 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_movie_list_recyclerview, parent, false)) {
        var img = itemView.findViewById<AppCompatImageView>(R.id.aciv_item_device_pic)
        var tv = itemView.findViewById<TextView>(R.id.tv_title)
        val ll = itemView.findViewById<LinearLayout>(R.id.ll_movie_item)

        //数据绑定
        fun bindTo(moviet: MovieByMidBean?) {
           // LogUtils.logD("http://192.168.0.107/Img/"+moviet!!.movieImgUrl)
            Glide.with(itemView).load(com.jld.vod.config.Config.ApiBaseUrl+
                    "/synchronization/previewImg?imgUrl="+moviet!!.movieImgUrl)
                .skipMemoryCache(true).diskCacheStrategy(
                DiskCacheStrategy.ALL).into(img)

            tv.text = moviet!!.movieName

            ll.setOnClickListener {
                itemClickListener!!.onItemClickListener(moviet)
            }
        }
    }

    fun setOnMovieClickListener(itemClickListener: IMovieItemClickListener) {
        this.itemClickListener = itemClickListener
    }

    interface IMovieItemClickListener {
        fun onItemClickListener(
            movie: MovieByMidBean
        )
    }
}