package com.pixelized.rplexicon.facotry import android.net.Uri import com.google.api.services.sheets.v4.model.ValueRange import com.pixelized.rplexicon.model.Location import com.pixelized.rplexicon.utilitary.exceptions.IncompatibleSheetStructure import com.pixelized.rplexicon.utilitary.extentions.checkSheetStructure import com.pixelized.rplexicon.utilitary.extentions.sheet import javax.inject.Inject class LocationParser @Inject constructor() { @Throws(IncompatibleSheetStructure::class) fun parse(data: ValueRange): List { var id = 0 var sheetStructure: Map? = null return data.values.sheet()?.mapIndexedNotNull { index, item -> when { index == 0 -> { sheetStructure = item.checkSheetStructure(COLUMNS) null } item is List<*> -> { val name = item.getOrNull(sheetStructure.name) as? String val uri = item.getOrNull(sheetStructure.uri)?.toString()?.let { Uri.parse(it) } if (name != null && uri != null) { Location( id = id++, sheetIndex = index, name = name, uri = uri, marquees = emptyList(), ) } else { null } } else -> null } } ?: emptyList() } private val Map?.name: Int get() = this?.getValue(COLUMNS[0]) ?: 0 private val Map?.uri: Int get() = this?.getValue(COLUMNS[1]) ?: 1 companion object { private val COLUMNS = listOf( "Nom", "Carte" ) } }