package lu.rewardflow.terminal

import android.app.ActivityManager
import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import dagger.hilt.android.AndroidEntryPoint
import lu.rewardflow.terminal.ui.RewardFlowNavHost
import lu.rewardflow.terminal.ui.theme.RewardFlowTheme

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

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

        setContent {
            RewardFlowTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background,
                ) {
                    RewardFlowNavHost()
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        // Re-apply immersive each time we come back from a transient overlay
        // (action sheets, system dialogs) — they sometimes restore the bars.
        configureImmersiveMode()
        enterLockTaskIfPossible()
    }

    /** Hide both the status bar and the navigation bar; reveal them on
     *  swipe and auto-hide again. Standard Android "immersive sticky"
     *  behavior, expressed via the Compat APIs. */
    private fun configureImmersiveMode() {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        val controller = WindowInsetsControllerCompat(window, window.decorView)
        controller.hide(WindowInsetsCompat.Type.systemBars())
        controller.systemBarsBehavior =
            WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    }

    /**
     * Promote the activity to Lock Task Mode (kiosk).
     *
     * Manifest has ``android:lockTaskMode="if_whitelisted"``, so this
     * call is only honored when a Device Owner / DPC app has whitelisted
     * our package. In dev — and on stock retail tablets without MDM
     * provisioning — the call is a silent no-op, which is the right
     * behavior. On a properly provisioned tablet, this prevents the
     * cashier from leaving the app via Home / Recents / notifications.
     */
    private fun enterLockTaskIfPossible() {
        val am = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        if (am.lockTaskModeState == ActivityManager.LOCK_TASK_MODE_NONE) {
            try {
                startLockTask()
            } catch (e: SecurityException) {
                // Whitelist isn't in place — fine, run as a regular app.
                Log.d("MainActivity", "Lock task not whitelisted: ${e.message}")
            } catch (e: IllegalStateException) {
                Log.d("MainActivity", "Cannot enter lock task: ${e.message}")
            }
        }
    }
}
