package club.clubk.ktag.apps.bletool

import androidx.compose.ui.graphics.Color
import club.clubk.ktag.apps.core.ui.theme.KTagBlue
import club.clubk.ktag.apps.core.ui.theme.KTagGreen
import club.clubk.ktag.apps.core.ui.theme.KTagPurple
import club.clubk.ktag.apps.core.ui.theme.KTagRed
import club.clubk.ktag.apps.core.ui.theme.KTagYellow

// Pastel versions of KTag colors for field highlighting
private val LightBlue = KTagBlue.copy(alpha = 0.25f)
private val LightGreen = KTagGreen.copy(alpha = 0.25f)
private val LightRed = KTagRed.copy(alpha = 0.25f)
private val LightYellow = KTagYellow.copy(alpha = 0.25f)
private val LightPurple = KTagPurple.copy(alpha = 0.25f)
private val LightGray = Color(0xFFF5F5F5)

object PacketFieldUtils {
    fun getFieldDescription(packetType: Byte, index: Int): String {
        return when (index) {
            0 -> "Length" // Always "Length"
            1 -> "Type" // Always "Type"
            2 -> "Mfg ID" // Always "Mfg ID"
            3 -> "Mfg ID" // Always "Mfg ID"
            4 -> "'K'" // Always "'K'"
            5 -> "'T'" // Always "'T'"
            6 -> "'a'" // Always "'a'"
            7 -> "'g'" // Always "'g'"
            8 -> when (packetType) {
                0x01.toByte() -> "Instigate Game"
                0x02.toByte() -> "Event"
                0x03.toByte() -> "Tag"
                0x04.toByte() -> "Console"
                0x05.toByte() -> "Status"
                0x06.toByte() -> "Parameters"
                0x07.toByte() -> "Hello"
                else -> "Packet Type"
            }

            9 -> "Event Number"
            else -> when (packetType) {
                0x01.toByte() -> when (index) { // Instigate Game packet fields
                    in 10..13 -> "Game Length (ms)"
                    in 14..17 -> "Time till Countdown (ms)"
                    else -> ""
                }

                0x02.toByte() -> when (index) { // Event packet fields
                    in 10..15 -> "Target Address"
                    in 16..19 -> "Event ID"
                    in 20..23 -> "Event Data"
                    else -> ""
                }

                0x03.toByte() -> when (index) { // Tag packet fields
                    10 -> "Tx Pwr (dBm)"
                    11 -> "Protocol"
                    12 -> "Team ID"
                    13 -> "Player ID"
                    14, 15 -> "Damage"
                    in 16..19 -> "Color"
                    in 20..25 -> "Target Address"
                    else -> ""
                }

                0x04.toByte() -> when (index) { // Console packet fields
                    in 10..30 -> "Console String"
                    else -> ""
                }

                0x05.toByte() -> when (index) { // Status packet fields
                    10 -> "Tx Pwr (dBm)"
                    11 -> "Protocol"
                    12 -> "Team ID"
                    13 -> "Player ID"
                    14, 15 -> "Health"
                    16, 17 -> "Max Health"
                    in 18..21 -> "Primary Color"
                    in 22..25 -> "Secondary Color"
                    26 -> "SystemK State"
                    else -> ""
                }

                0x06.toByte() -> when (index) { // Parameters packet fields
                    in 10..15 -> "Target Address"
                    16 -> "Subtype"
                    17, 18 -> "Key 1"
                    in 19..22 -> "Value 1"
                    23, 24 -> "Key 2"
                    in 25..28 -> "Value 2"
                    else -> ""
                }

                0x07.toByte() -> when (index) { // Hello packet fields
                    10 -> "SystemK Major Version"
                    11 -> "SystemK Minor Version"
                    12, 13 -> "Device Type"
                    14 -> "Team ID"
                    in 15..30 -> "Device Name"
                    else -> ""
                }

                else -> ""
            }
        }
    }

    fun getFieldColor(packetType: Byte, index: Int): Color? {
        // Header bytes always return null to use default header color
        if (index < 8) return null

        // Packet type byte is always a distinct color
        if (index == 8) return LightBlue

        // Event number is always the same color across all packet types
        if (index == 9) return LightGreen

        return when (packetType) {
            0x01.toByte() -> when (index) { // Instigate Game packet
                in 10..13 -> LightYellow // Game Length
                in 14..17 -> LightGreen // Time until Countdown
                else -> LightGray
            }

            0x02.toByte() -> when (index) { // Event packet
                in 10..15 -> LightBlue // Target Address
                in 16..19 -> LightPurple // Event ID
                in 20..23 -> LightGreen // Event Data
                else -> LightGray
            }

            0x03.toByte() -> when (index) { // Tag packet
                10 -> LightYellow // Tx Power
                11 -> LightGreen // Protocol
                12 -> LightRed // Team ID
                13 -> LightPurple // Player ID
                in 14..15 -> LightRed // Damage
                in 16..19 -> LightGreen // Color
                in 20..25 -> LightBlue // Target Address
                else -> LightGray
            }

            0x04.toByte() -> when (index) { // Console packet
                in 10..30 -> LightYellow // Console String
                else -> LightGray
            }

            0x05.toByte() -> when (index) { // Status packet
                10 -> LightYellow // Tx Power
                11 -> LightGreen // Protocol
                12 -> LightRed // Team ID
                13 -> LightPurple // Player ID
                14, 15 -> LightRed // Health
                16, 17 -> LightGreen // Maximum Health
                in 18..21 -> LightBlue // Primary Color
                in 22..25 -> LightPurple // Secondary Color
                26 -> LightYellow // SystemK State
                else -> LightGray
            }

            0x06.toByte() -> when (index) { // Parameters packet
                in 10..15 -> LightBlue // Target Address
                16 -> LightYellow // Subtype
                17, 18 -> LightPurple // Key 1
                in 19..22 -> LightGreen // Value 1
                23, 24 -> LightPurple // Key 2
                in 25..28 -> LightGreen // Value 2
                else -> LightGray
            }

            0x07.toByte() -> when (index) { // Hello packet
                10 -> LightYellow // SystemK Major Version
                11 -> LightGreen // SystemK Minor Version
                12, 13 -> LightBlue // Device Type
                14 -> LightRed // Team ID
                in 15..30 -> LightPurple // Device Name
                else -> LightGray
            }

            else -> LightGray
        }
    }
}
