package com.manalejandro.odb2bluetooth

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.manalejandro.odb2bluetooth.ui.navigation.Screen
import com.manalejandro.odb2bluetooth.ui.screens.*
import com.manalejandro.odb2bluetooth.ui.theme.ODB2BluetoothTheme
import com.manalejandro.odb2bluetooth.ui.viewmodel.MainViewModel

/**
 * Main Activity - Entry point of the OBD2 Bluetooth application
 */
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            ODB2BluetoothTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    OBD2App()
                }
            }
        }
    }
}

@Composable
fun OBD2App() {
    val navController = rememberNavController()
    val mainViewModel: MainViewModel = viewModel()

    NavHost(
        navController = navController,
        startDestination = Screen.Home.route
    ) {
        composable(Screen.Home.route) {
            HomeScreen(
                viewModel = mainViewModel,
                onNavigateToBluetooth = {
                    navController.navigate(Screen.Bluetooth.route)
                },
                onNavigateToVehicleSelection = {
                    navController.navigate(Screen.VehicleSelection.route)
                },
                onNavigateToDiagnostic = {
                    navController.navigate(Screen.Diagnostic.route)
                },
                onNavigateToCustomCommand = {
                    navController.navigate(Screen.CustomCommand.route)
                }
            )
        }

        composable(Screen.Bluetooth.route) {
            BluetoothScreen(
                viewModel = mainViewModel,
                onNavigateBack = { navController.popBackStack() }
            )
        }

        composable(Screen.VehicleSelection.route) {
            VehicleSelectionScreen(
                mainViewModel = mainViewModel,
                onNavigateBack = { navController.popBackStack() }
            )
        }

        composable(Screen.Diagnostic.route) {
            DiagnosticScreen(
                viewModel = mainViewModel,
                onNavigateBack = { navController.popBackStack() }
            )
        }

        composable(Screen.CustomCommand.route) {
            CustomCommandScreen(
                viewModel = mainViewModel,
                onNavigateBack = { navController.popBackStack() }
            )
        }
    }
}

