package com.example.wabi.view.components.bars.bottom

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import com.example.wabi.ui.theme.WabiTheme

@Composable
fun BottomBar(
    navHostController: NavHostController,
    modifier: Modifier = Modifier,
    vm: BottomBarViewModel = hiltViewModel()
) {
    val screens = remember {
        listOf(DestinationBB.Catalog, DestinationBB.Basket, DestinationBB.Profile)
    }

    NavigationBar(
        modifier = modifier,
        containerColor = WabiTheme.colors.mainColor
    ) {
        val navBackStackEntry by navHostController.currentBackStackEntryAsState()
        val currentRoute = navBackStackEntry?.destination?.route

        Row(
            modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceAround
        ) {
            screens.forEach { screen ->
                if (screen.route == currentRoute) {
                    Button(
                        onClick = {
                            vm.changeScreen(
                                navHostController = navHostController,
                                whereFrom = currentRoute,
                                where = screen.route
                            )
                        },
                        contentPadding = PaddingValues(10.dp),
                        modifier = Modifier
                            .size(60.dp)
                            .offset(y = (-8).dp),
                        colors = ButtonDefaults.buttonColors(
                            contentColor = WabiTheme.colors.backgroundColor,
                            containerColor = WabiTheme.colors.mainColor
                        ),
                        border = BorderStroke(
                            width = 3.dp, color = WabiTheme.colors.backgroundColor
                        )
                    ) {
                        Icon(
                            imageVector = ImageVector.vectorResource(screen.iconId),
                            contentDescription = screen.route,
                            modifier = Modifier.size(30.dp)
                        )
                    }
                } else {
                    Button(
                        onClick = {
                            vm.changeScreen(
                                navHostController = navHostController,
                                whereFrom = currentRoute!!,
                                where = screen.route
                            )
                        },
                        contentPadding = PaddingValues(10.dp),
                        modifier = Modifier.size(60.dp),
                        colors = ButtonDefaults.buttonColors(
                            contentColor = WabiTheme.colors.backgroundColor,
                            containerColor = WabiTheme.colors.mainColor
                        ),
                    ) {
                        Icon(
                            imageVector = ImageVector.vectorResource(screen.iconId),
                            contentDescription = screen.route,
                            modifier = Modifier.size(30.dp)
                        )
                    }
                }
            }
        }
    }
}