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 onClickFollow: (Long, Boolean) -> 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 && item.isNotify != null) {
                if (item.isFollow) {
                    binding.ivFollow.setImageResource(
                        if (item.isNotify) {
                            R.drawable.btn_following_big
                        } else {
                            R.drawable.btn_following_no_alarm_big
                        }
                    )
                } else {
                    binding.ivFollow.setImageResource(R.drawable.btn_follow_big)
                }

                binding.ivFollow.visibility = View.VISIBLE
                binding.ivFollow.setOnClickListener {
                    onClickFollow(item.userId, item.isFollow)
                }
            } else {
                binding.ivFollow.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
}
