package com.gh.gamecenter.entity

import android.graphics.Color
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.feature.entity.Time
import com.google.gson.annotations.SerializedName

data class GameNavigationWrapper(
    var data: List<GameNavigationEntity> = arrayListOf(),
    var version: String = ""
)

data class GameNavigationEntity(
    @SerializedName("_id")
    var id: String = "",
    @SerializedName("link_type")
    var linkEntity: LinkEntity? = null,
    @SerializedName("entry_name")
    var entryName: String = "",
    var image: String = "",
    var hint: Boolean = false,
    @SerializedName("hint_start_time")
    var hintStartTime: Long = 0,
    @SerializedName("hint_end_time")
    var hintEndTime: Long = 0,
    var time: Time? = null,
    @SerializedName("entry_name_status")
    private val _entryNameStatus: String? = null,
    @SerializedName("guide")
    private val _guide: Guide? = null
) {

    val isShowEntryName: Boolean
        get() = ENTRY_NAME_STATUS_HIDE != _entryNameStatus

    val isShowBubble: Boolean
        get() = _guide != null

    val guide: Guide
        get() = _guide ?: Guide()

    data class Guide(
        @SerializedName("text")
        private val _text: String? = null,
        @SerializedName("color")
        private val _color: String? = null
    ) {

        val text: String
            get() = _text ?: ""

        val color: String
            get() = _color ?: ""

        /**
         * 文字颜色 不透明度为 70%
         *  十六进制: B2
         * 十进制: 178
         */
        val textColorInt: Int
            get() = parseColorWithAlpha(TEXT_ALPHA, color)

        /**
         * 边框颜色 不透明度为 10%
         * 十六进制: 4D
         * 十进制: 77
         */
        val borderColorInt: Int
            get() = parseColorWithAlpha(BORDER_ALPHA, color)

        /**
         * 背景颜色 不透明度为 30%
         * 十六进制: 1A
         * 十进制: 26
         */
        val backgroundColorInt: Int
            get() = parseColorWithAlpha(BACKGROUND_ALPHA, color)


        private fun parseColorWithAlpha(alpha: Int, color: String): Int {
            val colorInt = try {
                Color.parseColor(color)
            } catch (e: IllegalArgumentException) {
                // 解析失败，默认使用红色
                Color.RED
            }
            val hsv = FloatArray(3)
            Color.colorToHSV(colorInt, hsv)
            return Color.HSVToColor(alpha, hsv)
        }

        companion object {
            private const val COLOR_BLUE = "blue"
            private const val COLOR_PURPLE = "purple"
            private const val COLOR_RED = "red"
            private const val COLOR_ORANGE = "orange"
            private const val COLOR_YELLOW = "yellow"
            private const val COLOR_GREEN = "green"

            private const val TEXT_ALPHA = 178
            private const val BORDER_ALPHA = 77
            private const val BACKGROUND_ALPHA = 26
        }
    }

    companion object {
        private const val ENTRY_NAME_STATUS_HIDE = "hide"
    }
}
