package pl.firmatpp.kierowca

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
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
import pl.firmatpp.kierowca.update.PlayAppUpdateState
import pl.firmatpp.kierowca.update.PlayInAppUpdateController

class MainActivity : ComponentActivity() {
    private var notificationRouteId by mutableStateOf<String?>(null)
    private var notificationRouteTarget by mutableStateOf<String?>(null)
    private var notificationLeaveRequestId by mutableStateOf<String?>(null)
    private var playAppUpdateState by mutableStateOf(PlayAppUpdateState())
    private lateinit var playInAppUpdates: PlayInAppUpdateController
    private val appUpdateLauncher = registerForActivityResult(
        ActivityResultContracts.StartIntentSenderForResult(),
    ) { result ->
        if (::playInAppUpdates.isInitialized) {
            playInAppUpdates.onUpdateFlowResult(result.resultCode)
        }
    }

    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)
        playInAppUpdates = PlayInAppUpdateController(
            activity = this,
            launcher = appUpdateLauncher,
            onStateChanged = { playAppUpdateState = it },
        )
        setContent {
            val viewModel: DriverViewModel = viewModel()
            val state by viewModel.state.collectAsStateWithLifecycle()

            TppKierowcaTheme(themeMode = state.themeMode) {
                DriverApp(
                    viewModel = viewModel,
                    initialRouteId = notificationRouteId,
                    initialRouteTarget = notificationRouteTarget,
                    initialLeaveRequestId = notificationLeaveRequestId,
                    playAppUpdateState = playAppUpdateState,
                    onStartAppUpdate = playInAppUpdates::startUpdate,
                    onCompleteAppUpdate = playInAppUpdates::completeDownloadedUpdate,
                )
            }
        }
        playInAppUpdates.checkForUpdate()
    }

    override fun onResume() {
        super.onResume()
        if (::playInAppUpdates.isInitialized) {
            playInAppUpdates.checkForUpdate()
        }
    }

    override fun onDestroy() {
        if (::playInAppUpdates.isInitialized) {
            playInAppUpdates.dispose()
        }
        super.onDestroy()
    }

    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"
    }
}
