package xyz.cottongin.radio247.util

import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertTrue
import org.junit.Test

class ArtworkGeneratorTest {

    @Test
    fun extractInitials_twoWords() {
        assertEquals("GS", ArtworkGenerator.extractInitials("Groove Salad"))
    }

    @Test
    fun extractInitials_threeWords() {
        assertEquals("DR", ArtworkGenerator.extractInitials("Drone Radio Station"))
    }

    @Test
    fun extractInitials_singleLongWord() {
        assertEquals("SO", ArtworkGenerator.extractInitials("SomaFM"))
    }

    @Test
    fun extractInitials_singleChar() {
        assertEquals("X", ArtworkGenerator.extractInitials("X"))
    }

    @Test
    fun extractInitials_blank() {
        assertEquals("?", ArtworkGenerator.extractInitials(""))
    }

    @Test
    fun extractInitials_whitespace() {
        assertEquals("?", ArtworkGenerator.extractInitials("   "))
    }

    @Test
    fun extractInitials_leadingTrailingSpaces() {
        assertEquals("GS", ArtworkGenerator.extractInitials("  Groove  Salad  "))
    }

    @Test
    fun extractInitials_lowercaseInput() {
        assertEquals("GS", ArtworkGenerator.extractInitials("groove salad"))
    }

    @Test
    fun pickColor_deterministic() {
        val c1 = ArtworkGenerator.pickColor("Groove Salad")
        val c2 = ArtworkGenerator.pickColor("Groove Salad")
        assertEquals(c1, c2)
    }

    @Test
    fun pickColor_differentNamesCanDiffer() {
        val c1 = ArtworkGenerator.pickColor("Groove Salad")
        val c2 = ArtworkGenerator.pickColor("Drone Zone")
        // Not guaranteed to differ (hash collisions) but the palette is broad enough
        // that two very different strings are likely to map to different colors.
        // This is a soft assertion -- we mainly verify no crash and determinism.
        assertTrue(c1 != 0)
        assertTrue(c2 != 0)
    }

    @Test
    fun pickColor_alwaysFromPalette() {
        val palette = intArrayOf(
            0xFF1E88E5.toInt(), 0xFF43A047.toInt(), 0xFFE53935.toInt(),
            0xFF8E24AA.toInt(), 0xFFFB8C00.toInt(), 0xFF00ACC1.toInt(),
            0xFF3949AB.toInt(), 0xFF7CB342.toInt(), 0xFFD81B60.toInt(),
            0xFF5E35B1.toInt(), 0xFF039BE5.toInt(), 0xFFC0CA33.toInt(),
        )
        val names = listOf(
            "Alpha", "Bravo", "Charlie", "Delta", "Echo",
            "Foxtrot", "Golf", "Hotel", "India", "Juliet",
            "Kilo", "Lima", "Mike", "November", "Oscar"
        )
        for (name in names) {
            assertTrue(
                "Color for '$name' should be in palette",
                ArtworkGenerator.pickColor(name) in palette
            )
        }
    }
}
