package mx.trackermap.TrackerMap.android.details.reports import androidx.lifecycle.* import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch import mx.trackermap.TrackerMap.client.models.EventInformation import mx.trackermap.TrackerMap.client.models.Geofence import mx.trackermap.TrackerMap.controllers.GeofencesController import mx.trackermap.TrackerMap.controllers.ReportController import mx.trackermap.TrackerMap.utils.ReportDates import org.koin.core.component.KoinComponent import org.koin.core.component.inject @DelicateCoroutinesApi class UnitReportsViewModel( savedStateHandle: SavedStateHandle ) : ViewModel(), KoinComponent { private val geofencesController: GeofencesController by inject() private val reportController: ReportController by inject() private var _deviceId = savedStateHandle.getLiveData("deviceId", 0) private val _reportType: MutableLiveData = savedStateHandle.getLiveData("reportType", null) private val _reportPeriod: MutableLiveData = savedStateHandle.getLiveData("reportPeriod", null) private val _report: MutableLiveData = MutableLiveData() private val _geofences: MutableLiveData> = MutableLiveData() val deviceId: LiveData get() = _deviceId val reportType: LiveData get() = _reportType val reportPeriod: LiveData get() = _reportPeriod val report: LiveData get() = _report val geofences: LiveData> get() = _geofences init { viewModelScope.launch { setupTypeObserver() } viewModelScope.launch { setupPeriodObserver() } viewModelScope.launch { setupReportObserver() } viewModelScope.launch { setupGeofenceObserver() } } private suspend fun setupTypeObserver() { _reportType.asFlow().collect { fetchReport() } } private suspend fun setupPeriodObserver() { _reportPeriod.asFlow().collect { fetchReport() } } private suspend fun setupReportObserver() { reportController.reportFlow.collect { _report.value = it } } private suspend fun setupGeofenceObserver() { geofencesController.geofencesFlow.collect { _geofences.value = it.values.toTypedArray() } } fun setDeviceId (id: Int) { _deviceId.value = id } fun setReportType(reportType: ReportController.ReportType) { _reportType.value = reportType } fun setReportPeriod(reportPeriod: ReportDates.ReportPeriod) { _reportPeriod.value = reportPeriod } fun fetchReportXlsx() { fetchReport(true) } fun clearReport() { _report.value = null } fun fetchReport(xlsx: Boolean = false) { if (_reportType.value == null || _reportPeriod.value == null) { return } viewModelScope.launch { _deviceId.value?.let { id -> reportController.fetchReport( deviceId = id, reportType = _reportType.value, reportPeriod = _reportPeriod.value, xlsx = xlsx, eventTypes = listOf( EventInformation.Type.DEVICE_INACTIVE, EventInformation.Type.DEVICE_MOVING, EventInformation.Type.DEVICE_STOPPED, EventInformation.Type.DEVICE_OVERSPEED, EventInformation.Type.DEVICE_FUEL_DROP, EventInformation.Type.COMMAND_RESULT, EventInformation.Type.GEOFENCE_ENTER, EventInformation.Type.GEOFENCE_EXIT, EventInformation.Type.ALARM, EventInformation.Type.IGNITION_ON, EventInformation.Type.IGNITION_OFF, EventInformation.Type.MAINTENANCE, EventInformation.Type.TEXT_MESSAGE, EventInformation.Type.DRIVER_CHANGED, EventInformation.Type.UNKNOWN ) ) } } } }