package kr.co.vividnext.sodalive.audio_content.all

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.CircleCropTransformation
import coil.transform.RoundedCornersTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.audio_content.main.GetAudioContentMainItem
import kr.co.vividnext.sodalive.databinding.ItemAudioContentNewAllBinding
import kr.co.vividnext.sodalive.extensions.dpToPx

class AudioContentNewAllAdapter(
    private val itemWidth: Int,
    private val onClickItem: (Long) -> Unit,
    private val onClickCreator: (Long) -> Unit,
) : RecyclerView.Adapter<AudioContentNewAllAdapter.ViewHolder>() {

    inner class ViewHolder(
        private val binding: ItemAudioContentNewAllBinding,
        private val onClickItem: (Long) -> Unit,
        private val onClickCreator: (Long) -> Unit
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: GetAudioContentMainItem) {
            binding.ivAudioContentCoverImage.load(item.coverImageUrl) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(RoundedCornersTransformation(2.7f.dpToPx()))

                val layoutParams = binding.ivAudioContentCoverImage
                    .layoutParams as ConstraintLayout.LayoutParams

                layoutParams.width = itemWidth
                layoutParams.height = itemWidth
                binding.ivAudioContentCoverImage.layoutParams = layoutParams
            }

            binding.ivAudioContentCreator.load(item.creatorProfileImageUrl) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(CircleCropTransformation())
            }

            binding.tvAudioContentTitle.text = item.title
            binding.tvAudioContentCreatorNickname.text = item.creatorNickname

            binding.ivAudioContentCreator.setOnClickListener { onClickCreator(item.creatorId) }
            binding.root.setOnClickListener { onClickItem(item.contentId) }
        }
    }

    private val items = mutableListOf<GetAudioContentMainItem>()

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ) = ViewHolder(
        ItemAudioContentNewAllBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        ),
        onClickItem = onClickItem,
        onClickCreator = onClickCreator
    )

    override fun getItemCount() = items.size

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

    @SuppressLint("NotifyDataSetChanged")
    fun addItems(items: List<GetAudioContentMainItem>) {
        this.items.addAll(items)
        notifyDataSetChanged()
    }

    fun clear() {
        this.items.clear()
    }
}
