package com.gh.gamecenter.gamedetail.detail.adapter

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.exposure.ExposureManager
import com.gh.common.filter.RegionSettingHelper
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.GamedetailItemGameCollectionBinding
import com.gh.gamecenter.entity.GameDetailRecommendGameCollectionEntity
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.gamedetail.detail.viewholder.BaseGameDetailItemViewHolder

class GameCollectionAdapter(
    private val mRecommendGameList: ArrayList<GameDetailRecommendGameCollectionEntity>,
    private val mGameId: String,
    private val mGameName: String,
    private val mGame: GameEntity?,
    private val mEntrance: String,
    private val mPath: String,
    private val mBasicExposureSource: List<ExposureSource>,
    private val trackData: BaseGameDetailItemViewHolder.GameDetailModuleTrackData,
    private val getGameStatus: () -> String
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val mDefaultHorizontalPadding by lazy { com.gh.gamecenter.common.R.dimen.game_detail_item_horizontal_padding.toPx() }

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

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val params = holder.itemView.layoutParams as RecyclerView.LayoutParams
        params.leftMargin = if (position == 0) mDefaultHorizontalPadding else 8F.dip2px()
        params.rightMargin = if (position == itemCount - 1) mDefaultHorizontalPadding else 0F.dip2px()
        holder.itemView.layoutParams = params
        if (holder is GameCollectionItemViewHolder) {
            val entity = mRecommendGameList[position]
            holder.binding.run {
                root.background = R.drawable.bg_shape_space_radius_5.toDrawable(root.context)
                titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(root.context))
                titleTv.text = entity.title
                ImageUtils.display(coverImage, entity.cover)
                val gameIcons = arrayListOf(gameImage1, gameImage2, gameImage3)
                val games = RegionSettingHelper.filterSimpleGame(entity.games)
                gameIcons.forEachIndexed { index, gameIcon ->
                    gameIcon.goneIf(games.size < index + 1) {
                        val gameEntity = games[index].toGameEntity()
                        val event = ExposureEvent.createEvent(
                            gameEntity = gameEntity.apply {
                                sequence = position
                            },
                            source = listOf(
                                ExposureSource("游戏详情", "$mGameName+$mGameId"),
                                ExposureSource("游戏单", "${entity.title}+${entity.id}")
                            )
                        )
                        ExposureManager.log(event)
                        gameIcon.displayGameIcon(gameEntity)
                        gameIcon.setBorderColor(com.gh.gamecenter.common.R.color.ui_surface)
                        gameIcon.setOnClickListener {
                            GameDetailActivity.startGameDetailActivity(
                                root.context,
                                games[index].id ?: "",
                                BaseActivity.mergeEntranceAndPath(mEntrance, "游戏详情"),
                                event
                            )
                        }
                    }
                }
                gameCountTv.text = "+${entity.count.game - games.size}"
                root.setOnClickListener {
                    SensorsBridge.trackGameDetailModuleClick(
                        trackData.gameId,
                        trackData.gameName,
                        trackData.gameType,
                        "组件内容",
                        trackData.moduleType,
                        trackData.moduleName,
                        trackData.sequence,
                        entity.title,
                        position + 1,
                        "game_list_detail",
                        entity.id,
                        entity.title,
                        getGameStatus()
                    )
                    DirectUtils.directToGameCollectionDetail(
                        it.context,
                        entity.id,
                        mEntrance,
                        mPath,
                        ExposureEvent.createEventWithSourceConcat(
                            null,
                            mBasicExposureSource,
                            listOf(ExposureSource("游戏单", "${entity.title}+${entity.id}"))
                        )
                    )
                }
            }
        }
    }

    override fun getItemCount(): Int = mRecommendGameList.size

    class GameCollectionItemViewHolder(val binding: GamedetailItemGameCollectionBinding) :
        RecyclerView.ViewHolder(binding.root)
}