package com.gh.gamecenter.gamedetail.detail.viewholder

import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.setDrawableEnd
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.databinding.ItemGameDetailRecyclerViewBinding
import com.gh.gamecenter.feature.view.DownloadButton
import com.gh.gamecenter.gamedetail.GameDetailViewModel
import com.gh.gamecenter.gamedetail.detail.adapter.GameCollectionAdapter
import com.gh.gamecenter.gamedetail.entity.GameDetailData

class GameDetailRecommendGameCollectionItemViewHolder(
    val binding: ItemGameDetailRecyclerViewBinding,
    downloadBtn: DownloadButton?,
    viewModel: GameDetailViewModel
) : BaseGameDetailItemViewHolder(binding.root, downloadBtn, viewModel) {
    override fun bindView(data: GameDetailData) {
        super.bindView(data)
        val gameCollectionList = data.linkRecommendGameList ?: return
        binding.run {
            titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
            moreTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(context))
            moreTv.setDrawableEnd(com.gh.gamecenter.common.R.drawable.ic_auxiliary_arrow_right_12.toDrawable(context)?.apply {
                colorFilter = PorterDuffColorFilter(com.gh.gamecenter.common.R.color.text_tertiary.toColor(context), PorterDuff.Mode.SRC_ATOP)
            })
            titleTv.text = "游戏单推荐"
            moreTv.isVisible = true
            moreTv.text = "游戏单广场"
            moreTv.setOnClickListener {
                NewFlatLogUtils.logGameDetailGameListRecommendSquareJump(viewModel.game?.name ?: "", viewModel.game?.id ?: "")
                SensorsBridge.trackGameDetailModuleClick(
                    gameId,
                    gameName,
                    gameType,
                    "右上角",
                    moduleType,
                    "游戏单推荐",
                    sequence,
                    gameStatus = gameStatus
                )
                DirectUtils.directToGameCollectionSquare(it.context, viewModel.entrance ?: "")
            }

            if (recyclerView.adapter !is GameCollectionAdapter) {
                recyclerView.run {
                    isNestedScrollingEnabled = false
                    (itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
                    layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
                    adapter = GameCollectionAdapter(
                        gameCollectionList,
                        viewModel.game?.id ?: "",
                        viewModel.game?.name ?: "",
                        viewModel.game,
                        viewModel.entrance ?: "",
                        "游戏详情[${viewModel.game?.name ?: ""}]:游戏单推荐",
                        listOf(ExposureSource("游戏详情", "${viewModel.game?.name ?: ""}+${viewModel.game?.id ?: ""}")),
                        baseTrackData.apply { moduleName = "游戏单推荐" }) { gameStatus }
                }
            } else {
                recyclerView.adapter?.run {
                    notifyItemRangeChanged(0, itemCount)
                }
            }
        }
    }
}