package com.gh.gamecenter.home.custom.adapter

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.eventbus.EBDownloadStatus
import com.gh.gamecenter.eventbus.EBPackage
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.home.PageConfigure
import com.gh.gamecenter.home.custom.eventlistener.SubjectEventHelper
import com.gh.gamecenter.home.custom.model.CustomSubjectItem
import com.gh.gamecenter.home.custom.viewholder.CustomHomeHorizontalSlideVideoItemViewHolder
import com.lightgame.download.DownloadEntity
import splitties.views.bottomPadding
import splitties.views.leftPadding
import splitties.views.rightPadding
import splitties.views.topPadding

class CustomHomeHorizontalSlideVideoAdapter(
    context: Context,
    private val pageConfigure: PageConfigure,
    private val eventHelper: SubjectEventHelper,
) : CustomBaseChildAdapter<GameEntity, CustomHomeHorizontalSlideVideoItemViewHolder>(context) {

    private lateinit var item: CustomSubjectItem

    fun setNewData(item: CustomSubjectItem) {
        this.item = item
        submitList(item.data.data)
    }

    override fun getKey(t: GameEntity): String {
        return t.id
    }

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

    override fun onBindViewHolder(holder: CustomHomeHorizontalSlideVideoItemViewHolder, position: Int) {
        val gameEntity = dataList[position]

        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(
            position,
            gameEntity,
            this,
            pageConfigure.entrance,
            item.data.showIndexIconSubscript,
            item.data.showIndexSubtitle
        )
    }

    override fun notifyDownload(download: DownloadEntity) {
        dataList.forEachIndexed { index, game ->
            if (game.id == download.gameId) {
                notifyItemChanged(index)
            }
        }
    }

    override fun notifyDownloadDeleted(status: EBDownloadStatus) {
        notifyChildItem(status.packageName)
    }

    override fun notifyInstalled(busFour: EBPackage) {
        notifyChildItem(busFour.packageName)
    }

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