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

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.gh.common.util.NewFlatLogUtils
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.RecyclerRefreshIconItemCustomBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.IExposureProvider
import com.gh.gamecenter.game.horizontal.GameHorizontalSimpleItemViewHolder
import com.gh.gamecenter.home.custom.eventlistener.GameSubjectCollectionEventHelper
import com.gh.gamecenter.home.custom.model.CustomPageData

class CustomRefreshIconAdapter(
    context: Context,
    private val eventHelper: GameSubjectCollectionEventHelper,
) : CustomBaseChildAdapter<GameEntity, CustomRefreshIconAdapter.RefreshIconChildViewHolder>(context) {

    private var collectionName = ""
    private var collectionId = ""
    private var subject: CustomPageData.LinkColumnCollection.CustomSubjectEntity? = null

    fun setData(
        collectionName: String,
        collectionId: String,
        subject: CustomPageData.LinkColumnCollection.CustomSubjectEntity,
        list: List<GameEntity>
    ) {
        this.collectionName = collectionName
        this.collectionId = collectionId
        this.subject = subject
        submitList(list)
    }

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

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

    override fun onBindViewHolder(holder: RefreshIconChildViewHolder, position: Int) {
        val gameEntity = getItem(position)

        holder.exposureEvent = gameEntity.exposureEvent
        holder.binding.run {
            val padR = if (position == itemCount - 1) 16F.dip2px() else 0
            root.setPadding(16F.dip2px(), 0, padR, 0)
            gameName.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
            gameIcon.displayGameIcon(gameEntity)
            GameHorizontalSimpleItemViewHolder.setHorizontalNameAndGravity(gameName, gameEntity.name)

            root.setOnClickListener {
                NewFlatLogUtils.logGameListCollectionClick(
                    "",
                    "",
                    "",
                    collectionName,
                    collectionId,
                    "游戏"
                )
                eventHelper.navigateToGameDetailPage(position, gameEntity, subject)
            }
        }
    }

    class RefreshIconChildViewHolder(val binding: RecyclerRefreshIconItemCustomBinding) :
        ViewHolder(binding.root), IExposureProvider {
        var exposureEvent: ExposureEvent? = null

        override fun provideExposureData(): ExposureEvent? {
            return exposureEvent?.getFreshExposureEvent()
        }
    }
}