package com.example.m20_gamepad

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.m20_gamepad.navigation.NavRoutes
import com.example.m20_gamepad.ui.MainScreen
import com.example.m20_gamepad.ui.SettingsScreen
import com.example.m20_gamepad.ui.theme.M20_gamepadTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        // 隐藏系统状态栏，顶部区域完全由自定义 StatusBar 接管
        WindowInsetsControllerCompat(window, window.decorView).apply {
            hide(WindowInsetsCompat.Type.statusBars())
            systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        }
        setContent {
            M20_gamepadTheme {
                AppNavGraph()
            }
        }
    }
}

/**
 * 应用导航图：主界面 <-> 设置界面。
 *
 * - 主界面：双摇杆控制 + RTSP 视频
 * - 设置界面：配置机器人连接与视频参数，保存后返回主界面
 */
@Composable
private fun AppNavGraph() {
    val navController = rememberNavController()
    val app = LocalContext.current.applicationContext as M20App
    val repository = app.settingsRepository

    NavHost(
        navController = navController,
        startDestination = NavRoutes.MAIN
    ) {
        composable(NavRoutes.MAIN) {
            MainScreen(
                repository = repository,
                onSettingsClick = {
                    navController.navigate(NavRoutes.SETTINGS)
                }
            )
        }
        composable(NavRoutes.SETTINGS) {
            SettingsScreen(
                repository = repository,
                onBack = {
                    navController.popBackStack(
                        route = NavRoutes.MAIN,
                        inclusive = false
                    )
                },
                onSaved = {
                    navController.popBackStack(
                        route = NavRoutes.MAIN,
                        inclusive = false
                    )
                }
            )
        }
    }
}