package kr.co.vividnext.sodalive.mypage.block

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.CircleCropTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemBlockedMemberListBinding

class BlockMemberAdapter(
    private val blockMember: (Long) -> Unit,
    private val unBlockMember: (Long) -> Unit
) : RecyclerView.Adapter<BlockMemberAdapter.ViewHolder>() {

    private val items = mutableListOf<GetBlockedMemberListItem>()

    inner class ViewHolder(
        private val context: Context,
        private val binding: ItemBlockedMemberListBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        @SuppressLint("NotifyDataSetChanged")
        fun bind(item: GetBlockedMemberListItem) {
            binding.tvNickname.text = item.nickname
            binding.ivProfile.load(item.profileImageUrl) {
                transformations(CircleCropTransformation())
                placeholder(R.drawable.bg_placeholder)
                crossfade(true)
            }

            binding.tvBlock.visibility = View.VISIBLE
            if (item.isBlocked) {
                binding.tvBlock.text = "차단해제"
                binding.tvBlock.background = ContextCompat.getDrawable(
                    context,
                    R.drawable.bg_round_corner_13_3_333bb9f1_3bb9f1
                )
                binding.tvBlock.setOnClickListener {
                    val index = items.indexOf(item)
                    val copyItem = item.copy(isBlocked = false)
                    items.add(index, copyItem)
                    items.removeAt(index + 1)
                    notifyDataSetChanged()
                    unBlockMember(item.memberId)
                }
            } else {
                binding.tvBlock.text = "차단"
                binding.tvBlock.background = ContextCompat.getDrawable(
                    context,
                    R.drawable.bg_round_corner_13_3_transparent_3bb9f1
                )
                binding.tvBlock.setOnClickListener {
                    val index = items.indexOf(item)
                    val copyItem = item.copy(isBlocked = true)
                    items.add(index, copyItem)
                    items.removeAt(index + 1)
                    notifyDataSetChanged()
                    blockMember(item.memberId)
                }
            }
        }
    }

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

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

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

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

    override fun getItemCount() = items.size
}
