package com.example.lexilearn.data.repository

import android.content.Context
import com.example.lexilearn.data.local.SharedPrefHelper
import com.example.lexilearn.data.remote.FirebaseHelper
import com.example.lexilearn.data.model.MaterialDataModel
import com.example.lexilearn.data.model.UserDataModel
import com.google.firebase.database.FirebaseDatabase
class MaterialRepository(private val context: Context) {
    private val sharedPrefHelper = SharedPrefHelper(context)
    private val firebaseHelper = FirebaseHelper(context)

    fun getMaterialData(filterMaterial: String, callback: (List<MaterialDataModel>) -> Unit) {
        sharedPrefHelper.getMaterialList(filterMaterial)?.let {
            callback(it)
        } ?: firebaseHelper.fetchMaterials(filterMaterial) { materials ->
            sharedPrefHelper.saveMaterialList(filterMaterial, materials)
            callback(materials)
        }
    }

    fun getAllMaterialData(filterMaterial: String, callback: (List<MaterialDataModel>) -> Unit) {
        sharedPrefHelper.getMaterialList(filterMaterial)?.let {
            callback(it)
        } ?: firebaseHelper.fetchAllMaterials { materials ->
            sharedPrefHelper.saveMaterialList(filterMaterial, materials)
            callback(materials)
        }
    }
}