package com.jld.kotilntest.room.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.paging.PagedListAdapter
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.jld.vod.R
import com.jld.vod.model.bean.CategorySonAllBean


/**
 * @author ZhaoFuXin
 * @Email：18276061387@163.com
 * @description:
 * @date :2019/11/19 13:42
 */
class CategorySonAllAdapter :
    PagingDataAdapter<CategorySonAllBean, CategorySonAllAdapter.LightSwitchViewHolder>(DIFF_CALLBACK) {
    private var itemClickListener: ICategorySonItemClickListener? = null
    private var oldselectpos:Int = -5
    override fun onBindViewHolder(holder: LightSwitchViewHolder, position: Int) {
        val concert: CategorySonAllBean? = 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<CategorySonAllBean>() {
            //是否为同一个Item
            override fun areContentsTheSame(oldItem: CategorySonAllBean, newItem: CategorySonAllBean): Boolean {
                // Log.e("tag","areContentsTheSame"+Thread.currentThread().name+ oldItem.titleName +"  new :"+newItem.titleName)
                return oldItem == newItem
            }

            //数据内容是否发生变化
            override fun areItemsTheSame(oldItem: CategorySonAllBean, newItem: CategorySonAllBean): 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_categoryfatherall_recyclerview, parent, false)) {
        var tv = itemView.findViewById<TextView>(R.id.tv_title)
        val ll_categoryfather = itemView.findViewById<LinearLayout>(R.id.ll_categoryfather_movie_item)

        fun bindTo(categoryfatherall: CategorySonAllBean?) {
            tv.setSelected(true)
            tv.text = categoryfatherall?.cName

            if (position == oldselectpos)
            {
                tv.setBackgroundColor(tv.resources.getColor(R.color.brown_974))
            }else{
                tv.setBackgroundColor(tv.resources.getColor(R.color.white_00))
            }

            ll_categoryfather.setOnClickListener {

                notifyItemChanged(oldselectpos);//刷新以前item 回置状态
                oldselectpos = position
                notifyItemChanged(position);//刷新当前点击item
                categoryfatherall!!.mid?.let { it1 -> itemClickListener!!.onItemClickListener(categoryfatherall.cid,it1) }
            }
        }
    }

    fun setOnCategorySonClickListener(itemClickListener: ICategorySonItemClickListener) {
        this.itemClickListener = itemClickListener
    }

    interface ICategorySonItemClickListener {
        fun onItemClickListener(
            cid: Long,
            mid: Long
        )
    }
}