package kr.co.vividnext.sodalive.explorer.profile.creator_community

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil.transform.BlurTransformation
import coil.transform.CircleCropTransformation
import coil.transform.RoundedCornersTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemCreatorCommunityBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.extensions.loadUrl

class CreatorCommunityAdapter(
    private val onClickItem: (Long) -> Unit
) : RecyclerView.Adapter<CreatorCommunityAdapter.ViewHolder>() {

    val items = mutableListOf<GetCommunityPostListResponse>()

    inner class ViewHolder(
        private val context: Context,
        private val binding: ItemCreatorCommunityBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: GetCommunityPostListResponse) {
            binding.ivCreatorProfile.loadUrl(item.creatorProfileUrl) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(CircleCropTransformation())
            }
            binding.tvCreatorNickname.text = item.creatorNickname
            binding.tvDate.text = item.date

            binding.tvContent.text = item.content
            binding.ivPostImage.loadUrl(item.imageUrl) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(RoundedCornersTransformation(4.7f.dpToPx()))

                if (!item.existOrdered && item.price > 0) {
                    transformations(
                        BlurTransformation(
                            context,
                            25f,
                            2.5f
                        )
                    )
                }
            }

            binding.tvLikeCount.text = "${item.likeCount}"
            binding.tvCommentCount.text = "${item.commentCount}"

            binding.root.setOnClickListener { onClickItem(item.creatorId) }
        }
    }

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

    override fun getItemCount() = items.size

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