package com.gh.gamecenter.catalog

import android.content.Context
import android.view.ViewGroup
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.databinding.CatalogSubjectGameItemBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.lightgame.adapter.BaseRecyclerAdapter

class SpecialCatalogSubjectAdapter(
    context: Context,
    private val mCatalogViewModel: SpecialCatalogViewModel,
    private var mList: List<GameEntity>
) : BaseRecyclerAdapter<SpecialCatalogSubjectAdapter.CatalogSubjectGameItemViewHolder>(context) {

    private val mEntrance = "精选分类"
    private var countAndKey: Pair<Int, String>? = null
    private var mPosition: Int = -1

    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) =
        CatalogSubjectGameItemViewHolder(parent.toBinding())

    override fun onBindViewHolder(holder: CatalogSubjectGameItemViewHolder, position: Int) {
        holder.binding.run {
            root.layoutParams = (root.layoutParams as ViewGroup.MarginLayoutParams).apply {
                topMargin = if (position > 2) 24F.dip2px() else 16F.dip2px()
            }

            val entity = mList[position]
            gameIcon.displayGameIcon(entity)
            gameName.text = entity.name
            gameName.setTextColor(R.color.text_title.toColor(mContext))

            if (!gameName.isSelected) {
                gameName.postDelayed({ gameName.isSelected = true }, 500)
            }

            root.setOnClickListener {
                GameDetailActivity.startGameDetailActivity(mContext, entity.id, "(${mEntrance})", entity.exposureEvent)
                mCatalogViewModel.logSpecialCatalogSpecificContentClick(
                    "专题",
                    entity.subjectName ?: "",
                    entity.name ?: "",
                    mPosition,
                    position
                )
            }
        }
    }

    fun checkResetData(update: List<GameEntity>) {
        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()
        } else {
            notifyItemRangeChanged(0, itemCount)
        }

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

    fun updatePosition(position: Int) {
        mPosition = position
    }

    class CatalogSubjectGameItemViewHolder(val binding: CatalogSubjectGameItemBinding) :
        BaseRecyclerViewHolder<Any>(binding.root)
}