package com.casic.br.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.casic.br.R
import com.casic.br.model.PushMessageModel

class MessageListAdapter(
    context: Context, private val dataRows: MutableList<PushMessageModel.DataModel.RowsModel>
) : RecyclerView.Adapter<MessageListAdapter.ItemViewHolder>() {

    private var layoutInflater: LayoutInflater = LayoutInflater.from(context)

    override fun getItemCount(): Int = dataRows.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ItemViewHolder(
        layoutInflater.inflate(R.layout.item_message_rv_l, parent, false)
    )

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        //绑定数据
        val rowsBean = dataRows[position]
        holder.dateView.text = rowsBean.createTime
        holder.titleView.text = rowsBean.messageTitle
        holder.contentView.text = rowsBean.messageContent
        val state = if (rowsBean.messageStatus == "0") {
            View.VISIBLE
        } else {
            View.INVISIBLE
        }
        holder.readStateView.visibility = state
        //绑定事件
        holder.deleteView.setOnClickListener {
            listener?.onDeleteClicked(position)
        }

        holder.showDetailLayout.setOnClickListener {
            listener?.onShowMoreClicked(position)
        }
    }

    inner class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var dateView: TextView = view.findViewById(R.id.dateView)
        var titleView: TextView = view.findViewById(R.id.titleView)
        var contentView: TextView = view.findViewById(R.id.contentView)
        var readStateView: TextView = view.findViewById(R.id.readStateView)

        var showDetailLayout: LinearLayout = view.findViewById(R.id.showDetailLayout)
        var deleteView: ImageView = view.findViewById(R.id.deleteView)
    }

    private var listener: OnItemClickListener? = null

    interface OnItemClickListener {
        fun onDeleteClicked(position: Int)

        fun onShowMoreClicked(position: Int)
    }

    fun setOnItemClickListener(onClickListener: OnItemClickListener?) {
        this.listener = onClickListener
    }
}