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

import android.content.Context
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.height
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.MutableState
import androidx.compose.runtime.getValue
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.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import com.example.wabi.domain.navigation.NavigationFun
import com.example.wabi.domain.repository.PrefManager
import com.example.wabi.ui.theme.WabiTheme

@Composable
fun BottomBar(
    navController: NavHostController,
    modifier: Modifier = Modifier,
    context: Context,
    isSession: MutableState<Boolean>
) {
    val screens = listOf(
        DestinationsBB.Catalog, DestinationsBB.Basket, DestinationsBB.Profile
    )

    NavigationBar(
        modifier = Modifier
            .height(70.dp)
            .clip(shape = RoundedCornerShape(topEnd = 15.dp, topStart = 15.dp)),
        containerColor = WabiTheme.colors.mainColor
    ) {
        PrefManager.initPrefManager(context)
        val navBackStackEntry by navController.currentBackStackEntryAsState()
        val currentRoute = navBackStackEntry?.destination?.route
        val nav = NavigationFun()

        Row(
            modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceAround
        ) {
            screens.forEach { screen ->
                if (screen.route == currentRoute || screen.anotherRoute == currentRoute) {
                    Button(
                        onClick = {
                            nav.NextScreen(
                                navController = navController,
                                whereFrom = currentRoute,
                                where = if (isSession.value) screen.route else screen.anotherRoute
                            )
                        },
                        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 = {
                            nav.NextScreen(
                                navController = navController,
                                whereFrom = currentRoute!!,
                                where = if (isSession.value) screen.route else screen.anotherRoute
                            )
                        },
                        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)
                        )
                    }
                }
            }
        }
    }
}