package com.pixelized.desktop.lwa.ui.composable

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.SnackbarHost
import androidx.compose.material.SnackbarHostState
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.LocalBlurController
import com.pixelized.desktop.lwa.LocalErrorSnackHost
import com.pixelized.desktop.lwa.LocalRollHostState
import com.pixelized.desktop.lwa.LocalSnackHost
import com.pixelized.desktop.lwa.Snackbar
import com.pixelized.desktop.lwa.ui.composable.blur.BlurContent
import com.pixelized.desktop.lwa.ui.composable.blur.BlurContentController
import com.pixelized.desktop.lwa.ui.composable.blur.rememberBlurContentController
import com.pixelized.desktop.lwa.ui.overlay.roll.RollHostState
import com.pixelized.desktop.lwa.ui.overlay.roll.RollOverlay

@Composable
fun LwaScaffold(
    modifier: Modifier = Modifier,
    snackHostState: SnackbarHostState = remember { SnackbarHostState() },
    errorSnackHostState: SnackbarHostState = remember { SnackbarHostState() },
    rollHostState: RollHostState = remember { RollHostState() },
    blurController: BlurContentController = rememberBlurContentController(),
    content: @Composable BoxScope.() -> Unit,
) {
    CompositionLocalProvider(
        LocalSnackHost provides snackHostState,
        LocalErrorSnackHost provides errorSnackHostState,
        LocalRollHostState provides rollHostState,
        LocalBlurController provides blurController,
    ) {
        Surface(
            modifier = modifier,
        ) {
            Scaffold(
                snackbarHost = {
                    Column(
                        modifier = Modifier.padding(all = 8.dp),
                        verticalArrangement = Arrangement.spacedBy(space = 4.dp)
                    ) {
                        SnackbarHost(
                            hostState = snackHostState,
                            snackbar = {
                                Snackbar(
                                    snackbarData = it,
                                    backgroundColor = MaterialTheme.colors.surface,
                                    contentColor = MaterialTheme.colors.onSurface,
                                    actionColor = MaterialTheme.colors.onSurface,
                                )
                            }
                        )
                        SnackbarHost(
                            hostState = errorSnackHostState,
                            snackbar = {
                                Snackbar(
                                    snackbarData = it,
                                    backgroundColor = MaterialTheme.colors.error,
                                    contentColor = MaterialTheme.colors.onError,
                                    actionColor = MaterialTheme.colors.onError,
                                )
                            }
                        )
                    }
                },
                content = {
                    BlurContent(
                        modifier = Modifier.fillMaxSize(),
                        controller = blurController,
                        content = content,
                    )
                    RollOverlay(
                        modifier = Modifier.fillMaxSize(),
                        hostState = rollHostState,
                    )
                }
            )
        }
    }
}