package com.prodhack.moscow2025.presentation.navigation

import android.content.Context
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Snackbar
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.navigation.compose.currentBackStackEntryAsState
import com.prodhack.moscow2025.presentation.components.TBottomNavigation
import com.prodhack.moscow2025.presentation.theme.MoscowHackatonTemplateTheme
import com.prodhack.moscow2025.presentation.utils.ui.AppSnackbarVisuals
import com.prodhack.moscow2025.presentation.utils.ui.SnackbarStyle

@Composable
fun TTasksApp(
	appState: TTasksAppState = rememberTTasksAppState(),
	context: Context,
	requestNotifyPermissions: () -> Unit,
	sessionDestination: AppDestination? = null
) {
	MoscowHackatonTemplateTheme {
		val snackbarHostState = remember { SnackbarHostState() }
		val bottomBarState = remember { mutableStateOf<Int?>(null) }

		when (appState.navController.currentBackStackEntryAsState().value?.destination?.route) {
			AppDestination.Login.route -> {
				bottomBarState.value = null
			}

			AppDestination.Register.route -> {
				bottomBarState.value = null
			}

			AppDestination.Main.route -> {
				bottomBarState.value = 1
			}

			AppDestination.Profile.route -> {
				bottomBarState.value = 2
			}
		}
		Scaffold(
			modifier = Modifier.fillMaxSize(),
			snackbarHost = {
				SnackbarHost(
					hostState = snackbarHostState,
					snackbar = { data ->
						val style = (data.visuals as? AppSnackbarVisuals)?.style ?: SnackbarStyle.Error
						val containerColor = when (style) {
							SnackbarStyle.Success -> MaterialTheme.colorScheme.tertiaryContainer
							SnackbarStyle.Error -> MaterialTheme.colorScheme.errorContainer
						}
						val contentColor = when (style) {
							SnackbarStyle.Success -> MaterialTheme.colorScheme.onTertiaryContainer
							SnackbarStyle.Error -> MaterialTheme.colorScheme.onErrorContainer
						}
						Snackbar(
							snackbarData = data,
							containerColor = containerColor,
							contentColor = contentColor,
							shape = MaterialTheme.shapes.medium
						)
					}
				)
			},
			bottomBar = {
				bottomBarState.value?.let { bbState ->
					TBottomNavigation(
						modifier = Modifier
							.background(MaterialTheme.colorScheme.surfaceContainer)
							.windowInsetsPadding(WindowInsets.navigationBars),
						selectedPage = bbState
					) { newPage ->
						when (newPage) {
							0 -> {
								appState.navController.navigate(AppDestination.Main.route)
							}

							1 -> {
								appState.navController.navigate(AppDestination.Profile.route)
							}
						}
					}
				}
			},
		) { padding ->
			TTasksNavHost(
				navController = appState.navController,
				modifier = Modifier.padding(padding),
				sessionDestination = sessionDestination,
				snackbarHostState = snackbarHostState,
				context = context,
				requestNotifyPermissions = requestNotifyPermissions
			)
		}
	}
}
