package com.onthelevel

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Adjust
import androidx.compose.material.icons.outlined.Handyman
import androidx.compose.material.icons.outlined.SquareFoot
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.foundation.layout.padding
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.onthelevel.core.design.OnTheLevelTheme
import com.onthelevel.feature.angle.AngleScreen
import com.onthelevel.feature.level.LevelScreen
import com.onthelevel.feature.level.SurfaceCalibrationScreen
import com.onthelevel.feature.ruler.RulerScreen
import com.onthelevel.feature.tools.SettingsScreen
import com.onthelevel.feature.tools.ToolsScreen

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        enableEdgeToEdge()
        super.onCreate(savedInstanceState)
        val container = (application as OnTheLevelApp).container
        setContent {
            OnTheLevelTheme {
                AppRoot(container)
            }
        }
    }
}

private data class TopDestination(
    val route: String,
    val label: String,
    val icon: ImageVector,
)

private val topDestinations = listOf(
    TopDestination("level", "Level", Icons.Outlined.Adjust),
    TopDestination("angle", "Angle", Icons.Outlined.SquareFoot),
    TopDestination("tools", "Tools", Icons.Outlined.Handyman),
)

@Composable
private fun AppRoot(container: AppContainer) {
    val navController = rememberNavController()
    val backStackEntry by navController.currentBackStackEntryAsState()
    val currentRoute = backStackEntry?.destination?.route

    Scaffold(
        bottomBar = {
            NavigationBar {
                topDestinations.forEach { destination ->
                    NavigationBarItem(
                        selected = currentRoute == destination.route,
                        onClick = {
                            navController.navigate(destination.route) {
                                popUpTo(navController.graph.findStartDestination().id) {
                                    saveState = true
                                }
                                launchSingleTop = true
                                restoreState = true
                            }
                        },
                        icon = { Icon(destination.icon, contentDescription = null) },
                        label = { Text(destination.label) },
                    )
                }
            }
        },
    ) { padding ->
        NavHost(
            navController = navController,
            startDestination = "level",
            modifier = Modifier.padding(padding),
        ) {
            composable("level") {
                LevelScreen(container, onOpenSettings = {
                    navController.navigate("settings")
                })
            }
            composable("angle") { AngleScreen(container) }
            composable("tools") {
                ToolsScreen(
                    onOpenRuler = { navController.navigate("ruler") },
                    onOpenSettings = { navController.navigate("settings") },
                )
            }
            composable("ruler") { RulerScreen(container) }
            composable("settings") {
                SettingsScreen(
                    container,
                    onOpenSurfaceCalibration = { navController.navigate("surface-calibration") },
                    onBack = { navController.popBackStack() },
                )
            }
            composable("surface-calibration") {
                SurfaceCalibrationScreen(container, onBack = { navController.popBackStack() })
            }
        }
    }
}
