package cy.hexor.furumi

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.SystemBarStyle
import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import cy.hexor.furumi.ui.login.LoginScreen
import cy.hexor.furumi.ui.login.LoginViewModel
import cy.hexor.furumi.ui.player.PlayerScreen
import cy.hexor.furumi.ui.theme.FurumiTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    
    private var deepLinkUri by mutableStateOf<Uri?>(null)
    private val notificationPermissionLauncher = registerForActivityResult(
        ActivityResultContracts.RequestPermission()
    ) { }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge(
            statusBarStyle = SystemBarStyle.dark(android.graphics.Color.TRANSPARENT),
            navigationBarStyle = SystemBarStyle.dark(android.graphics.Color.TRANSPARENT)
        )
        requestNotificationPermissionIfNeeded()
        
        if (deepLinkUri == null) {
            deepLinkUri = intent?.data
        }

        setContent {
            FurumiTheme {
                FurumiApp(
                    deepLinkUri = deepLinkUri,
                    onDeepLinkHandled = { deepLinkUri = null }
                )
            }
        }
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
        deepLinkUri = intent.data
    }

    private fun requestNotificationPermissionIfNeeded() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return

        if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
            notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
        }
    }
}

@Composable
fun FurumiApp(deepLinkUri: Uri?, onDeepLinkHandled: () -> Unit) {
    val navController = rememberNavController()
    
    NavHost(navController = navController, startDestination = "login") {
        composable("login") {
            val loginViewModel: LoginViewModel = hiltViewModel()
            
            // Handle deep link when it arrives
            androidx.compose.runtime.LaunchedEffect(deepLinkUri) {
                deepLinkUri?.let { uri ->
                    loginViewModel.handleDeepLink(uri)
                    onDeepLinkHandled()
                }
            }

            LoginScreen(
                viewModel = loginViewModel,
                onLoginSuccess = {
                    navController.navigate("player") {
                        popUpTo("login") { inclusive = true }
                    }
                }
            )
        }
        composable("player") {
            PlayerScreen(
                onLoggedOut = {
                    navController.navigate("login") {
                        popUpTo("player") { inclusive = true }
                    }
                }
            )
        }
    }
}
