package com.yidian.shenghuoquan.newscontent.adapter

import android.view.View
import android.view.ViewGroup
import android.widget.TextView
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

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
    val tvContent = holder.itemView.findViewById<TextView>(R.id.tv_content)
    holder.setData(dataList[position], tvContent)
    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, tvContent: TextView) {
      tvContent.text = content
    }
  }
}
