package com.gh.gamecenter.game

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.text.TextUtils
import android.view.View
import android.widget.Space
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import com.gh.common.databind.BindingAdapters
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.view.DrawableView
import com.gh.gamecenter.databinding.GameItemBinding
import com.gh.gamecenter.entity.GameEntity

class GameItemViewHolder(var binding: GameItemBinding) : BaseRecyclerViewHolder<Any>(binding.root) {

    fun initServerType(gameEntity: GameEntity) {
        initServerType(binding.gameName, binding.gameKaifuType, gameEntity)
    }

    fun bindGameItem(
        entity: GameEntity,
        isShowSuffix: Boolean = true,
        briefStyle: String = "",
        isShowPlatform: Boolean = false,
        forceShowSubtitle: Boolean = false
    ) {
        binding.run {
            root.background = R.drawable.reuse_listview_item_style.toDrawable(root.context)
            selectIv.setImageDrawable(DrawableView.getCheckSelectorDrawable(root.context))
            gameKaifuType.setBackgroundColor(R.color.theme.toColor(root.context))
            gameName.setTextColor(R.color.text_title.toColor(root.context))
            gameDes.setTextColor(R.color.text_subtitleDesc.toColor(root.context))
            recommendStarPref.setTextColor(R.color.text_subtitleDesc.toColor(root.context))

            gameIconView.displayGameIcon(entity)
            BindingAdapters.setGameName(gameName, entity, isShowPlatform, isShowSuffix)
            BindingAdapters.setTextSize(gameRating, if (entity.commentCount > 3) 12 else 10)
            BindingAdapters.setGameTags(labelList, entity)
            gameRating.setDrawableStart(if (entity.commentCount > 3) R.drawable.game_horizontal_rating.toDrawable() else null)
            gameRating.setPadding(0, 0, if (entity.commentCount > 3) 8F.dip2px() else 0, 0)
            gameRating.text = if (entity.commentCount > 3) {
                if (entity.star == 10.0F) "10" else entity.star.toString()
            } else ""
            gameRating.setTextColor(
                if (entity.commentCount > 3) R.color.theme_font.toColor(root.context) else R.color.theme.toColor(
                    root.context
                )
            )
            gameDes.text = entity.decoratedDes
            recommendStar.rating = entity.recommendStar.toFloat()
            initGameSubtitle(entity, gameSubtitleTv, gameDesSpace, root, forceShowSubtitle)
            adLabelTv.goneIf(!entity.adIconActive)
            ConstraintSet().apply {
                clone(root)
                connect(
                    R.id.adLabelTv,
                    ConstraintSet.START,
                    if (entity.adIconActive && entity.serverLabel != null && !entity.advanceDownload) R.id.game_kaifu_type else R.id.gameSubtitleTv,
                    ConstraintSet.END
                )
            }.applyTo(root)
        }
    }

    companion object {
        fun initServerType(gameNameTv: TextView, serverTypeTv: TextView, gameEntity: GameEntity) {
            val serverLabel = gameEntity.serverLabel
            when {
                gameEntity.test != null -> {
                    serverTypeTv.visibility = View.GONE
                    serverTypeTv.text = ""
                }
                serverLabel != null && !gameEntity.advanceDownload -> {
                    serverTypeTv.visibility = View.VISIBLE
                    serverTypeTv.text = serverLabel.value
                    serverTypeTv.setTextColor(Color.WHITE)
                    if (gameEntity.isUseDefaultServerStyle()) {
                        serverTypeTv.background =
                            R.drawable.server_label_default_bg.toDrawable(gameNameTv.context)
                        serverTypeTv.setTextColor(R.color.text_server_label.toColor(gameNameTv.context))
                    } else {
                        serverTypeTv.background = DrawableView.getServerDrawable(serverLabel.color)
                        serverTypeTv.setTextColor(R.color.white.toColor(gameNameTv.context))
                    }
                }
                else -> serverTypeTv.visibility = View.GONE
            }

            // 由于RecyclerView的复用机制 需要每次测量gameName的宽
            gameNameTv.requestLayout()
        }

        @JvmStatic
        fun initGameSubtitle(
            entity: GameEntity,
            gameSubtitleTv: TextView,
            gameDesSpace: Space? = null,
            rootContainer: ConstraintLayout? = null,
            forceShowSubtitle: Boolean = false,
            modificationCallback: (() -> Unit)? = null
        ) {
            gameSubtitleTv.visibility = View.GONE
            if ((entity.serverLabel == null && entity.subtitle.isNotEmpty() && !entity.advanceDownload) || (forceShowSubtitle && entity.subtitle.isNotEmpty())) {
                gameSubtitleTv.run {
                    visibility = View.VISIBLE
                    text = entity.subtitle
                    if (entity.subtitleStyle != null && !TextUtils.isEmpty(entity.subtitleStyle?.color)) {
                        setTextColor("#${entity.subtitleStyle?.color}".hexStringToIntColor())
                        background = GradientDrawable().apply {
                            cornerRadius = 2F.dip2px().toFloat()
                            if (entity.subtitleStyle?.style == "border") {
                                setColor(Color.TRANSPARENT)
                                setStroke(0.5F.dip2px(), "#${entity.subtitleStyle?.background}".hexStringToIntColor())
                            } else {
                                shape = GradientDrawable.RECTANGLE
                                setColor("#${entity.subtitleStyle?.background}".hexStringToIntColor())
                            }
                        }
                    }
                }
                gameDesSpace?.post {
                    // 模拟器上可能出现未提前 bindView 导致 gameDesSpace.width 为 0 的情况
                    gameSubtitleTv.maxWidthExcludeZero(if (entity.adIconActive) gameDesSpace.width - 25F.dip2px() else gameDesSpace.width)
                }
                modificationCallback?.invoke()
            }
            if (entity.advanceDownload) {
                gameSubtitleTv.apply {
                    visibility = View.VISIBLE
                    text = "预下载"
                    setTextColor(R.color.text_subtitle.toColor(context))
                    background = R.drawable.bg_advance_download_game_subtitle.toDrawable(context)
                }
            }
            rootContainer?.let {
                ConstraintSet().apply {
                    clone(rootContainer)
                    connect(R.id.game_name, ConstraintSet.END, if (entity.serverLabel != null && !entity.advanceDownload && !forceShowSubtitle) R.id.recent_played_tag else R.id.gameSubtitleTv, ConstraintSet.START)
                }.applyTo(rootContainer)
            }
        }
    }

}