package com.example.kmptpc_compose_sample.demos.navigation

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Card
import androidx.compose.material.MaterialTheme
import androidx.compose.material.OutlinedButton
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.toRoute
import kotlinx.serialization.Serializable

@Serializable
private data class TypedNavPage(
    val name: String = "NavigationPage"
)

@Serializable
private data class TypedNavPage1(
    val name: String = "NavigationPage1"
)

@Serializable
private data class TypedNavPage2(
    val name: String = "NavigationPage2"
)

@Composable
internal fun NavigationTypedRouteDemoScreen() {
    val navController = rememberNavController()
    TypedRouteNavHost(
        startDest = TypedNavPage(),
        navController = navController
    )
}

@Composable
private fun TypedRouteNavHost(
    startDest: Any,
    navController: NavHostController
) {
    NavHost(
        navController = navController,
        startDestination = startDest,
        modifier = Modifier.fillMaxSize()
    ) {
        routeTypedNavPage(navController)
        routeTypedNavPage1(navController)
        routeTypedNavPage2(navController)
    }
}

private fun androidx.navigation.NavGraphBuilder.routeTypedNavPage(navController: NavHostController) {
    composable<TypedNavPage> { entry ->
        val param = entry.toRoute<TypedNavPage>()
        TypedRoutePage(
            title = param.name,
            hint = "typed route: NavigationPage -> NavigationPage1",
            background = MaterialTheme.colors.primary.copy(alpha = 0.08f),
            onNext = { navController.navigate(TypedNavPage1()) },
            onBack = null
        )
    }
}

private fun androidx.navigation.NavGraphBuilder.routeTypedNavPage1(navController: NavHostController) {
    composable<TypedNavPage1> { entry ->
        val param = entry.toRoute<TypedNavPage1>()
        TypedRoutePage(
            title = param.name,
            hint = "typed route: NavigationPage1 -> NavigationPage2",
            background = MaterialTheme.colors.secondary.copy(alpha = 0.08f),
            onNext = { navController.navigate(TypedNavPage2()) },
            onBack = { navController.popBackStack() }
        )
    }
}

private fun androidx.navigation.NavGraphBuilder.routeTypedNavPage2(navController: NavHostController) {
    composable<TypedNavPage2> { entry ->
        val param = entry.toRoute<TypedNavPage2>()
        TypedRoutePage(
            title = param.name,
            hint = "typed route: NavigationPage2 -> NavigationPage",
            background = MaterialTheme.colors.error.copy(alpha = 0.08f),
            onNext = { navController.navigate(TypedNavPage()) },
            onBack = { navController.popBackStack() }
        )
    }
}

@Composable
private fun TypedRoutePage(
    title: String,
    hint: String,
    background: androidx.compose.ui.graphics.Color,
    onNext: () -> Unit,
    onBack: (() -> Unit)?
) {
    Card(
        modifier = Modifier
            .fillMaxSize()
            .padding(12.dp),
        backgroundColor = background,
        elevation = 4.dp
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(24.dp),
            verticalArrangement = Arrangement.SpaceBetween
        ) {
            Column {
                Text(text = title, style = MaterialTheme.typography.h5)
                Spacer(Modifier.height(8.dp))
                Text(text = hint, style = MaterialTheme.typography.body1)
            }
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                if (onBack != null) {
                    OutlinedButton(onClick = onBack) {
                        Text("返回")
                    }
                } else {
                    Spacer(Modifier)
                }
                Button(onClick = onNext) {
                    Text("下一页")
                }
            }
        }
    }
}
