package com.gh.gamecenter.home.horizontalslidevideo

import android.content.Context
import android.view.ViewGroup
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.ExposureSource
import com.lightgame.adapter.BaseRecyclerAdapter
import com.lightgame.download.DownloadEntity
import splitties.views.bottomPadding
import splitties.views.leftPadding
import splitties.views.rightPadding
import splitties.views.topPadding

class HomeHorizontalSlideVideoAdapter(
    mContext: Context,
    private var mGames: MutableList<GameEntity>,
    private val mSubjectName: String,
    private val mEntrance: String,
    private val mBasicExposureSources: List<ExposureSource>,
    private val mExposureClosure: (ExposureEvent) -> Unit
) :
    BaseRecyclerAdapter<HomeHorizontalSlideVideoItemViewHolder>(mContext) {

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

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeHorizontalSlideVideoItemViewHolder {
        return HomeHorizontalSlideVideoItemViewHolder(parent.toBinding())
    }

    override fun getItemCount(): Int = mGames.size

    override fun onBindViewHolder(holder: HomeHorizontalSlideVideoItemViewHolder, position: Int) {
        val gameEntity = mGames[position]
        val event = ExposureEvent.createEventWithSourceConcat(
            gameEntity = gameEntity,
            basicSource = mBasicExposureSources,
            source = listOf(ExposureSource("专题", mSubjectName))
        )
        gameEntity.exposureEvent = event
        mExposureClosure.invoke(event)

        holder.binding.root.setPadding(
            16F.dip2px(),
            holder.binding.root.topPadding,
            holder.binding.root.rightPadding,
            holder.binding.root.bottomPadding
        )

        if (position == itemCount - 1) {
            holder.binding.root.setPadding(
                holder.binding.root.leftPadding,
                holder.binding.root.topPadding,
                16F.dip2px(),
                holder.binding.root.bottomPadding
            )
        }
        holder.bindAttachGame(gameEntity, this, mEntrance, event)
    }


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

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

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

    fun notifyItemByDownload(downloadEntity: DownloadEntity?) {
        if (downloadEntity == null) {
            notifyDataSetChanged()
        } else {
            mGames.forEachIndexed { position, gameEntity ->
                if (downloadEntity.gameId == gameEntity.id) {
                    notifyItemChanged(position)
                }
            }
        }
    }

    fun notifyChildItem(packageName: String) {
        mGames.forEachIndexed { position, gameEntity ->
            gameEntity.getApk().forEach { apkEntity ->
                if (apkEntity.packageName == packageName) {
                    notifyItemChanged(position)
                    return
                }
            }
        }
    }
}