package com.adealink.weparty.profile.search.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.LayoutProfileSearchItemBinding
import com.adealink.weparty.profile.search.data.SearchResultItemData

class SearchItemViewBinder(val listener: OnResultClickListener) :
    ItemViewBinder<SearchResultItemData, BindingViewHolder<LayoutProfileSearchItemBinding>>() {

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: BindingViewHolder<LayoutProfileSearchItemBinding>,
        item: SearchResultItemData,
    ) {
        holder.binding.root.onClick {
            listener.goProfile(item.data.uid)
        }
        holder.binding.ivAvatar.setImageUrl(item.data.avatar)
        holder.binding.tvName.text = item.data.nickName
        holder.binding.vGender.setGender(item.data.gender)
        holder.binding.vGender.setAge(item.data.age)
        holder.binding.tvId.text = "ID ${item.data.uid}"
        holder.binding.tvFans.text =
            getCompatString(
                R.string.profile_search_user_fans,
                (item.data.followCount ?: 0).toString()
            )
        if (item.data.follow == true) {
            holder.binding.btnFollow.setBackgroundResource(R.drawable.profile_followed_button_bg)
            holder.binding.btnFollow.onClick {
                listener.unFollow(item.data.uid)
            }
        } else {
            holder.binding.btnFollow.setBackgroundResource(R.drawable.profile_follow_btn_bg)
            holder.binding.btnFollow.onClick {
                listener.follow(item.data.uid)
            }
        }
    }

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup,
    ): BindingViewHolder<LayoutProfileSearchItemBinding> {
        return BindingViewHolder(LayoutProfileSearchItemBinding.inflate(inflater, parent, false))
    }

    interface OnResultClickListener {
        fun goProfile(uid: String)

        fun follow(uid: String)

        fun unFollow(uid: String)

    }

}