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.*
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.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.pixelized.biblib.ui.navigation.ScreenNavHost
import com.pixelized.biblib.ui.screen.launch.LauncherViewModel
import com.skydoves.landscapist.glide.LocalGlideRequestOptions

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

@Composable
fun MainContent(
    launcherViewModel: LauncherViewModel = hiltViewModel(),
    glideOptions: RequestOptions = RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL)
) {
    val scaffoldState: ScaffoldState = rememberScaffoldState()

    CompositionLocalProvider(
        LocalGlideRequestOptions provides glideOptions,
        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
                    )
                }
            }
        )
    }
}