package com.example.kmptpc_compose_sample.demos.navigation

import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.animation.core.tween
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.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController

/**
 * 使用 androidx.navigation 库（navigation-compose）的 Demo：
 * - NavHost + rememberNavController + composable
 * - enter/exit/popEnter/popExit 转场动画（slideIntoContainer / slideOutOfContainer）
 *
 * 依赖：需在工程中引入 navigation-compose（及 navigation-runtime、navigation-common）。
 * 若在独立 sample 中运行，请在 settings 中 include compose_multiplatform_core 的
 * navigation 及传递依赖，或将本应用作为 core 子工程打开。
 */
@Composable
internal fun NavigationLibraryTestScreen() {
    val navController = rememberNavController()
    val animationPresetState = remember { mutableStateOf(AnimationPreset.HorizontalSlide) }
    val animationPreset = animationPresetState.value

    Column(Modifier.fillMaxSize().padding(16.dp)) {
        Text(
            "Navigation 库测试",
            style = MaterialTheme.typography.h6,
            modifier = Modifier.padding(bottom = 4.dp)
        )
        Text(
            "使用 NavHost + composable，测试进入/退出/返回动画",
            style = MaterialTheme.typography.caption,
            modifier = Modifier.padding(bottom = 12.dp)
        )
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(bottom = 8.dp),
            horizontalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            OutlinedButton(onClick = { animationPresetState.value = AnimationPreset.HorizontalSlide }) {
                Text("横向")
            }
            OutlinedButton(onClick = { animationPresetState.value = AnimationPreset.VerticalSlide }) {
                Text("纵向")
            }
            OutlinedButton(onClick = { animationPresetState.value = AnimationPreset.Fade }) {
                Text("淡入淡出")
            }
            OutlinedButton(onClick = { animationPresetState.value = AnimationPreset.ScaleFade }) {
                Text("缩放+淡入")
            }
        }
        Text(
            text = "当前动画：${animationPreset.title}",
            style = MaterialTheme.typography.caption,
            modifier = Modifier.padding(bottom = 10.dp)
        )

        Card(
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f),
            elevation = 4.dp,
            backgroundColor = MaterialTheme.colors.surface
        ) {
            NavHost(
                navController = navController,
                startDestination = "page1",
                modifier = Modifier.fillMaxSize(),
                enterTransition = {
                    when (animationPreset) {
                        AnimationPreset.HorizontalSlide -> slideIntoContainer(
                            towards = AnimatedContentTransitionScope.SlideDirection.Start,
                            animationSpec = tween(300)
                        )
                        AnimationPreset.VerticalSlide -> slideIntoContainer(
                            towards = AnimatedContentTransitionScope.SlideDirection.Up,
                            animationSpec = tween(280)
                        )
                        AnimationPreset.Fade -> fadeIn(animationSpec = tween(220))
                        AnimationPreset.ScaleFade -> {
                            fadeIn(animationSpec = tween(220)) + scaleIn(
                                animationSpec = tween(220),
                                initialScale = 0.92f
                            )
                        }
                    }
                },
                exitTransition = {
                    when (animationPreset) {
                        AnimationPreset.HorizontalSlide -> slideOutOfContainer(
                            towards = AnimatedContentTransitionScope.SlideDirection.Start,
                            animationSpec = tween(300)
                        )
                        AnimationPreset.VerticalSlide -> slideOutOfContainer(
                            towards = AnimatedContentTransitionScope.SlideDirection.Up,
                            animationSpec = tween(280)
                        )
                        AnimationPreset.Fade -> fadeOut(animationSpec = tween(220))
                        AnimationPreset.ScaleFade -> {
                            fadeOut(animationSpec = tween(180)) + scaleOut(
                                animationSpec = tween(180),
                                targetScale = 1.06f
                            )
                        }
                    }
                },
                popEnterTransition = {
                    when (animationPreset) {
                        AnimationPreset.HorizontalSlide -> slideIntoContainer(
                            towards = AnimatedContentTransitionScope.SlideDirection.End,
                            animationSpec = tween(300)
                        )
                        AnimationPreset.VerticalSlide -> slideIntoContainer(
                            towards = AnimatedContentTransitionScope.SlideDirection.Down,
                            animationSpec = tween(280)
                        )
                        AnimationPreset.Fade -> fadeIn(animationSpec = tween(220))
                        AnimationPreset.ScaleFade -> {
                            fadeIn(animationSpec = tween(220)) + scaleIn(
                                animationSpec = tween(220),
                                initialScale = 0.96f
                            )
                        }
                    }
                },
                popExitTransition = {
                    when (animationPreset) {
                        AnimationPreset.HorizontalSlide -> slideOutOfContainer(
                            towards = AnimatedContentTransitionScope.SlideDirection.End,
                            animationSpec = tween(300)
                        )
                        AnimationPreset.VerticalSlide -> slideOutOfContainer(
                            towards = AnimatedContentTransitionScope.SlideDirection.Down,
                            animationSpec = tween(280)
                        )
                        AnimationPreset.Fade -> fadeOut(animationSpec = tween(220))
                        AnimationPreset.ScaleFade -> {
                            fadeOut(animationSpec = tween(180)) + scaleOut(
                                animationSpec = tween(180),
                                targetScale = 0.94f
                            )
                        }
                    }
                }
            ) {
                composable("page1") {
                    NavLibraryPage(
                        title = "第 1 页",
                        onNext = { navController.navigate("page2") },
                        onBack = null
                    )
                }
                composable("page2") {
                    NavLibraryPage(
                        title = "第 2 页",
                        onNext = { navController.navigate("page3") },
                        onBack = { navController.popBackStack() }
                    )
                }
                composable("page3") {
                    NavLibraryPage(
                        title = "第 3 页",
                        onNext = null,
                        onBack = { navController.popBackStack() }
                    )
                }
            }
        }
    }
}

private enum class AnimationPreset(val title: String) {
    HorizontalSlide("横向滑动"),
    VerticalSlide("纵向滑动"),
    Fade("淡入淡出"),
    ScaleFade("缩放 + 淡入淡出")
}

@Composable
private fun NavLibraryPage(
    title: String,
    onNext: (() -> Unit)?,
    onBack: (() -> Unit)?
) {
    Column(
        Modifier
            .fillMaxSize()
            .padding(24.dp),
        verticalArrangement = Arrangement.SpaceBetween
    ) {
        Column {
            Text(title, style = MaterialTheme.typography.h5)
            Spacer(Modifier.height(8.dp))
            Text(
                "由 navigation-compose 的 NavHost + composable 驱动，用于验证库的转场动画。",
                style = MaterialTheme.typography.body1
            )
        }
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            if (onBack != null) {
                OutlinedButton(onClick = onBack) { Text("返回") }
            } else {
                Spacer(Modifier)
            }
            if (onNext != null) {
                Button(onClick = onNext) { Text("下一页") }
            } else {
                Spacer(Modifier)
            }
        }
    }
}
