package kr.co.vividnext.sodalive.audio_content.comment

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import coil.load
import coil.transform.CircleCropTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemAudioContentCommentBinding
import kr.co.vividnext.sodalive.databinding.ItemAudioContentCommentReplyBinding

class AudioContentCommentReplyAdapter :
    RecyclerView.Adapter<AudioContentCommentReplyViewHolder>() {

    var items = mutableSetOf<GetAudioContentCommentListItem>()

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): AudioContentCommentReplyViewHolder {
        return if (viewType == 0) {
            AudioContentCommentReplyHeaderViewHolder(
                ItemAudioContentCommentBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        } else {
            AudioContentCommentReplyItemViewHolder(
                ItemAudioContentCommentReplyBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        }
    }

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

    override fun getItemCount() = items.size

    override fun getItemViewType(position: Int): Int {
        return position
    }
}

abstract class AudioContentCommentReplyViewHolder(
    binding: ViewBinding
) : RecyclerView.ViewHolder(binding.root) {
    abstract fun bind(item: GetAudioContentCommentListItem)
}

class AudioContentCommentReplyHeaderViewHolder(
    private val binding: ItemAudioContentCommentBinding
) : AudioContentCommentReplyViewHolder(binding) {

    override fun bind(item: GetAudioContentCommentListItem) {
        binding.ivCommentProfile.load(item.profileUrl) {
            crossfade(true)
            placeholder(R.drawable.ic_place_holder)
            transformations(CircleCropTransformation())
        }

        binding.tvComment.text = item.comment
        binding.tvCommentDate.text = item.date
        binding.tvCommentNickname.text = item.nickname

        binding.tvWriteReply.visibility = View.GONE
    }
}

class AudioContentCommentReplyItemViewHolder(
    private val binding: ItemAudioContentCommentReplyBinding
) : AudioContentCommentReplyViewHolder(binding) {

    override fun bind(item: GetAudioContentCommentListItem) {
        binding.ivCommentProfile.load(item.profileUrl) {
            crossfade(true)
            placeholder(R.drawable.ic_place_holder)
            transformations(CircleCropTransformation())
        }

        binding.tvComment.text = item.comment
        binding.tvCommentDate.text = item.date
        binding.tvCommentNickname.text = item.nickname
    }
}
