package com.harshmallow.pilottoolkit

import android.os.Bundle
import android.net.Uri
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.*
import com.harshmallow.pilottoolkit.ui.PilotToolkitApp
import com.harshmallow.pilottoolkit.ui.getSavedDarkMode
import com.harshmallow.pilottoolkit.ui.saveDarkMode
import com.harshmallow.pilottoolkit.data.DataLoader
import com.harshmallow.pilottoolkit.ui.theme.PilotToolkitTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        DataLoader.init(this)

        setContent {
            var darkMode by remember { mutableStateOf(getSavedDarkMode(this@MainActivity)) }
            var importUri by remember { mutableStateOf<Uri?>(extractImportUri(intent)) }

            PilotToolkitTheme(darkTheme = darkMode) {
                PilotToolkitApp(
                    darkMode = darkMode,
                    onToggleDarkMode = {
                        darkMode = !darkMode
                        saveDarkMode(this@MainActivity, darkMode)
                    },
                    importUri = importUri,
                    onImportConsumed = { importUri = null }
                )
            }
        }
    }

    private fun extractImportUri(intent: android.content.Intent?): Uri? {
        if (intent == null) return null
        return when (intent.action) {
            android.content.Intent.ACTION_VIEW -> intent.data
            android.content.Intent.ACTION_SEND -> intent.getParcelableExtra(android.content.Intent.EXTRA_STREAM)
            else -> null
        }
    }
}
