package com.example.pmulabs.basecomponents.navigate

import androidx.compose.animation.AnimatedVisibility
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.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
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.unit.dp
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

@Composable
fun BottomBar(navController: NavHostController){
    val screens = listOf(
        BottomBarScreen.Profile,
        BottomBarScreen.Categories,
        BottomBarScreen.Main,
        BottomBarScreen.Cooperation,
        BottomBarScreen.Info
    )
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val currentDestination=navBackStackEntry?.destination
    val bottomBarDestination=screens.any{ it.route==currentDestination?.route }
    Row(
        modifier = Modifier
            .padding(start = 17.dp, end = 17.dp, top = 8.dp, bottom = 8.dp)
            .background(Color.Transparent),
        horizontalArrangement = Arrangement.SpaceEvenly,
        verticalAlignment = Alignment.CenterVertically
    ) {
        if (bottomBarDestination) {
            NavigationBar(
                containerColor = Color(0xFFDBDBF1),
                modifier = Modifier
                    .height(40.dp)
                    .clip(shape = RoundedCornerShape(20.dp))
            ) {
                screens.forEach { screen ->
                    AddItem(
                        screen = screen,
                        currentDestination = currentDestination,
                        navController = navController
                    )
                }
            }
        }
    }
}

@Composable
fun RowScope.AddItem(
    screen: BottomBarScreen,
    currentDestination: NavDestination?,
    navController: NavController
){
    val selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true

    val background = if (selected) Color(0xff423a99) else Color.Transparent

    val contentColor =
        if (selected) Color.White else Color(0xff423a99)

    Box(
        modifier = Modifier
            .height(40.dp)
            .clip(CircleShape)
            .background(background)
            .clickable(onClick = {
                navController.navigate(screen.route) {
                    popUpTo(navController.graph.findStartDestination().id)
                    launchSingleTop = true
                }
            })
    ) {
        Row(
            modifier = Modifier
                .padding(start = 17.dp, end = 17.dp, top = 8.dp, bottom = 8.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(4.dp)
        ) {
            Icon(
                painter = painterResource(id = if (selected) screen.iconFocused else screen.icon),
                contentDescription = "icon",
                tint = Color.Unspecified
            )
            AnimatedVisibility(visible = selected) {
                Text(
                    text = screen.title,
                    color = contentColor
                )
            }
        }
    }

    /*NavigationBarItem(
        label = {
            Text(text=screen.title, color = Color(0xff423a99))
              },
        icon = {
            Icon(
                painter = painterResource(id = screen.icon),
                tint = Color.Unspecified,
                contentDescription = "navigation icon")
        },
        selected = selected,
        onClick = {
            navController.navigate(screen.route)
        }

    )*/
}
