package kr.co.vividnext.sodalive.live.room.chat

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import kr.co.vividnext.sodalive.databinding.ItemLiveRoomChatBinding
import kr.co.vividnext.sodalive.databinding.ItemLiveRoomDonationStatusChatBinding
import kr.co.vividnext.sodalive.databinding.ItemLiveRoomJoinChatBinding

class LiveRoomChatAdapter(
    private val onClickProfile: (Long) -> Unit
) : RecyclerView.Adapter<LiveRoomChatViewHolder>() {

    val items = mutableListOf<LiveRoomChat>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LiveRoomChatViewHolder {
        when (viewType) {
            LiveRoomChatType.DONATION_STATUS.ordinal -> {
                return LiveRoomDonationStatusChatViewHolder(
                    parent.context,
                    ItemLiveRoomDonationStatusChatBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            LiveRoomChatType.JOIN.ordinal -> {
                return LiveRoomJoinChatViewHolder(
                    parent.context,
                    ItemLiveRoomJoinChatBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            else -> {
                return LiveRoomNormalChatViewHolder(
                    parent.context,
                    ItemLiveRoomChatBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }
        }
    }

    override fun onBindViewHolder(holder: LiveRoomChatViewHolder, position: Int) {
        holder.bind(items[position], onClickProfile)
    }

    override fun getItemCount() = items.count()

    override fun getItemViewType(position: Int): Int {
        return items[position].type.ordinal
    }
}

abstract class LiveRoomChatViewHolder(binding: ViewBinding) :
    RecyclerView.ViewHolder(binding.root) {
    abstract fun bind(chat: LiveRoomChat, onClickProfile: ((Long) -> Unit)? = null)
}

class LiveRoomNormalChatViewHolder(
    private val context: Context,
    private val binding: ItemLiveRoomChatBinding
) : LiveRoomChatViewHolder(binding) {
    override fun bind(
        chat: LiveRoomChat,
        onClickProfile: ((Long) -> Unit)?
    ) = chat.bind(context, binding, onClickProfile)
}

class LiveRoomDonationStatusChatViewHolder(
    private val context: Context,
    private val binding: ItemLiveRoomDonationStatusChatBinding
) : LiveRoomChatViewHolder(binding) {
    override fun bind(
        chat: LiveRoomChat,
        onClickProfile: ((Long) -> Unit)?
    ) = chat.bind(context, binding)
}

class LiveRoomJoinChatViewHolder(
    private val context: Context,
    private val binding: ItemLiveRoomJoinChatBinding
) : LiveRoomChatViewHolder(binding) {
    override fun bind(
        chat: LiveRoomChat,
        onClickProfile: ((Long) -> Unit)?
    ) = chat.bind(context, binding)
}
