package moe.lava.neon.ui

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalWindowInfo
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewScreenSizes
import androidx.compose.ui.unit.dp
import androidx.navigation3.runtime.NavEntry
import androidx.navigation3.runtime.NavKey
import androidx.navigation3.scene.SceneStrategyScope
import moe.lava.neon.ui.screens.chat.Chat
import moe.lava.neon.ui.screens.members.MembersList
import moe.lava.neon.ui.screens.navigator.Navigator
import moe.lava.neon.ui.screens.navigator.NavigatorModel
import moe.lava.neon.ui.screens.navigator.NavigatorPreviewProvider
import moe.lava.neon.ui.util.ThemedPreview
import moe.lava.neon.ui.util.ThreePaneSceneStrategy
import moe.lava.neon.ui.util.rememberThreePaneSceneStrategy

@PreviewScreenSizes
@Preview(
    name = "Phone - Navigator",
    device = "spec:width=411dp,height=892dp,orientation=portrait,dpi=420",
    showSystemUi = true,
)
@Composable
fun AppPrewiew() {
    val chat: NavEntry<NavKey> = NavEntry(
        Route.Chat,
        metadata = ThreePaneSceneStrategy.detailPane(),
    ) {
        Chat(onOpenMembers = {})
    }
    val nav: NavEntry<NavKey> = NavEntry(
        Route.Navigator(true),
        metadata = ThreePaneSceneStrategy.listPane(),
    ) {
        Navigator(
            NavigatorPreviewProvider.base2.copy(
                guildNavPosition = NavigatorModel.GuildNavPosition.LeftSidebar
            )
        )
    }
    val nav2: NavEntry<NavKey> = NavEntry(
        Route.Navigator(true),
        metadata = ThreePaneSceneStrategy.listPane(),
    ) {
        Navigator(
            NavigatorPreviewProvider.base2.copy(
                guildNavPosition = NavigatorModel.GuildNavPosition.BottomSheet
            )
        )
    }
    val mem: NavEntry<NavKey> = NavEntry(
        Route.MembersList,
        metadata = ThreePaneSceneStrategy.extraPane(),
    ) {
        MembersList()
    }
    val strat = rememberThreePaneSceneStrategy<NavKey>()
    ThemedPreview {
        Box(Modifier.fillMaxSize()) {
            SceneStrategyScope<NavKey>().run {
                strat.run {
                    calculateScene(listOf(chat, nav, mem))?.content()
                        ?: if (LocalWindowInfo.current.containerDpSize.height == 892.dp) {
                            nav2.Content()
                        } else {
                            chat.Content()
                        }
                }
            }
        }
    }
}
