package com.gh.gamecenter.forum.search

import android.content.Context
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.CheckLoginUtils
import com.gh.common.util.DialogUtils
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.callback.ConfirmListener
import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.databinding.UserSearchListItemBinding
import com.gh.gamecenter.entity.FollowersOrFansEntity
import com.gh.gamecenter.login.user.UserManager

class UserSearchListAdapter(context: Context, val mEntrance: String, val mViewModel: UserSearchListViewModel) :
    ListAdapter<FollowersOrFansEntity>(context) {

    override fun setListData(updateData: MutableList<FollowersOrFansEntity>) {
        var oldSize = 0
        if (mEntityList != null && mEntityList.size > 0) {
            oldSize += mEntityList.size
        }
        mEntityList = ArrayList<FollowersOrFansEntity>(updateData)
        if (oldSize == 0 || oldSize > updateData.size || mViewModel.getLinkPageSize() == 2 /* 第一页 */) {
            notifyDataSetChanged()
        } else {
            notifyItemRangeInserted(oldSize, updateData.size - oldSize)
        }
    }

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            ItemViewType.ITEM_FOOTER -> {
                FooterViewHolder(mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false))
            }
            else -> {
                UserSearchListViewHolder(parent.toBinding())
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mEntityList.isNotEmpty()) mEntityList.size + FOOTER_ITEM_COUNT else 0
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (getItemViewType(position)) {
            ItemViewType.ITEM_BODY -> {
                val binding = (holder as UserSearchListViewHolder).binding
                val entity = mEntityList[position]
                ImageUtils.display(binding.userAvatar, entity.icon)
                binding.userBadge.goneIf(entity.auth.icon.isEmpty()) {
                    ImageUtils.display(binding.userBadge, entity.auth.icon)
                }
                binding.userNameTv.text = entity.name
                ImageUtils.display(binding.userBadgeIcon, entity.badge?.icon)
                binding.userBadgeIcon.goneIf(entity.badge == null)
                binding.userBadgeName.goneIf(entity.badge == null)
                binding.userBadgeName.text = entity.badge?.name
                binding.userSignatureTv.text = if (entity.introduce.isNullOrEmpty()) "这是一个很神秘的崽~" else entity.introduce
                binding.userFansCountTv.text = "${entity.count.fans} 粉丝"
                binding.attentionTv.background =
                    if (entity.me.isFollower) R.drawable.button_round_gray_light.toDrawable(mContext) else R.drawable.bg_forum_follow.toDrawable(
                        mContext
                    )
                binding.attentionTv.text = if (entity.me.isFollower) "已关注" else "关注"
                binding.attentionTv.setTextColor(
                    if (entity.me.isFollower) R.color.text_subtitleDesc.toColor(mContext) else R.color.theme_font.toColor(
                        mContext
                    )
                )
                if (entity.id == UserManager.getInstance().userId) {
                    binding.attentionTv.text = "自己"
                    binding.attentionTv.setTextColor(ContextCompat.getColor(mContext, R.color.text_subtitleDesc))
                    binding.attentionTv.background =
                        ContextCompat.getDrawable(mContext, R.drawable.button_round_gray_light)
                    binding.attentionTv.isEnabled = false
                }
                holder.itemView.setOnClickListener {
                    NewLogUtils.logForumSearchResultClick(
                        "用户tab",
                        "",
                        "",
                        "",
                        "",
                        entity.id,
                        mViewModel.searchKey,
                        position + 1
                    )
                    DirectUtils.directToHomeActivity(mContext, entity.id, mEntrance, "用户搜索")
                }
                binding.userBadgeName.setOnClickListener { binding.userBadgeIcon.performClick() }
                binding.userBadgeIcon.setOnClickListener {
                    DialogUtils.showViewBadgeDialog(binding.root.context, entity.badge, object : ConfirmListener {
                        override fun onConfirm() {
                            MtaHelper.onEvent("进入徽章墙_用户记录", "用户搜索", entity.name + "（" + entity.id + "）")
                            MtaHelper.onEvent("徽章中心", "进入徽章中心", "用户搜索")
                            DirectUtils.directToBadgeWall(binding.root.context, entity.id, entity.name, entity.icon)
                        }
                    })
                }
                holder.binding.attentionTv.setOnClickListener {
                    CheckLoginUtils.checkLogin(mContext, "用户搜索") {
                        if (entity.me.isFollower) {
                            mViewModel.unFollow(entity.id) {
                                entity.me.isFollower = false
                                notifyItemChanged(position)
                            }
                        } else {
                            NewLogUtils.logForumSearchFollowClick("关注用户", "", "", entity.id)
                            mViewModel.follow(entity.id) {
                                entity.me.isFollower = true
                                notifyItemChanged(position)
                            }
                        }
                    }
                }
            }
            ItemViewType.ITEM_FOOTER -> {
                val footerViewHolder = holder as FooterViewHolder
                footerViewHolder.initItemPadding()
                footerViewHolder.initFooterViewHolder(mIsLoading, mIsNetworkError, mIsOver)
                footerViewHolder.hint.textSize = 12f
                footerViewHolder.hint.setTextColor(ContextCompat.getColor(mContext, R.color.aaaaaa))
            }
        }
    }


    class UserSearchListViewHolder(val binding: UserSearchListItemBinding) : BaseRecyclerViewHolder<Any>(binding.root)
}