package com.gh.gamecenter.home.custom.adapter

import android.content.Context
import android.widget.TextView
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toResString
import com.gh.gamecenter.common.utils.visibleIf
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.feature.entity.GameEntity
import kotlin.math.roundToInt

object CustomViewExt {

    fun setGameRatting(tvRatting: TextView, briefStyle: String, game: GameEntity) {
        tvRatting.goneIf(!(game.commentCount > 3 && briefStyle == "star&brief")) {
            tvRatting.text = if (game.star == 10.0f) {
                "10"
            } else {
                "${game.star}"
            }
        }
    }

    fun setGameRattingWithSubject(tvRatting: TextView, showStar: Boolean, game: GameEntity) {
        if (showStar) {
            tvRatting.visibleIf(game.commentCount > 3) {
                tvRatting.text = if (game.star == 10.0f) {
                    "10"
                } else {
                    "${game.star}"
                }
            }
        } else {
            tvRatting.goneIf(true)
        }
    }

    fun setDescription(tvDesc: TextView, briefStyle: String, game: GameEntity) {
        fun getBriefWithSize(): String {
            val size = game.getApk().firstOrNull()?.size ?: ""
            return if (size.isNotBlank()) {
                "$size  ${game.brief ?: ""}"
            } else {
                game.brief ?: ""
            }
        }

        val brief = when (briefStyle) {
            "size&brief" -> getBriefWithSize()
            "star&brief" -> game.brief
            "recommend" -> game.columnRecommend?.text ?: getBriefWithSize()
            "test&appointment" -> getBriefWithTestAndAppointment(game, tvDesc.context)

            else -> getBriefWithSize()
        }
        tvDesc.text = brief
    }

    private fun getBriefWithTestAndAppointment(game: GameEntity, context: Context) =
        if (game.test?.startPending == true) {
            val startText = game.test?.startText ?: ""
            if (startText.isBlank()) {
                getBookVolumeText(game.appointmentCount, context, false)
            } else {
                "$startText${getBookVolumeText(game.appointmentCount, context, true)}"
            }
        } else {
            "${getTestDescription(game)}${getBookVolumeText(game.appointmentCount, context, true)}"
        }

    private fun getTestDescription(game: GameEntity): String {
        val timeText = TimeUtils.formatTestTime(game.test?.start ?: 0L)
        val eventName = if (game.test?.type == "删档内测") {
            R.string.delete_test.toResString()
        } else {
            R.string.go_live.toResString()
        }
        return "$timeText$eventName"
    }

    private fun getBookVolumeText(number: Int, context: Context, showDivider: Boolean): String =
        when {
            number == 0 -> ""
            number <= 99999 -> context.getString(
                R.string.number_of_reservations,
                if (showDivider) " | $number" else "$number"
            )

            else -> context.getString(
                R.string.number_of_reservations,
                if (showDivider) " | ${(number / 10000F).roundToInt()}W" else "${(number / 10000F).roundToInt()}W"
            )

        }

}