package com.ponzischeme89.memby.data

import androidx.compose.ui.graphics.Color
import com.ponzischeme89.memby.data.model.GatewayPalette
import com.ponzischeme89.memby.ui.theme.MembyPalette
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test

/**
 * The client's whole share of the theme rule: reading hex, and refusing to.
 *
 * There is deliberately no parallel to `themes_test.go` here, unlike the subtitle and intro
 * tests. Which theme is in force is decided in one place, on the gateway; this end only
 * paints what it is handed, and with no gateway it paints the default. A second copy of the
 * seasonal calendar would be a television that could disagree with the server about whether
 * it is Christmas.
 */
class ThemesTest {

    @Test
    fun `reads the alpha-first form the gateway writes`() {
        assertEquals(Color(0xFF52B54B), parseThemeColor("#FF52B54B"))
        assertEquals(Color(0x28FFFFFF), parseThemeColor("#28FFFFFF"))
    }

    @Test
    fun `six digits are opaque`() {
        // The form somebody hand-editing a palette reaches for, and the form Emby's own
        // artwork colours take. Read as fully opaque rather than fully transparent, which
        // is what a naive parse produces and what would draw nothing at all.
        assertEquals(Color(0xFF52B54B), parseThemeColor("52B54B"))
        assertEquals(Color(0xFF52B54B), parseThemeColor("#52b54b"))
    }

    @Test
    fun `anything unreadable is refused rather than guessed at`() {
        // Every one of these has to come back null so the caller keeps the app's own token
        // for that slot. A theme one colour wrong is a blemish; a screen drawn transparent
        // because of a typo is a television nobody can use.
        listOf(null, "", "#", "#FFF", "#GGGGGGGG", "#FF52B54B52", "rgb(1,2,3)", "  ")
            .forEach { assertNull("expected null for '$it'", parseThemeColor(it)) }
    }

    @Test
    fun `a palette keeps the current colour for anything the server did not send`() {
        val current = MembyPalette(accent = Color(0xFFAA0000), quietText = Color(0xFF445566))
        // A gateway that has grown a token this build does not send back, or one field with
        // a typo in it, must leave the rest of the scheme alone rather than resetting the
        // whole palette to the class defaults.
        val palette = GatewayPalette(surface = "#FF010203", accent = "not a colour")
            .toMembyPalette(fallback = current)

        assertEquals(Color(0xFF010203), palette.surface)
        assertEquals("an unreadable colour keeps the one in force", current.accent, palette.accent)
        assertEquals("an absent colour keeps the one in force", current.quietText, palette.quietText)
    }

    @Test
    fun `an empty palette changes nothing`() {
        // What a server predating themes effectively sends. It must be a no-op, not a repaint
        // into the defaults over whatever the viewer already had.
        val current = MembyPalette(accent = Color(0xFFAA0000))
        assertEquals(current, GatewayPalette().toMembyPalette(fallback = current))
    }
}
