package com.gh.gamecenter.servers.gametest2

import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.DirectUtils
import com.gh.common.util.DownloadItemUtils
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.adapter.viewholder.GameViewHolder
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.view.DrawableView
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.databinding.ItemGameServerTestV2Binding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.entity.PageLocation
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.gh.gamecenter.feature.game.GameItemViewHolder

class GameServerTestV2ItemViewHolder(val binding: ItemGameServerTestV2Binding) :
    BaseRecyclerViewHolder<Any>(binding.root) {
    fun bindAttachGame(
        holder: GameServerTestV2ItemViewHolder,
        gameEntity: GameEntity,
        adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder>,
        entrance: String,
        exposureEvent: ExposureEvent?,
        location: String,
        linkText: String,
        pageLocation: PageLocation
    ) {
        holder.bindGameInfo(gameEntity, adapter, exposureEvent, entrance, location, linkText, pageLocation)

        holder.itemView.setOnClickListener {
            GameDetailActivity.startGameDetailActivity(
                binding.root.context,
                gameEntity.id,
                "(${entrance}-游戏[" + gameEntity.name + "])",
                exposureEvent
            )
            SensorsBridge.trackColumnTestClick(
                location = location,
                recommendType = linkText,
                text = "游戏",
                bottomTab = pageLocation.bottomTab,
                multiTabName = pageLocation.severalTabPageName,
                multiTabId = pageLocation.severalTabPageId,
                position = pageLocation.tabPosition,
                tabContent = pageLocation.tabContent,
                gameId = gameEntity.id,
                gameName = gameEntity.name ?: ""
            )
        }
    }

    private fun bindGameInfo(
        gameEntity: GameEntity,
        adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder>,
        exposureEvent: ExposureEvent?,
        entrance: String,
        location: String,
        linkText: String,
        pageLocation: PageLocation
    ) {
        binding.root.setCardBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(binding.root.context))
        binding.container.background = com.gh.gamecenter.common.R.drawable.reuse_listview_item_style.toDrawable(binding.root.context)
        binding.infoContainer.background = com.gh.gamecenter.common.R.drawable.reuse_listview_item_style.toDrawable(binding.root.context)
        binding.gameIcon.displayGameIcon(gameEntity)
        binding.gameName.text = gameEntity.name
        val time = if (gameEntity.test?.startPending == true) {
            gameEntity.test?.startText ?: ""
        } else {
            TimeUtils.getGameServerTestTransformedTime(gameEntity.time?.time ?: 0L)
        }
        binding.gameBrief.text = if (gameEntity.eventType == "appointment") gameEntity.brief else "$time · ${gameEntity.brief}"
        binding.gameName.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(binding.root.context))
        binding.gameBrief.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(binding.root.context))
        val color: Int
        when (gameEntity.eventType) {
            "test" -> {
                color = com.gh.gamecenter.common.R.color.primary_theme_50
                binding.statusTv.background = DrawableView.getStrokeDrawable(color, 1F, 3F.dip2px().toFloat())
                binding.statusTv.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(binding.statusTv.context))
                binding.statusTv.text = "测试"
            }
            "first" -> {
                color = com.gh.gamecenter.common.R.color.primary_theme_50
                binding.statusTv.background = DrawableView.getStrokeDrawable(color, 1F, 3F.dip2px().toFloat())
                binding.statusTv.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(binding.statusTv.context))
                binding.statusTv.text = "首发"
            }
            "update" -> {
                color = com.gh.gamecenter.common.R.color.primary_theme_50
                binding.statusTv.background = DrawableView.getStrokeDrawable(color, 1F, 3F.dip2px().toFloat())
                binding.statusTv.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(binding.statusTv.context))
                binding.statusTv.text = "更新"
            }
            "appointment" -> {
                color = com.gh.gamecenter.common.R.color.secondary_yellow_50
                binding.statusTv.background = DrawableView.getStrokeDrawable(color, 1F, 3F.dip2px().toFloat())
                binding.statusTv.setTextColor(com.gh.gamecenter.common.R.color.secondary_yellow.toColor(binding.statusTv.context))
                binding.statusTv.text = "预约"
            }
            else -> {
                //Do nothing
            }
        }
        binding.container.setPadding(0, 0, 0, if (gameEntity.articleData == null) 12F.dip2px() else 0)
        binding.infoGroup.goneIf(gameEntity.articleData == null) {
            binding.cardDashLineView.setDashLineColor(com.gh.gamecenter.common.R.color.ui_divider.toColor(binding.root.context))
            binding.leftCardDividerIv.setImageResource(R.drawable.card_divider_left)
            binding.rightCardDividerIv.setImageResource(R.drawable.card_divider_right)
            binding.infoTv.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(binding.root.context))
            binding.arrowIv.setImageResource(R.drawable.ic_auxiliary_arrow_right_8)
            gameEntity.articleData?.let {
                binding.infoTv.text = it.title
                binding.infoContainer.setOnClickListener { _ ->
                    it.link?.let { link ->
                        DirectUtils.directToLinkPage(
                            binding.root.context,
                            link,
                            "新游开测",
                            "",
                            ""
                        )
                        SensorsBridge.trackColumnTestClick(
                            location = location,
                            recommendType = linkText,
                            text = "资讯信息",
                            bottomTab = pageLocation.bottomTab,
                            multiTabName = pageLocation.severalTabPageName,
                            multiTabId = pageLocation.severalTabPageId,
                            position = pageLocation.tabPosition,
                            tabContent = pageLocation.tabContent,
                            gameId = gameEntity.id,
                            gameName = gameEntity.name ?: "",
                            linkType = link.type,
                            linkId = link.link,
                            linkText = link.text
                        )
                    }
                }
            }
        }
        GameItemViewHolder.initGameSubtitleAndAdLabel(gameEntity, binding.gameSubtitleTv)

        DownloadItemUtils.setOnClickListener(
            binding.root.context,
            binding.downloadBtn,
            gameEntity,
            position,
            adapter,
            entrance,
            location = "",
            traceEvent = exposureEvent
        ) {
            SensorsBridge.trackColumnTestClick(
                location = location,
                recommendType = linkText,
                text = "按钮",
                bottomTab = pageLocation.bottomTab,
                multiTabName = pageLocation.severalTabPageName,
                multiTabId = pageLocation.severalTabPageId,
                position = pageLocation.tabPosition,
                tabContent = pageLocation.tabContent,
                gameId = gameEntity.id,
                gameName = gameEntity.name ?: ""
            )
        }
        DownloadItemUtils.updateItem(
            binding.root.context,
            gameEntity,
            GameViewHolder(binding.root).apply {
                gameDownloadBtn = binding.downloadBtn
                multiVersionDownloadTv = binding.multiVersionDownloadTv
                gameDownloadTips = binding.downloadTipsLottie
            }
        )

        ConstraintSet().apply {
            clone(binding.gameNameContainer.parent as ConstraintLayout)
            clear(binding.gameNameContainer.id, ConstraintSet.END)
            if (binding.downloadBtn.visibility == View.VISIBLE) {
                connect(binding.gameNameContainer.id, ConstraintSet.END, binding.downloadBtn.id, ConstraintSet.START)
            } else {
                connect(binding.gameNameContainer.id, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)
            }
            setMargin(binding.gameNameContainer.id, ConstraintSet.END, 16F.dip2px())
            applyTo(binding.gameNameContainer.parent as ConstraintLayout)
        }

    }
}