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.NewLogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.databinding.ForumRecordItemBinding
import com.gh.gamecenter.entity.ForumEntity
import com.gh.gamecenter.forum.detail.ForumDetailActivity
import com.lightgame.adapter.BaseRecyclerAdapter

class ForumRecordsAdapter(
    context: Context,
    private val mEntrance: String,
    private var mList: List<ForumEntity>
) : BaseRecyclerAdapter<ForumRecordsAdapter.ForumRecordViewHolder>(context) {

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

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

    override fun getItemCount() = mList.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 = mList[position]
            gameNameTv.text = forumEntity.name
            gameNameTv.setTextColor(R.color.text_subtitle.toColor(mContext))
            unreadHint.goneIf(!forumEntity.unread)
            if (forumEntity.type == "official_bbs") {
                forumIv.displayGameIcon(forumEntity.icon, null)
            } else {
                forumIv.displayGameIcon(
                    forumEntity.game.getIcon(),
                    forumEntity.game.iconSubscript,
                    forumEntity.game.iconFloat
                )
            }

            root.setOnClickListener {
                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
        }

        mList = 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: ForumRecordItemBinding) : BaseRecyclerViewHolder<Any>(binding.root)
}