package com.jld.vod.view.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.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.jld.vod.R
import com.jld.vod.model.bean.SettingBean

/**
 *设置界面适配器
 *
 */
class SettingAdapter: PagedListAdapter<SettingBean, SettingAdapter.LightSwitchViewHolder>(DIFF_CALLBACK){

    //事件监听
    private var itemClickListener: IMovieItemClickListener? = null

    //适配内容
    inner class LightSwitchViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(
        LayoutInflater.from(parent.context).inflate(
            R.layout.item_setting_list, parent, false)) {
                //数据绑定
                fun bindTo(setting: SettingBean?) {
                    var img = itemView.findViewById<AppCompatImageView>(R.id.app_setting_img)
                    var tv = itemView.findViewById<TextView>(R.id.tv_title)
                    val ll = itemView.findViewById<LinearLayout>(R.id.ll_setting_item)

                    tv.text = setting!!.name//名称
                    ll.setOnClickListener {
                        //设置监听事件
                        itemClickListener!!.onItemClickListener(setting.sid)
                    }
                }
    }


    companion object {
        private val DIFF_CALLBACK = object :
            DiffUtil.ItemCallback<SettingBean>() {
            //是否为同一个Item
            override fun areContentsTheSame(oldItem: SettingBean, newItem: SettingBean): Boolean {
                return oldItem == newItem
            }

            //数据内容是否发生变化
            override fun areItemsTheSame(oldItem: SettingBean, newItem: SettingBean): Boolean {
                return oldItem == newItem
            }
        }
    }


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

        return LightSwitchViewHolder(parent)
    }




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

        holder.bindTo(concert)
    }

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

    interface IMovieItemClickListener {
        fun onItemClickListener(
            id: Int?
        )
    }

}
