package com.amz.genie.helpers

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit

object Preferences {
    const val PREFS_NAME = "genieData"
//    const val API_URL = "https://api-genie.naminaniar.com/api/"
//    const val API_URL = "http://192.168.21.102:5000/api/"
//    const val API_URL = "http://192.160.3.17:5000/api/"
//    const val API_URL = "http://192.168.18.19:5000/api/"
    const val API_URL = "http://192.168.1.22:5000/api/"
    const val BASE_UPLOAD_URL = "https://api-genie.naminaniar.com/uploads"
    const val PROFILE_PICTURE_URL = "https://api-genie.naminaniar.com/profile_picture"
    private const val USER = "user"
    private const val REFRESH_TOKEN = "refreshToken"
    private const val ACCESS_TOKEN = "accessToken"
    private const val KEY_LAST_FCM_TOPIC = "last_fcm_topic"

    fun getLastFcmTopic(ctx: Context): String {
        val sp = ctx.getSharedPreferences("prefs", Context.MODE_PRIVATE)
        return sp.getString(KEY_LAST_FCM_TOPIC, "") ?: ""
    }

    fun setLastFcmTopic(ctx: Context, topic: String) {
        val sp = ctx.getSharedPreferences("prefs", Context.MODE_PRIVATE)
        sp.edit { putString(KEY_LAST_FCM_TOPIC, topic) }
    }

    fun preferences(context: Context): SharedPreferences =
        context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)

    fun setRefreshToken(context: Context, value: String?) {
        preferences(context).edit { putString(REFRESH_TOKEN, value) }
    }

    fun setAccessToken(context: Context, value: String?) {
        preferences(context).edit { putString(ACCESS_TOKEN, value) }
    }

    fun getAccessToken(context: Context): String? =
        preferences(context).getString(ACCESS_TOKEN, null)

    fun getRefreshToken(context: Context): String? =
        preferences(context)
            .getString(REFRESH_TOKEN, null)

    fun setUserData(context: Context, value: String?) {
        preferences(context).edit { putString(USER, value) }
    }

    fun getUserData(context: Context): String? =
        preferences(context).getString(USER, null)

    fun clearAll(context: Context) {
        preferences(context).edit {
            clear()
        }
    }
}