package com.isidroid.c23

import android.app.Activity
import android.net.Uri
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.isidroid.c23.ext.isEdgeToEdge
import com.isidroid.c23.ui.navigation.AppNavHost
import com.isidroid.c23.ui.navigation.PrintJobs
import com.isidroid.c23.ui.navigation.routeHome
import com.isidroid.c23.ui.theme.AppTheme
import com.isidroid.core.ext.navigateSingleTopTo
import com.isidroid.core.ext.printCurrentDestination
import com.isidroid.core.ext.printNavigationStack
import com.isidroid.utils.extractUris
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        enableEdgeToEdge(
            statusBarStyle = SystemBarStyle.light(Color.Transparent.toArgb(), Color.Transparent.toArgb()),
            navigationBarStyle = SystemBarStyle.light(Color.Transparent.toArgb(), Color.Transparent.toArgb()),
        )


        setContent {
            ComposeApp()
        }
    }
}


@Composable
fun ComposeApp() {
    val navController = rememberNavController()
    val currentBackStack by navController.currentBackStackEntryAsState()
    val isEdgeToEdge = currentBackStack?.destination?.route.isEdgeToEdge
    val activity = LocalContext.current as Activity
    val intent = activity.intent

    LaunchedEffect(intent) {
        val uris = intent.extractUris()
        if (uris != null)
            navController.navigateSingleTopTo(routeHome(uris = uris.joinToString()))
    }

    LaunchedEffect(Unit) { navController.printNavigationStack() }

    val surfaceModifier = with(Modifier) {
        if (!isEdgeToEdge)
            navigationBarsPadding().systemBarsPadding()
        else
            this
    }

    AppTheme {
        Surface(modifier = surfaceModifier) {
            val horizontalPaddings = if (isEdgeToEdge) 0 else 16
            AppNavHost(
                navController = navController,
                modifier = Modifier
                    .fillMaxSize()
                    .padding(horizontal = horizontalPaddings.dp)

            )
        }
    }
}

