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

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.CircleCropTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemFollowerListBinding

class UserFollowerListAdapter(
    private val onClickRegisterNotification: (Long) -> Unit,
    private val onClickUnRegisterNotification: (Long) -> Unit,
) : RecyclerView.Adapter<UserFollowerListAdapter.ViewHolder>() {

    private val items = mutableListOf<GetFollowerListResponseItem>()

    inner class ViewHolder(
        private val binding: ItemFollowerListBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: GetFollowerListResponseItem) {
            binding.tvNickname.text = item.nickname
            binding.ivProfile.load(item.profileImage) {
                transformations(CircleCropTransformation())
                placeholder(R.drawable.ic_place_holder)
                crossfade(true)
            }

            if (item.isFollow != null) {
                binding.ivNotification.visibility = View.VISIBLE
                if (item.isFollow) {
                    binding.ivNotification.setImageResource(R.drawable.btn_notification_selected)
                    binding.ivNotification.setOnClickListener {
                        onClickUnRegisterNotification(item.userId)
                        clear()
                    }
                } else {
                    binding.ivNotification.setImageResource(R.drawable.btn_notification)
                    binding.ivNotification.setOnClickListener {
                        onClickRegisterNotification(item.userId)
                        clear()
                    }
                }
            } else {
                binding.ivNotification.visibility = View.GONE
            }
        }
    }

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

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

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

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

    override fun getItemCount() = items.size
}
