package de.haushalt.app

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import de.haushalt.app.ui.schema.SchemaAllScreen
import de.haushalt.app.ui.schema.SchemaCreateScreen
import de.haushalt.app.ui.schema.SchemaEditScreen
import de.haushalt.app.ui.task.TaskAllScreen
import de.haushalt.app.ui.task.TaskEditScreen
import de.haushalt.app.ui.task.TaskScreen

private val trailMap: Map<String, List<Pair<String, String>>> = mapOf(
    "start" to listOf("start" to "Haushalt"),
    "tasks" to listOf("start" to "Haushalt", "tasks" to "Tasks"),
    "tasks/all" to listOf(
        "start" to "Haushalt",
        "tasks" to "Tasks",
        "tasks/all" to "All",
    ),
    "tasks/{id}" to listOf(
        "start" to "Haushalt",
        "tasks" to "Tasks",
        "tasks/{id}" to "Edit",
    ),
    "schemas" to listOf(
        "start" to "Haushalt",
        "tasks" to "Tasks",
        "schemas" to "Schemas",
    ),
    "schemas/create" to listOf(
        "start" to "Haushalt",
        "tasks" to "Tasks",
        "schemas" to "Schemas",
        "schemas/create" to "Create",
    ),
    "schemas/{id}" to listOf(
        "start" to "Haushalt",
        "tasks" to "Tasks",
        "schemas" to "Schemas",
        "schemas/{id}" to "Edit",
    ),
)

@Composable
fun MainScreen() {
    val navController = rememberNavController()
    val backStackEntry by navController.currentBackStackEntryAsState()
    val currentRoute = backStackEntry?.destination?.route

    Scaffold(
        topBar = { Breadcrumb(currentRoute = currentRoute, navController = navController) }
    ) { innerPadding ->
        NavHost(
            navController = navController,
            startDestination = "start",
            modifier = Modifier.padding(innerPadding)
        ) {
            composable("start") {
                StartScreen(onOpenTasks = { navController.navigate("tasks") })
            }
            composable("tasks") {
                TaskScreen(navController = navController)
            }
            composable("tasks/all") {
                TaskAllScreen(navController = navController)
            }
            composable(
                route = "tasks/{id}",
                arguments = listOf(navArgument("id") { type = NavType.IntType })
            ) { entry ->
                val id = entry.arguments?.getInt("id") ?: return@composable
                TaskEditScreen(navController = navController, taskId = id)
            }
            composable("schemas") {
                SchemaAllScreen(navController = navController)
            }
            composable("schemas/create") {
                SchemaCreateScreen(navController = navController)
            }
            composable(
                route = "schemas/{id}",
                arguments = listOf(navArgument("id") { type = NavType.IntType })
            ) { entry ->
                val id = entry.arguments?.getInt("id") ?: return@composable
                SchemaEditScreen(navController = navController, schemaId = id)
            }
        }
    }
}

@Composable
private fun Breadcrumb(currentRoute: String?, navController: NavController) {
    val trail = trailMap[currentRoute] ?: listOf("start" to "Haushalt")

    Surface(
        modifier = Modifier.fillMaxWidth(),
        color = MaterialTheme.colorScheme.surfaceContainer
    ) {
        Row(
            modifier = Modifier
                .statusBarsPadding()
                .padding(horizontal = 16.dp, vertical = 12.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            trail.forEachIndexed { index, (route, label) ->
                if (index > 0) {
                    Text(
                        text = "›",
                        modifier = Modifier.padding(horizontal = 6.dp),
                        color = MaterialTheme.colorScheme.onSurfaceVariant
                    )
                }
                val isCurrent = route == currentRoute
                Text(
                    text = label,
                    fontWeight = if (isCurrent) FontWeight.SemiBold else FontWeight.Normal,
                    modifier = if (isCurrent) {
                        Modifier
                    } else {
                        Modifier.clickable {
                            navController.popBackStack(route, inclusive = false)
                        }
                    }
                )
            }
        }
    }
}
