package com.gh.gamecenter.home.custom.viewholder

import android.content.Context
import android.view.View
import com.gh.common.util.AdHelper
import com.gh.gamecenter.R
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.GameGalleryItemCustomBinding
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.exposure.IExposureListProvider
import com.gh.gamecenter.home.custom.CustomPageViewModel
import com.gh.gamecenter.home.custom.createExposureEvent
import com.gh.gamecenter.home.custom.eventlistener.SubjectEventHelper
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.home.custom.model.CustomSubjectItem

/**
 * 游戏专题-静态图集
 */
class CustomGameGalleryViewHolder(
    viewModel: CustomPageViewModel,
    cell: GameGalleryItemCell
) : BaseCustomViewHolder(viewModel, cell.rootView), IExposureListProvider {

    private var boundedItem: CustomSubjectItem? = null

    override val childEventHelper by lazy(LazyThreadSafetyMode.NONE) {
        SubjectEventHelper(viewModel)
    }

    override fun bindView(item: CustomPageItem) {
        super.bindView(item)
        if (item is CustomSubjectItem) {
            val subjectEntity = item.data
            item.exposureEventList.clear()
            boundedItem = item
            (itemView as? GameGalleryItemCell)?.let {
                it.bindWhenInflated {
                    val binding = it.binding ?: return@bindWhenInflated
                    binding.titleTv.text = subjectEntity.name
                    binding.titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.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 dataSize = subjectEntity.data?.size ?: 0
                        if (index >= dataSize) {
                            gameIcon.visibility = View.GONE
                        } else {
                            val gameEntity = subjectEntity.data?.safelyGetInRelease(index) ?: continue

                            gameEntity.subjectId = subjectEntity.id
                            gameEntity.pageLevelString = pageLocation.pageLevelString

                            gameIcon.visibility = View.VISIBLE
                            gameIcon.rotation = 35F
                            gameIcon.displayGameIconWithIfShowSubscript(
                                gameEntity,
                                subjectEntity.showIndexIconSubscript
                            )

                            gameIcon.setOnClickListener {
                                childEventHelper.navigateToGameDetailPage(index, gameEntity)
                            }

                            runOnIoThread(true) {
                                val exposureEvent = createExposureEvent(
                                    gameEntity,
                                    item.exposureSource,
                                    pageConfigure.exposureSourceList,
                                    index,
                                    item.componentPosition,
                                    createTrackData(item)
                                )

                                if (!gameEntity.adGroupId.isEmpty() && !gameEntity.isAdRequestReported) {
                                    AdHelper.reportAdRequest(exposureEvent)
                                    gameEntity.isAdRequestReported = true
                                }
                                item.exposureEventList.add(exposureEvent)
                            }
                        }
                    }

                    binding.cardView.setCardBackgroundColor(com.gh.gamecenter.common.R.color.text_FAFAFA.toColor(binding.root.context))
                    binding.titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(binding.root.context))

                    binding.root.setOnClickListener {
                        childEventHelper.navigateSubjectPage()
                    }
                }
            }
        }
    }

    override fun provideExposureData(): ExposureEvent? = null

    override fun provideExposureDataList(): List<ExposureEvent>? {
        if (boundedItem?.exposureEventList == null) return null

        for (exposure in boundedItem!!.exposureEventList) {
            exposure.getFreshExposureEvent()
        }

        return boundedItem?.exposureEventList
    }

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

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

}