package mx.trackermap.TrackerMap.controllers import android.util.Log import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.launch import mx.trackermap.TrackerMap.client.apis.GeofencesApi import mx.trackermap.TrackerMap.client.apis.ReportsApi import mx.trackermap.TrackerMap.client.models.EventInformation import mx.trackermap.TrackerMap.client.models.Position import mx.trackermap.TrackerMap.client.models.Stop import mx.trackermap.TrackerMap.utils.ReportDates @DelicateCoroutinesApi class ReportController( private val reportsApi: ReportsApi, private val geofencesApi: GeofencesApi ) { sealed class Report { class PositionsReport(val positions: Array) : Report() class EventsReport(val events: Array) : Report() class StopsReport(val stops: Array) : Report() class XlsxReport(val data: ByteArray) : Report() object LoadingReport: Report() } enum class ReportType { POSITIONS, EVENTS, STOPS } val reportFlow: MutableStateFlow = MutableStateFlow(Report.LoadingReport) fun fetchReport( deviceId: Int, reportType: ReportType?, reportPeriod: ReportDates.ReportPeriod?, xlsx: Boolean = false ) { if (reportType == null || reportPeriod == null) { return } val (currentDate, previousDate) = ReportDates.getDates(reportPeriod) Log.d("UnitReportsVM", "Current report type: $reportType") Log.d("UnitReportsVM", "Current report period: $reportType") Log.d("UnitReportsVM", "Current date:${currentDate}") Log.d("UnitReportsVM", "Previous date:${previousDate}") if (!xlsx) { reportFlow.value = Report.LoadingReport } GlobalScope.launch { when (reportType) { ReportType.POSITIONS -> fetchPositions(deviceId, previousDate, currentDate, xlsx) ReportType.EVENTS -> fetchEvents(deviceId, previousDate, currentDate, xlsx) ReportType.STOPS -> fetchStops(deviceId, previousDate, currentDate, xlsx) } } } private suspend fun fetchPositions( deviceId: Int, from: String, to: String, xlsx: Boolean ) { Log.d("UnitReportsVM", "Fetching positions") if (!xlsx) { val result = reportsApi.reportsRouteGet(from, to, deviceId) Log.d("UnitReportsVM", "Positions report: $result") reportFlow.value = Report.PositionsReport(result) } else { val result = reportsApi.reportsRouteGetXlsx(from, to, deviceId) Log.d("UnitReportsVM", "Positions report: $result") reportFlow.value = Report.XlsxReport(result) } } private suspend fun fetchEvents( deviceId: Int, from: String, to: String, xlsx: Boolean ) { Log.d("UnitReportsVM", "Fetching events") if (!xlsx) { val positionsResult = reportsApi.reportsRouteGet(from, to, deviceId) val eventsResult = reportsApi.reportsEventsGet(from, to, deviceId) val geofencesResult = geofencesApi.geofencesGet(all = true) val result = mutableListOf() eventsResult.forEach { event -> result.add(EventInformation( event = event, position = positionsResult.find { it.id == event.positionId }, geofence = geofencesResult.find { it.id == event.geofenceId } )) } Log.d("UnitReportsVM", "Events report: $result") reportFlow.value = Report.EventsReport(result.toTypedArray()) } else { val result = reportsApi.reportsEventsGetXlsx(from, to, deviceId) Log.d("UnitReportsVM", "Events report: $result") reportFlow.value = Report.XlsxReport(result) } } private suspend fun fetchStops( deviceId: Int, from: String, to: String, xlsx: Boolean ) { Log.d("UnitReportsVM", "Fetching stops") if (!xlsx) { val result = reportsApi.reportsStopsGet(from, to, deviceId) Log.d("UnitReportsVM", "Stops report: $result") reportFlow.value = Report.StopsReport(result) } else { val result = reportsApi.reportsStopsGetXlsx(from, to, deviceId) Log.d("UnitReportsVM", "Stops report: $result") reportFlow.value = Report.XlsxReport(result) } } }