package com.gh.gamecenter.room.converter

import androidx.room.TypeConverter
import com.gh.gamecenter.common.utils.toJson
import com.gh.gamecenter.common.utils.toObject
import com.gh.gamecenter.feature.entity.AcctGameInfo
import com.gh.gamecenter.feature.entity.GameEntity
import com.google.gson.Gson

class AcctGameInfoConverter {

    @TypeConverter
    fun fromZoneInfo(zoneInfo: AcctGameInfo.ZoneInfo): String {
        // 使用Gson将ZoneInfo转换为JSON字符串
        return zoneInfo.toJson()
    }

    @TypeConverter
    fun toZoneInfo(value: String): AcctGameInfo.ZoneInfo {
        // 将JSON字符串转回ZoneInfo对象
        return value.toObject<AcctGameInfo.ZoneInfo>() ?: AcctGameInfo.ZoneInfo(0)
    }

    @TypeConverter
    fun fromZoneList(zoneList: List<AcctGameInfo>): String {
        return zoneList.toJson()
    }

    @TypeConverter
    fun toZoneList(value: String): List<AcctGameInfo> {
        return value.toObject<List<AcctGameInfo>>() ?: listOf()
    }

    @TypeConverter
    fun fromGame(game: GameEntity): String {
        return game.toJson()
    }

    @TypeConverter
    fun toGame(value: String): GameEntity? {
        return value.toObject<GameEntity>()
    }

}