package com.example.shawarma.screens

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
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 com.example.shawarma.screens.authorization.AuthorizationScreen
import com.example.shawarma.screens.cart.CartScreen
import com.example.shawarma.screens.discount.DiscountScreen
import com.example.shawarma.screens.home.HomeScreen
import com.example.shawarma.screens.orders.OrdersScreen
import com.example.shawarma.screens.products.ProductScreen
import com.example.shawarma.screens.products.ProductsScreen
import com.example.shawarma.screens.registration.RegistrationScreen
import com.example.shawarma.ui.theme.MyLightYellow
import com.example.shawarma.utils.ScreenPaths
import com.example.shawarma.widgets.BottomNavBar

@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun MainNavBar() {
    val navController = rememberNavController()
    Scaffold(
        backgroundColor = MyLightYellow,
        modifier = Modifier.fillMaxSize(),
        bottomBar = {
            val navBackStackEntry by navController.currentBackStackEntryAsState()
            val currentRoute = navBackStackEntry?.destination?.route
            if ( currentRoute != ScreenPaths.authorization.name && currentRoute != ScreenPaths.registration.name) {
                BottomNavBar(navController = navController)
            }
        }
    ) {
        NavHost(navController = navController, startDestination = ScreenPaths.authorization.name) {
            composable(ScreenPaths.authorization.name) {
                AuthorizationScreen(navController)
            }
            composable(ScreenPaths.registration.name) {
                RegistrationScreen(navController)
            }
            composable(ScreenPaths.home.name) {
                HomeScreen()
            }
            composable(ScreenPaths.discount.name) {
                DiscountScreen()
            }
            composable(ScreenPaths.cart.name) {
                CartScreen()
            }
            composable(ScreenPaths.orders.name) {
                OrdersScreen()
            }
            composable(ScreenPaths.products.name) {
                ProductsScreen(navController)
            }
            composable(
                route = ScreenPaths.product.name + "?productId={productId}",
                arguments = listOf(
                    navArgument("productId") {
                        defaultValue = 0
                        type = NavType.IntType
                    }
                )

            ) { navBackStackEntry ->
                /* Extracting the id from the route */
                val productId = navBackStackEntry.arguments?.getInt("productId")
                ProductScreen(navController, productId)
            }
        }
    }
}