package com.gh.gamecenter.game.doublecard

import android.content.Context
import android.view.ViewGroup
import com.gh.common.exposure.ExposureEvent
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.display
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.core.utils.StringUtils
import com.gh.gamecenter.databinding.GameDoubleCardItemBinding
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.entity.SubjectEntity
import com.gh.gamecenter.game.GameItemViewHolder
import com.lightgame.adapter.BaseRecyclerAdapter

class DoubleCardListAdapter(
    context: Context,
    private var mSubjectEntity: SubjectEntity
) : BaseRecyclerAdapter<DoubleCardListAdapter.DoubleCardItemViewHolder>(context) {

    var exposureEventList: ArrayList<ExposureEvent>? = null

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

    init {
        var dataIds = ""
        mSubjectEntity.data?.forEach {
            dataIds += it.id
        }
        if (dataIds.isNotEmpty()) countAndKey = Pair(mSubjectEntity.data?.size ?: 0, dataIds)
    }

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

    override fun getItemCount() = if (mSubjectEntity.data?.isNotEmpty() == true) mSubjectEntity.data!!.size else 0

    override fun onBindViewHolder(holder: DoubleCardItemViewHolder, position: Int) {
        val gameEntity = mSubjectEntity.data!![position]
        holder.binding.run {
            poster.display(gameEntity.columnImage)
            gameName.text = gameEntity.name
            brief.text =
                if (gameEntity.columnRecommend?.text.isNullOrBlank()) gameEntity.brief
                else gameEntity.columnRecommend!!.text
            GameItemViewHolder.initGameSubtitle(gameEntity, gameSubtitle)

            root.setOnClickListener {
                GameDetailActivity.startGameDetailActivity(
                    mContext,
                    gameEntity.id,
                    StringUtils.buildString("(游戏-专题:", mSubjectEntity.name, "-列表[", (position + 1).toString(), "])"),
                    traceEvent = exposureEventList?.get(position)
                )
            }
        }
    }

    // notifyDataSetChanged 会出现页面抖动情况
    fun checkResetData(subjectEntity: SubjectEntity) {
        var dataIds = ""
        subjectEntity.data?.forEach {
            dataIds += it.id
        }

        mSubjectEntity = subjectEntity
        if (countAndKey?.first == subjectEntity.data?.size && countAndKey?.second != dataIds) { // 数量不变,内容发生改变
            notifyItemRangeChanged(0, itemCount)
        } else if (countAndKey?.first != subjectEntity.data?.size) { // 数量发生改变
            notifyDataSetChanged()
        }

        // 重新刷新数据标识
        countAndKey = Pair(subjectEntity.data?.size ?: 0, dataIds)
    }

    inner class DoubleCardItemViewHolder(val binding: GameDoubleCardItemBinding) :
        BaseRecyclerViewHolder<GameEntity>(binding.root)
}