package com.yidian.shenghuoquan.newscontent.adapter

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.yidian.common.cardutils.adapter.OnItemClickListener
import com.yidian.common.utils.ViewHolderUtil.Companion.createItemView
import com.yidian.shenghuoquan.newscontent.R
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.item_feedback_detail.*

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

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

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

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

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

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

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

        fun setData(content: String) {
            tv_content.text = content
        }
    }
}
