package com.yidian.shenghuoquan.newscontent.adapter

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.yidian.common.cardutils.adapter.OnItemClickListener
import com.yidian.common.utils.ViewHolderUtil.Companion.createItemView
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.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) {
            Glide.with(itemView.context).load(imageItemBean.imageUrl).into(photo_view)
        }
    }
}
