package com.yidian.yac.userinfo.adapter

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.yidian.common.utils.ViewHolderUtil.Companion.createItemView
import com.yidian.yac.userinfo.R
import com.yidian.yac.userinfo.bean.ImageItemBean
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.view_pager_item.*

class CommenViewPagerAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private val dataList: ArrayList<ImageItemBean> by lazy { ArrayList<ImageItemBean>() }
    private lateinit var mOnItemClickListener: OnItemClickListener<ImageItemBean>

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ViewHolder(createItemView(parent, R.layout.view_pager_item))
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        holder as ViewHolder
        holder.setData(dataList[position])
        holder.photo_view.setOnClickListener{
            mOnItemClickListener.onClick(it, position, dataList[position])
        }
    }



    override fun getItemCount(): Int {
        return dataList.size
    }

    fun update(newData: ArrayList<ImageItemBean>){
        newData.let {
            dataList.clear()
            dataList.addAll(it)
            notifyDataSetChanged()
        }
    }

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener<ImageItemBean>) {
        mOnItemClickListener = onItemClickListener
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), LayoutContainer {
        override val containerView: View
            get() = itemView

        fun setData(imageItemBean: ImageItemBean) {
            photo_view.setImageResource(imageItemBean.imageId)
        }
    }
}
