package de.sebse.fuplanner2.database

import androidx.room.TypeConverter
import de.sebse.fuplanner2.auth.UserCookies
import de.sebse.fuplanner2.blackboard.BlackboardInfo
import de.sebse.fuplanner2.whiteboard.WhiteboardInfo
import java.util.*

class Converters {
    private val delimiter = "~@@~"

    @TypeConverter
    fun fromUserCookies(value: UserCookies?): String? {
        return value?.toString()
    }
    @TypeConverter
    fun toUserCookies(value: String?): UserCookies? {
        return value?.let { UserCookies(it) }
    }



    @TypeConverter
    fun fromBlackboardInfo(value: BlackboardInfo?): String? {
        return value?.toString()
    }
    @TypeConverter
    fun toBlackboardInfo(value: String?): BlackboardInfo? {
        return value?.let { BlackboardInfo(it) }
    }



    @TypeConverter
    fun fromWhiteboardInfo(value: WhiteboardInfo?): String? {
        return value?.toString()
    }
    @TypeConverter
    fun toWhiteboardInfo(value: String?): WhiteboardInfo? {
        return value?.let { WhiteboardInfo(it) }
    }



    @TypeConverter
    fun fromDate(value: Date?): Long? {
        return value?.time
    }
    @TypeConverter
    fun toDate(value: Long?): Date? {
        return value?.let { Date(it) }
    }



    @TypeConverter
    fun fromStringSet(value: HashSet<String>?): String? {
        return value?.joinToString(separator = delimiter)
    }
    @TypeConverter
    fun toStringSet(value: String?): HashSet<String>? {
        return value?.split(delimiter)?.toHashSet()
    }



    @TypeConverter
    fun fromLecturerList(value: List<Lecturer>?): String? {
        return value?.joinToString(separator = delimiter) { it.toJsonString() }
    }
    @TypeConverter
    fun toLecturerList(value: String?): List<Lecturer>? {
        return value?.split(delimiter)?.mapNotNull { Lecturer.fromString(it) }
    }



    @TypeConverter
    fun fromAttachmentList(value: List<Attachment>?): String? {
        return value?.joinToString(separator = delimiter) { it.toJsonString() }
    }
    @TypeConverter
    fun toAttachmentList(value: String?): List<Attachment>? {
        return value?.split(delimiter)?.mapNotNull { Attachment.fromString(it) }
    }


}
