package com.gh.gamecenter.qa.dialog

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.NewFlatLogUtils
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.constant.ItemViewType
import com.gh.gamecenter.common.entity.CommunityEntity
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.viewholder.FooterViewHolder
import com.gh.gamecenter.core.utils.HtmlUtils
import com.gh.gamecenter.core.utils.NumberUtils
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.databinding.ForumItemBinding
import com.gh.gamecenter.entity.ForumEntity
import com.gh.gamecenter.eventbus.EBForumFollowChange
import org.greenrobot.eventbus.EventBus

class ChooseForumContainerAdapter(
    content: Context,
    val type: String,
    val viewModel: ChooseForumContainerViewModel,
    private val entrance: String,
    private val childClick: (Int, ForumEntity, String) -> Unit,
    val onSelectCallback: ((entity: CommunityEntity, position: Int) -> Unit)? = null
) : ListAdapter<ForumEntity>(content) {

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

            else -> {
                ForumItemViewHolder(parent.toBinding())
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mEntityList.isNotEmpty()) {
            if (type != ChooseForumContainerFragment.ChooseForumType.ATTENTION.value) {
                mEntityList.size + FOOTER_ITEM_COUNT
            } else {
                mEntityList.size
            }
        } else 0
    }

    override fun getItemViewType(position: Int): Int {
        if (type != ChooseForumContainerFragment.ChooseForumType.ATTENTION.value) {
            if (position == itemCount - 1) return ItemViewType.ITEM_FOOTER
            return ItemViewType.ITEM_BODY
        } else {
            return ItemViewType.ITEM_BODY
        }

    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ForumItemViewHolder) {
            val forumEntity = mEntityList[position]
            holder.binding.forumNameTv.text = forumEntity.name.fromHtml()

            val drawableResId =
                if (forumEntity.me.isFollowForum) R.drawable.bg_forum_already_follow else R.drawable.bg_forum_follow

            holder.binding.followTv.goneIf(type == ChooseForumContainerFragment.ChooseForumType.ATTENTION.value)

            holder.binding.followTv.background = drawableResId.toDrawable(holder.itemView.context)

            holder.binding.followTv.text = if (forumEntity.me.isFollowForum) "已关注" else "关注"
            holder.binding.followTv.setTextColor(
                if (forumEntity.me.isFollowForum) com.gh.gamecenter.common.R.color.text_tertiary.toColor(mContext) else com.gh.gamecenter.common.R.color.primary_theme.toColor(
                    mContext
                )
            )
            holder.binding.hotTv.text = NumberUtils.transSimpleCount(forumEntity.hot)

            holder.binding.followTv.setOnClickListener {
                val button = if (forumEntity.me.isFollowForum) BUTTON_UN_FOLLOW else BUTTON_FOLLOW
                childClick(position, forumEntity, button)
                handleFollowTvClick(holder.binding, forumEntity)
            }

            val icon = forumEntity.icon.ifEmpty { forumEntity.game.getIcon() }
            holder.binding.forumIcon.displayGameIcon(icon, forumEntity.game.iconSubscript, forumEntity.game.iconFloat)
            holder.itemView.setOnClickListener {
                val tabType = if (type == ChooseForumContainerFragment.ChooseForumType.SEARCH.value) "论坛tab" else ""
                val bbsType = if (forumEntity.type == "official_bbs") "综合论坛" else "游戏论坛"
                NewLogUtils.logForumSearchResultClick(
                    tabType,
                    forumEntity.id,
                    bbsType,
                    "",
                    "",
                    "",
                    viewModel.searchKey,
                    position + 1
                )
                onSelectCallback?.invoke(
                    CommunityEntity(
                        forumEntity.id,
                        HtmlUtils.stripHtml(forumEntity.name),
                        type = forumEntity.type,
                        game = forumEntity.game.toCommunityGameEntity(),
                        icon = icon,
                        iconSubscript = forumEntity.game.iconSubscript
                    ), position
                )

                childClick(position, forumEntity, VIEW_FORUM_DETAIL)
            }
        } else if (holder is FooterViewHolder) {
            holder.initItemPadding()
            holder.initFooterViewHolder(mIsLoading, mIsNetworkError, mIsOver)
            holder.hint.textSize = 12f
            holder.hint.setTextColor(ContextCompat.getColor(mContext, com.gh.gamecenter.common.R.color.aaaaaa))
        }
    }

    private fun handleFollowTvClick(binding: ForumItemBinding, forumEntity: ForumEntity) {
        binding.run {
            debounceActionWithInterval(R.id.followTv) {
                SensorsBridge.trackFollowForumClick(
                    bbsId = forumEntity.id,
                    forumName = forumEntity.name,
                    bbsType = forumEntity.typeChinese,
                    buttonName = followTv.text.toString(),
                    gameForumType = forumEntity.game.categoryChinese
                )
                CheckLoginUtils.checkLogin(mContext, entrance) {
                    if (forumEntity.me.isFollowForum) {
                        viewModel.unFollowForum(forumEntity.id) {
                            forumEntity.me.isFollowForum = false
                            followTv.background = R.drawable.bg_forum_follow.toDrawable(mContext)
                            followTv.setTextColor(com.gh.gamecenter.common.R.color.primary_theme.toColor(mContext))
                            followTv.text = "关注"
                            ToastUtils.showToast("取消成功")
                            EventBus.getDefault().post(EBForumFollowChange(newEntity(forumEntity), false))
                        }
                    } else {
                        viewModel.followForum(forumEntity.id) {
                            forumEntity.me.isFollowForum = true
                            followTv.background = R.drawable.bg_forum_already_follow.toDrawable(mContext)
                            followTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(mContext))
                            followTv.text = "已关注"
                            ToastUtils.showToast("关注成功")
                            EventBus.getDefault().post(EBForumFollowChange(newEntity(forumEntity), true))
                        }
                    }
                }
            }
        }
    }


    private fun newEntity(forumEntity: ForumEntity) = with(forumEntity) {
        ForumEntity(
            id, game, HtmlUtils.stripHtml(name), icon, isFollow, isRecommend, orderTag, unread, type, me, hot
        )
    }

    companion object {

        private const val BUTTON_FOLLOW = "关注"
        private const val BUTTON_UN_FOLLOW = "取消关注"
        private const val VIEW_FORUM_DETAIL = "查看论坛详情"
    }

    class ForumItemViewHolder(val binding: ForumItemBinding) : BaseRecyclerViewHolder<ForumEntity>(binding.root)
}