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

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.CircleCropTransformation
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
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
import kr.co.vividnext.sodalive.extensions.moneyFormat

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 context: Context,
        private val binding: ItemAudioContentNewAllBinding,
        private val onClickItem: (Long) -> Unit,
        private val onClickCreator: (Long) -> Unit
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: GetAudioContentMainItem) {
            Glide
                .with(context)
                .load(item.coverImageUrl)
                .apply(
                    RequestOptions().transform(
                        CenterCrop(),
                        RoundedCorners(8)
                    )
                )
                .into(binding.ivAudioContentCoverImage)

            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

            if (item.price > 0) {
                binding.ivCan.visibility = View.VISIBLE
                binding.tvCan.text = item.price.moneyFormat()
            } else {
                binding.ivCan.visibility = View.GONE
                binding.tvCan.text = "무료"
            }

            binding.tvTime.text = item.duration

            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(
        parent.context,
        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()
    }
}
