package com.example.tasks.navigation.customers

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
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.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.example.tasks.viewModels.AuthViewModel
import com.example.tasks.viewModels.DatabaseViewModel

@Composable
fun AppNavigationCustomers(authViewModel: AuthViewModel,viewModel: DatabaseViewModel,mainNavController: NavController) {
    val navController = rememberNavController()
    Scaffold(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.Transparent) ,
        bottomBar = { BottomBarCustomer(navController = navController) }
    ) {
        Column(modifier = Modifier
            .fillMaxSize()
            .padding(it)) {
            BottomGraphCustomers(navController = navController,viewModel,authViewModel,mainNavController)
        }
    }
}

@Composable
fun BottomBarCustomer(navController: NavHostController) {
    val screens = listOf(
        NavItemCustomer.ProfileCustomer,
        NavItemCustomer.Catalog,

    )

    val navStackBackEntry by navController.currentBackStackEntryAsState()
    val currentDestination = navStackBackEntry?.destination

    Row(
        modifier = Modifier
            .height(60.dp)
            .background(Color.White)
            .fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceEvenly,
        verticalAlignment = Alignment.CenterVertically
    ) {
        screens.forEach { screen ->
            AddItemCustomer(
                screen = screen,
                currentDestination = currentDestination,
                navController = navController
            )
        }
    }

}
@Composable
fun AddItemCustomer(
    screen: NavItemCustomer,
    currentDestination: NavDestination?,
    navController: NavHostController
) {
    val selected = currentDestination?.hierarchy?.any { it.route == screen.rote } == true

    val background =
        if (selected) Color.White else Color.Transparent

    val contentColor =
        if (selected) Color(0xFF951DDD) else Color(0xFF951DDD)

    Box(
        modifier = Modifier
            .width(85.dp)
            .clip(RoundedCornerShape(10.dp))
            .background(background)
            .clickable(onClick = {
                navController.navigate(screen.rote) {
                    popUpTo(navController.graph.findStartDestination().id)
                    launchSingleTop = true
                }
            })
    ) {
        Column(
            modifier = Modifier
                .height(60.dp)
                .align(Alignment.Center),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Icon(
                painter = painterResource(id = if (selected) screen.icon else screen.icon),
                contentDescription = "icon",
                tint = contentColor
            )
            Text(
                text = screen.label,
                color = contentColor,
                fontSize = 12.sp,
                fontWeight = FontWeight.Normal

            )
        }
    }
}