package com.example.lexilearn.data.local

import android.content.Context
import android.content.SharedPreferences
import com.example.lexilearn.data.model.MaterialDataModel
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class SharedPrefHelper(context: Context) {

    private val sharedPreferences: SharedPreferences =
        context.getSharedPreferences("MaterialDataPrefs", Context.MODE_PRIVATE)

    fun saveMaterialList(materialList: List<MaterialDataModel>) {
        val editor = sharedPreferences.edit()
        val json = Gson().toJson(materialList)
        editor.putString("material_list", json)
        editor.apply()
    }

    fun getMaterialList(): List<MaterialDataModel>? {
        val json = sharedPreferences.getString("material_list", null)
        return if (json != null) {
            val type = object : TypeToken<List<MaterialDataModel>>() {}.type
            Gson().fromJson(json, type)
        } else {
            null
        }
    }

    fun clearMaterialList() {
        sharedPreferences.edit().remove("material_list").apply()
    }
}
