package com.gh.gamecenter.forum.home

import android.content.Context
import android.view.ViewGroup
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.core.utils.NumberUtils
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.databinding.HorizontalForumItemBinding
import com.gh.gamecenter.entity.ForumEntity
import com.gh.gamecenter.eventbus.EBForumFollowChange
import com.gh.gamecenter.forum.detail.ForumDetailActivity
import com.lightgame.adapter.BaseRecyclerAdapter
import org.greenrobot.eventbus.EventBus

class HorizontalForumsAdapter(
    context: Context,
    private val mEntrance: String,
    var list: List<ForumEntity>,
    private var mViewModel: ForumViewModel? = null
) : BaseRecyclerAdapter<HorizontalForumsAdapter.ForumRecordViewHolder>(context) {

    private var countAndKey: Pair<Int, String>? = null

    init {
        var dataIds = ""
        list.forEach {
            dataIds += it.id
        }
        if (dataIds.isNotEmpty()) countAndKey = Pair(list.size, dataIds)
    }

    override fun getItemCount() = list.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ForumRecordViewHolder(parent.toBinding())


    override fun onBindViewHolder(holder: ForumRecordViewHolder, position: Int) {
        holder.binding.run {
            root.layoutParams = (root.layoutParams as ViewGroup.MarginLayoutParams).apply {
                leftMargin = if (position == 0) 16F.dip2px() else 0
            }

            val forumEntity = list[position]
            gameNameTv.text = forumEntity.name
            gameNameTv.setTextColor(R.color.text_title.toColor(mContext))
            hotTv.text = NumberUtils.transSimpleCount(forumEntity.hot)

            unreadHint.goneIf(!forumEntity.unread || mEntrance.contains("热门论坛"))
            followTv.goneIf(!mEntrance.contains("热门论坛"))

            if (forumEntity.type == "official_bbs") {
                forumIv.displayGameIcon(forumEntity.icon, null)
            } else {
                forumIv.displayGameIcon(
                    forumEntity.game.getIcon(),
                    forumEntity.game.iconSubscript,
                    forumEntity.game.iconFloat
                )
            }

            followTv.run {
                text = if (forumEntity.me.isFollowForum) {
                    background = R.drawable.button_round_gray_light.toDrawable(mContext)
                    setTextColor(R.color.text_subtitleDesc.toColor(mContext))
                    "已关注"
                } else {
                    background = R.drawable.button_round_primary_light.toDrawable(mContext)
                    setTextColor(R.color.theme_font.toColor(mContext))
                    "关注"
                }
            }

            followTv.setOnClickListener {
                handleFollowTvClick(this, forumEntity)
            }

            root.setOnClickListener {
                handleItemViewClick(forumEntity)
            }
        }
    }

    private fun handleFollowTvClick(binding: HorizontalForumItemBinding, forumEntity: ForumEntity) {
        binding.run {
            debounceActionWithInterval(R.id.followTv) {
                CheckLoginUtils.checkLogin(mContext, mEntrance) {
                    if (forumEntity.me.isFollowForum) {
                        mViewModel?.unFollowForum(forumEntity.id) {
                            forumEntity.me.isFollowForum = false
                            followTv.background = R.drawable.button_round_primary_light.toDrawable(mContext)
                            followTv.setTextColor(R.color.theme_font.toColor(mContext))
                            followTv.text = "关注"
                            ToastUtils.showToast("取消成功")
                            EventBus.getDefault().post(EBForumFollowChange(forumEntity, false))
                        }
                    } else {
                        mViewModel?.followForum(forumEntity.id) {
                            forumEntity.me.isFollowForum = true
                            followTv.background = R.drawable.button_round_gray_light.toDrawable(mContext)
                            followTv.setTextColor(R.color.text_subtitleDesc.toColor(mContext))
                            followTv.text = "已关注"
                            ToastUtils.showToast("关注成功")
                            EventBus.getDefault().post(EBForumFollowChange(forumEntity, true))
                        }
                    }
                }
            }
        }
    }

    private fun handleItemViewClick(forumEntity: ForumEntity) {
        val bbsType = if (forumEntity.type == "official_bbs") "综合论坛" else "游戏论坛"
        val event = if (mEntrance.contains("最近浏览")) "click_recent_forum" else "click_following_forum"
        val location = if (mEntrance.contains("最近浏览")) "推荐信息流" else "论坛页"
        NewLogUtils.logForumClick(event, location, forumEntity.id, bbsType)
        mContext.startActivity(ForumDetailActivity.getIntent(mContext, forumEntity.id, mEntrance))
    }

    fun checkResetData(update: List<ForumEntity>) {
        var dataIds = ""
        update.forEach {
            dataIds += it.id
        }

        list = update
        if (countAndKey?.first == update.size && countAndKey?.second != dataIds) {
            notifyItemRangeChanged(0, itemCount)
        } else if (countAndKey?.first != update.size) {
            notifyDataSetChanged()
        }

        // 重新刷新数据标识
        countAndKey = Pair(update.size, dataIds)
    }

    class ForumRecordViewHolder(val binding: HorizontalForumItemBinding) : BaseRecyclerViewHolder<Any>(binding.root)
}