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

import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.gh.gamecenter.R
import com.gh.gamecenter.ShellActivity
import com.gh.gamecenter.ShellActivity.Type
import com.gh.gamecenter.common.utils.*
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.GameLibaoAdapter
import com.gh.gamecenter.gamedetail.entity.GameDetailData
import com.gh.gamecenter.gamedetail.entity.GameDetailTabEntity
import com.gh.gamecenter.gamedetail.libao.LibaoListFragment

class GameDetailGiftItemViewHolder(
    val binding: ItemGameDetailRecyclerViewBinding,
    downloadBtn: DownloadButton?,
    viewModel: GameDetailViewModel
) : BaseGameDetailItemViewHolder(binding.root, downloadBtn, viewModel) {
    override fun bindView(data: GameDetailData) {
        super.bindView(data)
        val giftList = data.linkLibao ?: return
        binding.run {
            titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
            moreTv.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(context))
            moreTv.setDrawableEnd(R.drawable.ic_auxiliary_arrow_right_text_theme_12.toDrawable(context))
            titleTv.text = "游戏礼包"
            moreTv.goneIf(data.libaoCount <= 3) {
                moreTv.text = "更多"
                moreTv.setOnClickListener {
                    SensorsBridge.trackGameDetailModuleClick(
                        gameId,
                        gameName,
                        gameType,
                        "右上角",
                        moduleType,
                        "游戏礼包",
                        sequence,
                        gameStatus = gameStatus
                    )

                    val tabList = viewModel.gameDetailTabListLiveData.value?.data
                    if (tabList?.find { it.type == GameDetailTabEntity.TYPE_GIFT } != null) {
                        viewModel.performTabSelected(GameDetailTabEntity.TYPE_GIFT)
                    } else {
                        val bundle = LibaoListFragment.getBundle(viewModel.game, false)
                        context.startActivity(ShellActivity.getIntent(context, Type.SIMPLE_LIBAO_LIST, bundle))
                    }
                }
            }

            if (recyclerView.adapter !is GameLibaoAdapter) {
                recyclerView.isNestedScrollingEnabled = false
                (recyclerView.itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
                recyclerView.layoutManager = LinearLayoutManager(context)
                recyclerView.adapter =
                    GameLibaoAdapter(context, giftList, viewModel.game?.name ?: "", viewModel.game?.id ?: "", true,
                        baseTrackData.apply {
                            moduleName = "游戏礼包"
                        }) { gameStatus }
            } else {
                (recyclerView.adapter as GameLibaoAdapter).run {
                    libaos = giftList
                    notifyItemRangeChanged(0, itemCount)
                }
            }
        }
    }
}