package com.gh.gamecenter.personalhome

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.common.util.*
import com.gh.common.util.DialogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.callback.ConfirmListener
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.NumberUtils
import com.gh.gamecenter.databinding.FollowersOrFansItemBinding
import com.gh.gamecenter.entity.FollowersOrFansEntity
import com.gh.gamecenter.login.user.UserManager

class FollowersOrFansAdapter(context: Context, val mViewModel: FollowersOrFansViewModel, val entrance: String) :
    ListAdapter<FollowersOrFansEntity>(context) {

    var onBadgeClickListener: ((entity: FollowersOrFansEntity) -> Unit)? = null

    override fun areItemsTheSame(oldItem: FollowersOrFansEntity?, newItem: FollowersOrFansEntity?): Boolean {
        return oldItem?.id == newItem?.id
    }

    override fun areContentsTheSame(oldItem: FollowersOrFansEntity?, newItem: FollowersOrFansEntity?): Boolean {
        return oldItem == newItem
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == itemCount - 1) ItemViewType.ITEM_FOOTER else ItemViewType.ITEM_BODY
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        val view: View
        return when (viewType) {
            ItemViewType.ITEM_FOOTER -> {
                view = mLayoutInflater.inflate(com.gh.gamecenter.common.R.layout.refresh_footerview, parent, false)
                FooterViewHolder(view)
            }
            else -> {
                view = mLayoutInflater.inflate(R.layout.followers_or_fans_item, parent, false)
                FollowersOrFansViewHolder(FollowersOrFansItemBinding.bind(view))
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mEntityList == null || mEntityList.isEmpty()) 0 else mEntityList.size + FOOTER_ITEM_COUNT
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        if (holder is FollowersOrFansViewHolder) {
            val entity = mEntityList[position]
            holder.binding.countRl.goneIf(!(entity.count.answer > 0 && entity.count.vote > 0))
            holder.binding.answerCount.goneIf(entity.count.answer <= 0)
            holder.binding.answerCount.text = NumberUtils.transSimpleCount(entity.count.answer) + "回答"
            holder.binding.voteCount.goneIf(entity.count.vote <= 0)
            holder.binding.voteCount.text = NumberUtils.transSimpleCount(entity.count.vote) + "赞同"
            holder.binding.userIntroduce.text = if (entity.introduce.isNullOrEmpty()) "这是一个很神秘的崽~" else entity.introduce
            holder.binding.userName.text = entity.name
            holder.binding.divider.visibility = if (position == 0) View.GONE else View.VISIBLE
            holder.binding.userIcon.display(entity.border, entity.icon, entity.auth.icon)
            holder.binding.followerBtn.run {
                when {
                    entity.id == UserManager.getInstance().userId -> {
                        setText(R.string.myself)
                        background = com.gh.gamecenter.feature.R.drawable.button_round_gray_light.toDrawable(mContext)
                        setTextColor(ContextCompat.getColor(mContext, com.gh.gamecenter.common.R.color.text_tertiary))
                    }
                    entity?.me?.isFollower!! -> {
                        setText(R.string.concerned)
                        background = com.gh.gamecenter.feature.R.drawable.button_round_gray_light.toDrawable(mContext)
                        setTextColor(ContextCompat.getColor(mContext, com.gh.gamecenter.common.R.color.text_tertiary))
                    }
                    else -> {
                        setText(com.gh.gamecenter.feature.R.string.concern)
                        background = R.drawable.button_round_primary_light.toDrawable(mContext)
                        setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(mContext))
                    }
                }
            }

            holder.binding.followerBtn.setOnClickListener {
                SensorsBridge.trackUserFollowClick(
                    userId = entity.id,
                    userName = entity.name,
                    buttonName = holder.binding.followerBtn.text.toString()
                )
                mContext.ifLogin("个人主页-粉丝-[关注]") {
                    if (entity.id != UserManager.getInstance().userId) {
                        if (entity?.me?.isFollower!!) {
                            DialogHelper.showDialog(
                                mContext, "取消关注", "确定要取消关注 ${entity.name} 吗？",
                                "确定取消", "暂不取消", {
                                    mViewModel.followingCommand(!entity.me.isFollower, entity.id, position)
                                }, extraConfig = DialogHelper.Config(centerTitle = true, centerContent = true)
                            )
                        } else {
                            mViewModel.followingCommand(!entity.me.isFollower, entity.id, position)
                        }
                    }
                }
            }
            holder.itemView.setOnClickListener {
                DirectUtils.directToHomeActivity(mContext, entity.id, entrance, "个人主页-粉丝及关注页面")
            }

            if (entity.badge != null) {
                holder.binding.sdvUserBadge.visibility = View.VISIBLE
                ImageUtils.display(holder.binding.sdvUserBadge, entity.badge.icon)
            } else {
                holder.binding.sdvUserBadge.visibility = View.GONE
            }

            holder.binding.sdvUserBadge.setOnClickListener { v ->
                DialogUtils.showViewBadgeDialog(mContext, entity.badge, object : ConfirmListener {
                    override fun onConfirm() {
                        onBadgeClickListener?.invoke(entity)
                        DirectUtils.directToBadgeWall(mContext, entity.id, entity.name, entity.icon)
                    }
                })
            }
        } else if (holder is FooterViewHolder) {
            holder.initFooterViewHolder(mViewModel, mIsLoading, mIsNetworkError, mIsOver)
        }
    }

}