package com.gh.gamecenter.game.gallery

import android.content.Context
import android.view.View
import com.gh.common.exposure.ExposureEvent
import com.gh.common.exposure.ExposureSource
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.safelyGetInRelease
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.view.AsyncCell
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.databinding.GameGalleryItemBinding
import com.gh.gamecenter.entity.SubjectEntity

class GameGalleryViewHolder(val cell: GameGalleryItemCell) :
    BaseRecyclerViewHolder<Any>(cell) {

    fun bindView(
        subjectEntity: SubjectEntity,
        binding: GameGalleryItemBinding,
        basicExposureSource: List<ExposureSource>,
        exposureClosure: (ExposureEvent) -> Unit
    ) {
        binding.titleTv.text = subjectEntity.name
        binding.titleTv.setTextColor(R.color.text_title.toColor(binding.root.context))

        val gameIconList = arrayListOf(
            binding.iconIv1,
            binding.iconIv2,
            binding.iconIv3,
            binding.iconIv4,
            binding.iconIv5,
            binding.iconIv6,
            binding.iconIv7,
            binding.iconIv8
        )

        for ((index, gameIcon) in gameIconList.withIndex()) {
            val gameEntity = subjectEntity.data?.safelyGetInRelease(index) ?: continue

            gameEntity.subjectId = subjectEntity.id

            gameIcon.rotation = 35F
            gameIcon.displayGameIcon(gameEntity)

            runOnIoThread(true) {
                val exposureEvent = ExposureEvent.createEventWithSourceConcat(
                    gameEntity,
                    basicExposureSource,
                    listOf(ExposureSource("专题", "${subjectEntity.name}-图集"))
                )
                exposureClosure.invoke(exposureEvent)
            }
        }

        binding.cardView.setCardBackgroundColor(R.color.text_FAFAFA.toColor(binding.root.context))
        binding.titleTv.setTextColor(R.color.text_title.toColor(binding.root.context))
    }

    class GameGalleryItemCell(context: Context) : AsyncCell(context) {
        var binding: GameGalleryItemBinding? = null

        override val layoutId = R.layout.game_gallery_item
        override val delayFirstTimeBindView = false
        override fun createDataBindingView(view: View): View? {
            binding = GameGalleryItemBinding.bind(view)
            return view.rootView
        }
    }

}