package com.example.ops_android

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Surface
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import com.example.ops_android.printer.PrinterManager
import com.example.ops_android.ui.LocalAppContainer
import com.example.ops_android.ui.navigation.OPSNavGraph
import com.example.ops_android.ui.theme.Ops_androidTheme
import kotlinx.coroutines.runBlocking

class MainActivity : ComponentActivity() {

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

        val appContainer = (application as OpsApplication).appContainer
        val printerManager = PrinterManager(this)
        appContainer.printerManager = printerManager
        val isLoggedIn = runBlocking { appContainer.sessionManager.isLoggedIn() }

        enableEdgeToEdge()
        setContent {
            Ops_androidTheme {
                CompositionLocalProvider(LocalAppContainer provides appContainer) {
                    Surface(modifier = Modifier.fillMaxSize()) {
                        OPSNavGraph(isLoggedIn = isLoggedIn)
                    }
                }
            }
        }
    }

    override fun onStart() {
        super.onStart()
        (application as OpsApplication).appContainer.printerManager?.bindService()
    }

    override fun onDestroy() {
        super.onDestroy()
        (application as OpsApplication).appContainer.printerManager?.unbindService()
    }
}
