package pl.firmatpp.kierowca

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import pl.firmatpp.kierowca.ui.DriverApp
import pl.firmatpp.kierowca.ui.DriverViewModel
import pl.firmatpp.kierowca.ui.theme.TppKierowcaTheme

class MainActivity : ComponentActivity() {
    private var notificationRouteId by mutableStateOf<String?>(null)
    private var notificationRouteTarget by mutableStateOf<String?>(null)
    private var notificationLeaveRequestId by mutableStateOf<String?>(null)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        notificationRouteId = intent.getStringExtra(EXTRA_ROUTE_ID)
        notificationRouteTarget = intent.getStringExtra(EXTRA_ROUTE_TARGET)
        notificationLeaveRequestId = intent.getStringExtra(EXTRA_LEAVE_REQUEST_ID)
        setContent {
            val viewModel: DriverViewModel = viewModel()
            val state by viewModel.state.collectAsStateWithLifecycle()

            TppKierowcaTheme(themeMode = state.themeMode) {
                DriverApp(
                    viewModel = viewModel,
                    initialRouteId = notificationRouteId,
                    initialRouteTarget = notificationRouteTarget,
                    initialLeaveRequestId = notificationLeaveRequestId,
                )
            }
        }
    }

    override fun onNewIntent(intent: android.content.Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
        notificationRouteId = intent.getStringExtra(EXTRA_ROUTE_ID)
        notificationRouteTarget = intent.getStringExtra(EXTRA_ROUTE_TARGET)
        notificationLeaveRequestId = intent.getStringExtra(EXTRA_LEAVE_REQUEST_ID)
    }

    companion object {
        const val EXTRA_ROUTE_ID = "pl.firmatpp.kierowca.EXTRA_ROUTE_ID"
        const val EXTRA_ROUTE_TARGET = "pl.firmatpp.kierowca.EXTRA_ROUTE_TARGET"
        const val EXTRA_LEAVE_REQUEST_ID = "pl.firmatpp.kierowca.EXTRA_LEAVE_REQUEST_ID"
        const val ROUTE_TARGET_DETAIL = "detail"
        const val ROUTE_TARGET_FINISH = "finish"
    }
}
