package com.adealink.weparty.profile.relation.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.aab.util.getCompatColor
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.module.profile.data.FollowStatus
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.ItemRelationshipFollowBinding
import com.adealink.weparty.profile.relation.data.FollowItemData
import com.adealink.weparty.R as APP_R

class FollowItemViewBinder(val listener: OnFollowItemClick) :
    ItemViewBinder<FollowItemData, BindingViewHolder<ItemRelationshipFollowBinding>>() {

    override fun onBindViewHolder(
        holder: BindingViewHolder<ItemRelationshipFollowBinding>,
        item: FollowItemData,
    ) {
        holder.binding.root.onClick {
            listener.goProfile(item.data.uid)
        }
        holder.binding.ivAvatar.setImageUrl(item.data.avatar)
        holder.binding.tvUserName.text = item.data.nickName
        holder.binding.vGender.setGender(item.data.gender)
        holder.binding.vGender.setAge(item.data.age)

        if (item.data.uid == ProfileModule.getMyUid()) {
            holder.binding.btnFollow.gone()
            holder.binding.btnEachFollow.gone()
        } else {
            when (FollowStatus.map(item.data.followStatus)) {
                FollowStatus.NONE -> {
                    holder.binding.btnFollow.show()
                    holder.binding.btnFollow.setBackgroundResource(R.drawable.profile_follow_button_bg)
                    holder.binding.btnFollow.text = getCompatString(APP_R.string.common_follow)
                    holder.binding.btnFollow.setTextColor(getCompatColor(APP_R.color.white))
                    holder.binding.btnFollow.onClick {
                        listener.follow(item, holder.layoutPosition)
                    }
                    holder.binding.btnEachFollow.gone()
                }

                FollowStatus.FANS -> {
                    //对方关注了我, 是否互关
                    holder.binding.btnFollow.gone()
                    holder.binding.btnEachFollow.show()
                    holder.binding.btnEachFollow.setImageResource(R.drawable.profile_each_follow_button_ic)
                    holder.binding.btnEachFollow.onClick {
                        listener.follow(item, holder.layoutPosition)
                    }
                }

                FollowStatus.EACH_FOLLOW -> {
                    //对方关注了我, 是否取消互关
                    holder.binding.btnFollow.gone()
                    holder.binding.btnEachFollow.show()
                    holder.binding.btnEachFollow.setImageResource(R.drawable.profile_each_unfollow_button_ic)
                    holder.binding.btnEachFollow.onClick {
                        listener.unFollow(item, holder.layoutPosition)
                    }
                }

                FollowStatus.FOLLOW -> {
                    holder.binding.btnFollow.show()
                    holder.binding.btnFollow.setBackgroundResource(R.drawable.profile_followed_button_bg)
                    holder.binding.btnFollow.text = getCompatString(APP_R.string.common_unfollow)
                    holder.binding.btnFollow.setTextColor(getCompatColor(APP_R.color.color_FFC9CDD4))
                    holder.binding.btnFollow.onClick {
                        listener.unFollow(item, holder.layoutPosition)
                    }
                    holder.binding.btnEachFollow.gone()

                }
            }
        }
    }

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

    interface OnFollowItemClick {
        fun follow(item: FollowItemData, position: Int)
        fun unFollow(item: FollowItemData, position: Int)

        fun goProfile(uid: String)
    }
}