package com.applemand.zmanim

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Surface
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import com.applemand.zmanim.core.design.LuachTheme
import com.applemand.zmanim.core.design.ThemeMode
import com.applemand.zmanim.core.design.UiLanguage
import com.applemand.zmanim.core.settings.SettingsRepository
import com.applemand.zmanim.core.settings.proto.Language
import com.applemand.zmanim.core.settings.proto.ThemeMode as ProtoThemeMode
import com.applemand.zmanim.nav.LuachNavHost
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    @Inject lateinit var settingsRepository: SettingsRepository

    override fun onCreate(savedInstanceState: Bundle?) {
        val splash = installSplashScreen()
        super.onCreate(savedInstanceState)

        enableEdgeToEdge(
            statusBarStyle = SystemBarStyle.auto(Color.Transparent.hashCode(), Color.Transparent.hashCode()),
        )
        splash.setKeepOnScreenCondition { false }

        setContent {
            val settings by settingsRepository.settings.collectAsState(initial = null)

            val themeMode = when (settings?.themeMode) {
                ProtoThemeMode.THEME_DARK -> ThemeMode.DARK
                ProtoThemeMode.THEME_LIGHT -> ThemeMode.LIGHT
                else -> ThemeMode.SYSTEM
            }
            val uiLang = when (settings?.uiLanguage) {
                Language.LANGUAGE_HEBREW -> UiLanguage.HEBREW
                Language.LANGUAGE_ENGLISH -> UiLanguage.ENGLISH
                else -> UiLanguage.SYSTEM
            }
            val dynamic = settings?.useDynamicColor ?: false

            LuachTheme(
                themeMode = themeMode,
                useDynamicColor = dynamic,
                uiLanguage = uiLang,
            ) {
                Surface(modifier = Modifier.fillMaxSize(), color = androidx.compose.material3.MaterialTheme.colorScheme.background) {
                    val config = LocalConfiguration.current
                    LuachNavHost(widthDp = config.screenWidthDp)
                }
            }
        }
    }
}
