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

import androidx.core.view.isVisible
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.ItemGameDetailDrawerBinding
import com.gh.gamecenter.databinding.SubItemGameDetailDrawerBinding
import com.gh.gamecenter.feature.view.DownloadButton
import com.gh.gamecenter.gamedetail.GameDetailViewModel
import com.gh.gamecenter.gamedetail.entity.GameDetailData
import com.gh.gamecenter.gamedetail.entity.GameDetailLink

class GameDetailDrawerItemViewHolder(
    val binding: ItemGameDetailDrawerBinding,
    downloadBtn: DownloadButton?,
    viewModel: GameDetailViewModel
) : BaseGameDetailItemViewHolder(binding.root, downloadBtn, viewModel) {

    override fun bindView(data: GameDetailData) {
        super.bindView(data)
        val entity = data.linkDrawer ?: return
        binding.root.setPadding(16F.dip2px(), if (entity.showName) 0 else 8F.dip2px(), 16F.dip2px(), 8F.dip2px())
        binding.drawer2Container.root.isVisible = entity.data.size > 1
        binding.dividerContainer.isVisible = entity.data.size > 1
        binding.dividerContainer.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_container_1.toColor(context))
        binding.divider.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_divider.toColor(context))
        binding.titleTv.goneIf(!entity.showName) {
            binding.titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
            binding.titleTv.text = entity.name
        }
        entity.data.getOrNull(0)?.let {
            binding.drawer1Container.root.background = if (entity.data.size == 1) {
                R.drawable.bg_game_detail_drawer_item_style.toDrawable(context)
            } else {
                R.drawable.bg_game_detail_drawer_left_item_style.toDrawable(context)
            }
            bindSubView(binding.drawer1Container, it, 1)
        }
        entity.data.getOrNull(1)?.let {
            binding.drawer2Container.root.background = R.drawable.bg_game_detail_drawer_right_item_style.toDrawable(context)
            bindSubView(binding.drawer2Container, it, 2)
        }
    }

    private fun bindSubView(subBinding: SubItemGameDetailDrawerBinding, data: GameDetailLink, supSequence: Int) {
        subBinding.run {
            iconIv.goneIf(data.componentIcon?.icon.isNullOrEmpty()) {
                ImageUtils.display(iconIv, data.componentIcon?.icon)
                iconIv.updateRoundingParams {
                    setOverlayColor(com.gh.gamecenter.common.R.color.ui_container_1.toColor(context))
                }
            }
            contentTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(context))
            contentTv.text = data.text
            arrowIv.goneIf(data.link == null) {
                arrowIv.setImageResource(com.gh.gamecenter.common.R.drawable.ic_auxiliary_arrow_right_12)
            }
            root.setOnClickListener { _ ->
                data.link?.let {
                    DirectUtils.directToLinkPage(context, it, "游戏详情-抽屉列表", "")
                    SensorsBridge.trackGameDetailModuleClick(
                        gameId,
                        gameName,
                        gameType,
                        "组件内容",
                        moduleType,
                        itemData?.linkDrawer?.name,
                        sequence,
                        data.text,
                        supSequence,
                        it.type,
                        it.link,
                        it.text,
                        gameStatus
                    )
                }
            }
        }
    }
}