package com.rosetta.messenger.ui.settings

import androidx.annotation.DrawableRes
import com.rosetta.messenger.R

data class ThemeWallpaper(
    val id: String,
    val name: String,
    val preferredTheme: WallpaperTheme,
    val pairGroup: String,
    @DrawableRes val drawableRes: Int
)

enum class WallpaperTheme {
    DARK,
    LIGHT
}

object ThemeWallpapers {
    // Desktop parity: keep the same order/mapping as desktop WALLPAPERS.
    val all: List<ThemeWallpaper> =
        listOf(
            ThemeWallpaper(
                id = "light_01",
                name = "Light 1",
                preferredTheme = WallpaperTheme.LIGHT,
                pairGroup = "pair_1",
                drawableRes = R.drawable.wallpaper_light_01
            ),
            ThemeWallpaper(
                id = "light_02",
                name = "Light 2",
                preferredTheme = WallpaperTheme.LIGHT,
                pairGroup = "pair_2",
                drawableRes = R.drawable.wallpaper_light_02
            ),
            ThemeWallpaper(
                id = "light_03",
                name = "Light 3",
                preferredTheme = WallpaperTheme.LIGHT,
                pairGroup = "pair_3",
                drawableRes = R.drawable.wallpaper_light_03
            ),
            ThemeWallpaper(
                id = "dark_01",
                name = "Dark 1",
                preferredTheme = WallpaperTheme.DARK,
                pairGroup = "pair_1",
                drawableRes = R.drawable.wallpaper_dark_01
            ),
            ThemeWallpaper(
                id = "dark_02",
                name = "Dark 2",
                preferredTheme = WallpaperTheme.DARK,
                pairGroup = "pair_2",
                drawableRes = R.drawable.wallpaper_dark_02
            ),
            ThemeWallpaper(
                id = "dark_03",
                name = "Dark 3",
                preferredTheme = WallpaperTheme.DARK,
                pairGroup = "pair_3",
                drawableRes = R.drawable.wallpaper_dark_03
            )
        )

    private val byId: Map<String, ThemeWallpaper> = all.associateBy { it.id }
    private val byPairGroup: Map<String, List<ThemeWallpaper>> = all.groupBy { it.pairGroup }

    fun findById(id: String): ThemeWallpaper? = byId[id]

    fun forTheme(isDarkTheme: Boolean): List<ThemeWallpaper> {
        val targetTheme = if (isDarkTheme) WallpaperTheme.DARK else WallpaperTheme.LIGHT
        return all.filter { it.preferredTheme == targetTheme }
    }

    fun mapToTheme(wallpaperId: String, isDarkTheme: Boolean): String {
        if (wallpaperId.isBlank()) return ""

        val targetTheme = if (isDarkTheme) WallpaperTheme.DARK else WallpaperTheme.LIGHT
        val wallpaper = findById(wallpaperId) ?: return wallpaperId
        if (wallpaper.preferredTheme == targetTheme) return wallpaperId

        val fromPair =
            byPairGroup[wallpaper.pairGroup]
                ?.firstOrNull { it.preferredTheme == targetTheme }
                ?.id
        if (!fromPair.isNullOrBlank()) return fromPair

        return all.firstOrNull { it.preferredTheme == targetTheme }?.id ?: wallpaperId
    }

    fun resolveWallpaperForTheme(
        currentWallpaperId: String,
        isDarkTheme: Boolean,
        darkThemeWallpaperId: String,
        lightThemeWallpaperId: String
    ): String {
        val savedForTargetTheme = if (isDarkTheme) darkThemeWallpaperId else lightThemeWallpaperId
        val mappedSaved = mapToTheme(savedForTargetTheme, isDarkTheme)
        if (mappedSaved.isNotBlank()) return mappedSaved
        return mapToTheme(currentWallpaperId, isDarkTheme)
    }

    @DrawableRes
    fun drawableResOrNull(id: String): Int? = findById(id)?.drawableRes
}
