package com.tomhempel.unityudp.data

import android.content.Context
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class StorageService(context: Context) {

    private val gson = Gson()
    private val prefs = context.getSharedPreferences("unity_udp_prefs", Context.MODE_PRIVATE)

    fun loadProjects(): List<ProjectData> {
        val json = prefs.getString("projects", null) ?: return emptyList()
        return try {
            val type = object : TypeToken<List<ProjectData>>() {}.type
            gson.fromJson(json, type) ?: emptyList()
        } catch (e: Exception) {
            emptyList()
        }
    }

    fun saveProjects(projects: List<ProjectData>) {
        prefs.edit().putString("projects", gson.toJson(projects)).apply()
    }
}
