package com.example.lexilearn.data.local

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

class SharedPrefHelper(context: Context) {

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

    // 🔥 Simpan User ID setelah login
    fun saveUserId(userId: String) {
        val editor = sharedPreferences.edit()
        editor.putString("user_id", userId)
        editor.apply()
    }

    // 🔥 Ambil User ID yang tersimpan
    fun getUserId(): String? {
        return sharedPreferences.getString("user_id", null)
    }

    // 🔥 Hapus data user saat logout
    fun clearUserData() {
        sharedPreferences.edit().clear().apply()
    }

    // 🔥 Fungsi SharedPreferences untuk menyimpan daftar material (kode lama, tetap dipertahankan)
    fun saveMaterialList(filterMaterial: String, materialList: List<MaterialDataModel>) {
        val editor = sharedPreferences.edit()
        val json = Gson().toJson(materialList)
        editor.putString(" $filterMaterial", json)
        editor.apply()
    }

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

    fun getCombinedMaterialList(): List<MaterialDataModel> {
        val categories = listOf("animal", "limb", "family", "house")
        val combinedList = mutableListOf<MaterialDataModel>()

        for (category in categories) {
            getMaterialList(category)?.let { combinedList.addAll(it) }
        }

        return combinedList
    }

    fun getRandomMaterials(nData: Int): List<MaterialDataModel> {
        val combinedList = getCombinedMaterialList()

        return if (combinedList.size <= nData) {
            combinedList // Jika jumlah data kurang dari nData, kembalikan semua
        } else {
            combinedList.shuffled().take(nData) // Acak dan ambil sejumlah nData
        }
    }


    fun clearMaterialList(filterMaterial: String) {
        sharedPreferences.edit().remove("material_list_$filterMaterial").apply()
    }

    fun clearAllMaterialData() {
        sharedPreferences.edit().clear().apply()
    }

    fun getUserData(): UserModel? {
        val json = sharedPreferences.getString("user_data", null)
        return if (json != null) {
            Gson().fromJson(json, UserModel::class.java)
        } else {
            null
        }
    }

    fun saveUserData(user: UserModel) {
        val editor = sharedPreferences.edit()
        val json = Gson().toJson(user)
        editor.putString("user_data", json)
        editor.apply()
    }
}
