package com.swoosh.microblog.ui.theme

import android.app.Application
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(application = Application::class)
class ThemePreferencesTest {

    private lateinit var context: Context
    private lateinit var themePreferences: ThemePreferences

    @Before
    fun setUp() {
        context = ApplicationProvider.getApplicationContext()
        // Clear prefs before each test
        context.getSharedPreferences(ThemePreferences.PREFS_NAME, Context.MODE_PRIVATE)
            .edit().clear().commit()
        themePreferences = ThemePreferences(context)
    }

    @Test
    fun `default theme mode is SYSTEM`() {
        assertEquals(ThemeMode.SYSTEM, themePreferences.themeMode)
    }

    @Test
    fun `set and get LIGHT mode`() {
        themePreferences.themeMode = ThemeMode.LIGHT
        assertEquals(ThemeMode.LIGHT, themePreferences.themeMode)
    }

    @Test
    fun `set and get DARK mode`() {
        themePreferences.themeMode = ThemeMode.DARK
        assertEquals(ThemeMode.DARK, themePreferences.themeMode)
    }

    @Test
    fun `set and get SYSTEM mode`() {
        themePreferences.themeMode = ThemeMode.DARK
        themePreferences.themeMode = ThemeMode.SYSTEM
        assertEquals(ThemeMode.SYSTEM, themePreferences.themeMode)
    }

    @Test
    fun `persists across instances`() {
        themePreferences.themeMode = ThemeMode.DARK
        val newInstance = ThemePreferences(context)
        assertEquals(ThemeMode.DARK, newInstance.themeMode)
    }

    @Test
    fun `overwriting mode replaces previous value`() {
        themePreferences.themeMode = ThemeMode.LIGHT
        themePreferences.themeMode = ThemeMode.DARK
        assertEquals(ThemeMode.DARK, themePreferences.themeMode)
    }

    @Test
    fun `invalid stored value falls back to SYSTEM`() {
        // Write an invalid value directly to SharedPreferences
        context.getSharedPreferences(ThemePreferences.PREFS_NAME, Context.MODE_PRIVATE)
            .edit().putString(ThemePreferences.KEY_THEME_MODE, "INVALID_VALUE").commit()
        val prefs = ThemePreferences(context)
        assertEquals(ThemeMode.SYSTEM, prefs.themeMode)
    }

    @Test
    fun `null stored value falls back to SYSTEM`() {
        // Remove the key from SharedPreferences
        context.getSharedPreferences(ThemePreferences.PREFS_NAME, Context.MODE_PRIVATE)
            .edit().remove(ThemePreferences.KEY_THEME_MODE).commit()
        val prefs = ThemePreferences(context)
        assertEquals(ThemeMode.SYSTEM, prefs.themeMode)
    }

    @Test
    fun `all modes can be round-tripped`() {
        for (mode in ThemeMode.values()) {
            themePreferences.themeMode = mode
            assertEquals(mode, themePreferences.themeMode)
        }
    }
}
