package com.gh.gamecenter.forum.moderator

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.common.util.*
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.databinding.ModeratorListItemBinding
import com.gh.gamecenter.feature.entity.PersonalEntity
import com.gh.gamecenter.login.user.UserManager
import com.lightgame.adapter.BaseRecyclerAdapter

class ModeratorListAdapter(context: Context, val mViewModel: ModeratorListViewModel?) :
    BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {
    val datas = ArrayList<PersonalEntity>()

    fun setListData(datas: ArrayList<PersonalEntity>) {
        this.datas.clear()
        this.datas.addAll(datas)
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int = datas.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ModeratorItemViewHolder(parent.toBinding())
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ModeratorItemViewHolder) {
            val personalEntity = datas[position]
            var mHasNameLabel = false
            if (personalEntity.nameLabel?.isNotEmpty() == true) {
                mHasNameLabel = true
            }
            holder.binding.run {
                ImageUtils.display(userIcon, personalEntity.icon)
                userNameTv.text = personalEntity.name
                labelTv.text = personalEntity.nameLabel
                labelTv.visibility = if (mHasNameLabel) View.VISIBLE else View.GONE
                followTv.background =
                    if (personalEntity.me.isFollower) com.gh.gamecenter.feature.R.drawable.button_round_gray_light.toDrawable(mContext) else R.drawable.bg_forum_follow.toDrawable(
                        mContext
                    )
                followTv.text = if (personalEntity.me.isFollower) "已关注" else "关注"
                followTv.setTextColor(
                    if (personalEntity.me.isFollower) com.gh.gamecenter.common.R.color.text_tertiary.toColor(mContext) else com.gh.gamecenter.common.R.color.text_theme.toColor(
                        mContext
                    )
                )

                followTv.goneIf(personalEntity.id == UserManager.getInstance().userId)

                userIcon.setOnClickListener {
                    NewLogUtils.logForumModeratorClick("click_forum_member_profile_photo", personalEntity.id)
                    root.performClick()
                }
                userNameTv.setOnClickListener {
                    NewLogUtils.logForumModeratorClick("click_forum_member_nickname", personalEntity.id)
                    root.performClick()
                }
                followTv.setOnClickListener {
                    SensorsBridge.trackUserFollowClick(
                        userId = personalEntity.id,
                        userName = personalEntity.name,
                        buttonName = followTv.text.toString()
                    )
                    mContext.ifLogin("板块成员") {
                        if (personalEntity.id == UserManager.getInstance().userId) return@ifLogin
                        debounceActionWithInterval(it.id) {
                            if (personalEntity.me.isFollower) {
                                NewLogUtils.logForumModeratorClick("click_forum_member_followed", personalEntity.id)
                                mViewModel?.followingCommand(personalEntity.id, false) {
                                    personalEntity.me.isFollower = false
                                    notifyItemChanged(position)
                                }
                            } else {
                                NewLogUtils.logForumModeratorClick("click_forum_member_follow", personalEntity.id)
                                mViewModel?.followingCommand(personalEntity.id, true) {
                                    personalEntity.me.isFollower = true
                                    notifyItemChanged(position)
                                }
                            }
                        }
                    }
                }
                root.setOnClickListener {
                    DirectUtils.directToHomeActivity(mContext, personalEntity.id, 1, "板块成员", "")
                }
            }
        }
    }

    class ModeratorItemViewHolder(val binding: ModeratorListItemBinding) :
        BaseRecyclerViewHolder<PersonalEntity>(binding.root)
}