package com.keylightpiano.pianotimeline

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.keylightpiano.pianotimeline.ui.RecordingListScreen
import com.keylightpiano.pianotimeline.ui.RecordingListViewModel
import com.keylightpiano.pianotimeline.ui.SettingsScreen
import com.keylightpiano.pianotimeline.ui.theme.PianoTimelineTheme
import kotlinx.serialization.Serializable

/**
 * Type-safe navigation routes. Kotlin-serialization-backed destinations
 * (Navigation-Compose 2.8+), so adding arguments later is just adding fields.
 */
@Serializable
object ListRoute

@Serializable
object SettingsRoute

// Standard Android forward/back slide feel: the incoming screen slides in from
// the right while the outgoing one slides partway out to the left, both with a
// short fade. On back (pop), the directions reverse. 300ms matches the platform
// default duration closely enough to feel native.
private const val NAV_ANIM_MS = 300

class MainActivity : ComponentActivity() {

    // Hoist the ViewModel to the Activity so deep-link intents (which arrive here)
    // can be forwarded to it. Both Compose screens share this same instance.
    private val vm: RecordingListViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        // Cold start: an intent may already carry a deep link.
        handleIntent(intent)

        setContent {
            // Force dark mode regardless of system setting
            PianoTimelineTheme(darkTheme = true) {
                val navController = rememberNavController()

                // Opaque themed backdrop BEHIND the NavHost. During a screen
                // transition Compose does not stack the two destinations -- it
                // plays exit then enter -- so mid-animation there's a gap where
                // neither fully covers the host. Without this, the window's
                // default (white) background shows through as a flash. Filling
                // the host with the theme background makes that gap dark instead.
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    NavHost(
                        navController = navController,
                        startDestination = ListRoute,
                        // Forward navigation (A -> B): new screen enters from the right.
                        enterTransition = {
                            slideIntoContainer(
                                AnimatedContentTransitionScope.SlideDirection.Left,
                                animationSpec = tween(NAV_ANIM_MS)
                            ) + fadeIn(tween(NAV_ANIM_MS))
                        },
                        exitTransition = {
                            slideOutOfContainer(
                                AnimatedContentTransitionScope.SlideDirection.Left,
                                animationSpec = tween(NAV_ANIM_MS)
                            ) + fadeOut(tween(NAV_ANIM_MS))
                        },
                        // Back navigation (pop B -> A): directions reverse.
                        popEnterTransition = {
                            slideIntoContainer(
                                AnimatedContentTransitionScope.SlideDirection.Right,
                                animationSpec = tween(NAV_ANIM_MS)
                            ) + fadeIn(tween(NAV_ANIM_MS))
                        },
                        popExitTransition = {
                            slideOutOfContainer(
                                AnimatedContentTransitionScope.SlideDirection.Right,
                                animationSpec = tween(NAV_ANIM_MS)
                            ) + fadeOut(tween(NAV_ANIM_MS))
                        }
                    ) {
                        composable<ListRoute> {
                            RecordingListScreen(
                                vm = vm,
                                onOpenSettings = { navController.navigate(SettingsRoute) }
                            )
                        }
                        composable<SettingsRoute> {
                            SettingsScreen(
                                vm = vm,
                                onBack = { navController.popBackStack() }
                            )
                        }
                    }
                }
            }
        }
    }

    // Warm start: app already running, a new link arrives.
    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
        handleIntent(intent)
    }

    private fun handleIntent(intent: Intent?) {
        val data: Uri = intent?.data ?: return
        if (intent.action == Intent.ACTION_VIEW) {
            vm.handleDeepLink(data)
        }
    }
}
