@file:OptIn(ExperimentalTime::class)

package eu.gaudian.translator.model.db

import androidx.room.TypeConverter
import eu.gaudian.translator.model.Language
import eu.gaudian.translator.model.VocabularyStage
import kotlinx.datetime.LocalDate
import kotlinx.serialization.json.Json
import kotlin.time.ExperimentalTime
import kotlin.time.Instant

@Suppress("HardCodedStringLiteral", "unused")
class Converters {
    @TypeConverter
    fun fromTimestamp(value: Long?): Instant? {
        return value?.let { Instant.fromEpochMilliseconds(it) }
    }

    @TypeConverter
    fun dateToTimestamp(date: Instant?): Long? {
        return date?.toEpochMilliseconds()
    }

    @TypeConverter
    fun fromLocalDate(value: String?): LocalDate? {
        return value?.let { LocalDate.parse(it) }
    }

    @TypeConverter
    fun toLocalDate(date: LocalDate?): String? {
        return date?.toString()
    }

    @TypeConverter
    fun fromLanguageList(languages: List<Language>?): String? {
        return languages?.let { Json.encodeToString(it) }
    }

    @TypeConverter
    fun toLanguageList(json: String?): List<Language>? {
        return json?.let { Json.decodeFromString<List<Language>>(it) }
    }

    @TypeConverter
    fun fromStageList(stages: List<VocabularyStage>?): String? {
        return stages?.let { Json.encodeToString(it) }
    }

    @TypeConverter
    fun toStageList(json: String?): List<VocabularyStage>? {
        return json?.let { Json.decodeFromString<List<VocabularyStage>>(it) }
    }
}