package com.horrortv.app.presentation

import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.navigation.compose.rememberNavController
import com.horrortv.app.presentation.theme.HorrorTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        val w = window
        WindowCompat.setDecorFitsSystemWindows(w, false)
        val controller = WindowInsetsControllerCompat(w, w.decorView)
        controller.hide(WindowInsetsCompat.Type.systemBars())
        controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        
        setContent {
            HorrorTheme {
                val navController = rememberNavController()
                AppNavigation(
                    navController = navController,
                    modifier = Modifier.fillMaxSize()
                )
            }
        }
    }
    
    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
    }
    
    companion object {
        var keyEventHandler: ((KeyEvent) -> Boolean)? = null
    }
    
    override fun dispatchKeyEvent(event: KeyEvent): Boolean {
        keyEventHandler?.let { handler ->
            if (handler(event)) return true
        }
        return super.dispatchKeyEvent(event)
    }

    override fun onDestroy() {
        keyEventHandler = null
        super.onDestroy()
    }
}