package com.pixelized.biblib.ui

import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.ScaffoldState
import androidx.compose.material.Snackbar
import androidx.compose.material.SnackbarHost
import androidx.compose.material.SnackbarHostState
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import com.pixelized.biblib.ui.navigation.ScreenNavHost
import com.pixelized.biblib.ui.screen.launch.LauncherViewModel

val LocalSnackHostState = staticCompositionLocalOf<SnackbarHostState> {
    error("SnackBarHostState is not ready yet.")
}

@Composable
fun MainContent(
    launcherViewModel: LauncherViewModel = hiltViewModel(),
) {
    val scaffoldState: ScaffoldState = rememberScaffoldState()

    CompositionLocalProvider(
        LocalSnackHostState provides scaffoldState.snackbarHostState,
    ) {
        Scaffold(
            scaffoldState = scaffoldState,
            snackbarHost = { snackHostState ->
                SnackbarHost(
                    modifier = Modifier
                        .systemBarsPadding()
                        .imePadding(),
                    hostState = snackHostState,
                ) { snackBarData ->
                    Snackbar(
                        snackbarData = snackBarData,
                        shape = MaterialTheme.shapes.medium,
                        backgroundColor = MaterialTheme.colors.error,
                        contentColor = MaterialTheme.colors.onError,
                        actionColor = MaterialTheme.colors.onError,
                    )
                }
            },
            content = {
                if (launcherViewModel.isLoadingDone) {
                    ScreenNavHost(
                        modifier = Modifier.padding(it),
                        startDestination = launcherViewModel.startDestination
                    )
                }
            }
        )
    }
}