package com.example.livingai

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.LocalActivityResultRegistryOwner
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.CompositionLocalProvider
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.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat
import com.example.livingai.domain.usecases.AppDataUseCases
import com.example.livingai.pages.home.HomeViewModel
import com.example.livingai.pages.navigation.NavGraph
import com.example.livingai.ui.theme.LivingAITheme
import com.example.livingai.utils.LocaleHelper
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel

class MainActivity : ComponentActivity() {
    private val viewModel by viewModel<HomeViewModel>()
    private val appDataUseCases: AppDataUseCases by inject()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        WindowCompat.setDecorFitsSystemWindows(window, false)
        installSplashScreen().apply {
            setKeepOnScreenCondition {
                viewModel.splashCondition.value
            }
        }

        setContent {
            val settings by appDataUseCases.getSettings().collectAsState(initial = null)
            val context = LocalContext.current

            // Update locale and provide it through CompositionLocalProvider
            val localizedContext = settings?.let {
                LocaleHelper.applyLocale(context, it.language)
            } ?: context

            CompositionLocalProvider(
                LocalContext provides localizedContext,
                LocalActivityResultRegistryOwner provides this
            ) {
                LivingAITheme {
                    enableEdgeToEdge(
                        statusBarStyle = SystemBarStyle.auto(
                            lightScrim = Color.Transparent.toArgb(),
                            darkScrim = Color.Transparent.toArgb()
                        ),
                        navigationBarStyle = SystemBarStyle.auto(
                            lightScrim = Color.Transparent.toArgb(),
                            darkScrim = Color.Transparent.toArgb()
                        )
                    )
                    Box(modifier = Modifier.background(color = MaterialTheme.colorScheme.background)) {
                        val startDestination = viewModel.startDestination.value
                        // Ensure startDestination is not null before rendering NavGraph
                        if (startDestination != null) {
                            NavGraph(startDestination = startDestination)
                        } else {
                            // Optional: Show a loading indicator if startDestination is null
                            // for an extended period, though splash screen should handle it.
                        }
                    }
                }
            }
        }
    }
}