package com.example.livingai.pages.components

import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Build
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.List
import androidx.compose.material3.Badge
import androidx.compose.material3.BadgedBox
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
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.ui.tooling.preview.Preview
import androidx.navigation.NavController
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.toRoute
import com.example.livingai.pages.commons.Dimentions
import com.example.livingai.pages.navigation.Route
import com.example.livingai.ui.theme.LivingAITheme

@Composable
fun LivingAIBottomBar(
    navItems: List<BottomBarItem>,
    navController: NavController,
    onClick: (BottomBarItem) -> Unit,
    modifier: Modifier = Modifier
) {
    val backStackEntry by navController.currentBackStackEntryAsState()

    NavigationBar(
        modifier = modifier,
        containerColor = MaterialTheme.colorScheme.surface,
        contentColor = MaterialTheme.colorScheme.onSurface,
        tonalElevation = Dimentions.BOTTOM_BAR_ELEVATION
    ) {
        navItems.forEach { item ->
            val selected = backStackEntry?.destination?.hierarchy?.any { 
                // This is a safer check. It compares the string route pattern.
                // It's less ideal than type-safe checking but avoids deserialization crashes.
                it.route == item.route::class.simpleName 
            } == true
            
            NavigationBarItem(
                selected = selected,
                onClick = { onClick(item) },
                colors = NavigationBarItemDefaults.colors(
                    selectedIconColor = MaterialTheme.colorScheme.onSecondaryContainer,
                    selectedTextColor = MaterialTheme.colorScheme.onSurface,
                    indicatorColor = MaterialTheme.colorScheme.secondaryContainer,
                    unselectedIconColor = MaterialTheme.colorScheme.onSurfaceVariant,
                    unselectedTextColor = MaterialTheme.colorScheme.onSurfaceVariant
                ),
                icon = {
                    BadgedBox(
                        badge = {
                            if (item.notifications > 0) {
                                Badge(
                                    containerColor = MaterialTheme.colorScheme.error,
                                    contentColor = MaterialTheme.colorScheme.onError
                                ) {
                                    Text(
                                        text = item.notifications.toString()
                                    )
                                }
                            }
                        },
                        content = {
                            Icon(
                                imageVector = item.icon,
                                contentDescription = null,
                                modifier = Modifier.padding(Dimentions.BOTTOM_BAR_PADDING)
                            )
                        }
                    )
                },
                label = { Text(text = item.name) }
            )
        }
    }
}

data class BottomBarItem(
    val route: Route,
    val icon: ImageVector,
    val name: String,
    val notifications: Int
)

@Preview(showBackground = true)
@Composable
fun showBottomBar() {
    val bottomNavItems = listOf(
        BottomBarItem(
            route = Route.OnBoardingScreen,
            icon = Icons.Default.Build,
            name = "Nav",
            notifications = 1,
        ),
        BottomBarItem(
            route = Route.HomeScreen,
            icon = Icons.Default.Home,
            name = "Home",
            notifications = 5,
        ),
        BottomBarItem(
            route = Route.ListingsScreen,
            icon = Icons.Default.List,
            name = "Listings",
            notifications = 0,
        )
    )
    LivingAITheme {
        Scaffold(
            bottomBar = {
                LivingAIBottomBar(
                    navItems = bottomNavItems,
                    navController = rememberNavController(),
                    onClick = {},
                    modifier = Modifier,
                )
            }
        ) { innerPadding ->
            Text(
                text = "test page",
                modifier = Modifier.padding(innerPadding)
            )
        }
    }
}