package com.assimilate.alltrans.model

import android.content.Context
import android.content.SharedPreferences
import com.assimilate.alltrans.MyApp
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

object PreferenceLanguageUtils {

    private const val SP_NAME = "sp_language"
    private const val KEY_RECENT_LANGUAGES = "recent_languages"
    private const val MAX_RECENT_LANGUAGES = 5
    private const val PREF_KEY_FIRST_TIME = "first_time"
    private const val PREF_KEY_FIRST_ALLOW = "first_allow"
    private val LANGUAGE_LIST_TYPE = object : TypeToken<List<Language>>() {}.type

    @Volatile
    private var sharedPreferences: SharedPreferences? = null

    private fun getSharedPreferences(): SharedPreferences {
        return sharedPreferences ?: synchronized(this) {
            val instance = MyApp.applicationContext()
                .getSharedPreferences(SP_NAME, Context.MODE_PRIVATE)
            sharedPreferences = instance
            instance
        }
    }


    fun putString(key: String, value: String) {
        getSharedPreferences().edit().putString(key, value).apply()
    }

    fun getString(key: String, defValue: String = "English"): String {
        val value = getSharedPreferences().getString(key, defValue) ?: defValue
        return value
    }

    fun clear() {
        getSharedPreferences().edit().clear().apply()
    }

    fun clearExceptReferrerUrl() {
        val sharedPreferences = getSharedPreferences()
        val editor = sharedPreferences.edit()
        sharedPreferences.all.keys.forEach { key ->
            if (key != "refe_url") {
                editor.remove(key)
            }
        }
        editor.apply()

    }

    fun addRecentLanguage(language: Language) {
        val recentLanguages = getRecentLanguages().toMutableList()
        recentLanguages.remove(language) // Remove if already exists
        recentLanguages.add(0, language) // Add to the beginning
        if (recentLanguages.size > MAX_RECENT_LANGUAGES) {
            recentLanguages.removeAt(recentLanguages.size - 1) // Remove the oldest
        }
        saveRecentLanguages(recentLanguages)
    }


    fun getRecentLanguages(): List<Language> {
        val json = getSharedPreferences().getString(KEY_RECENT_LANGUAGES, null)
        return if (json != null) {
            Gson().fromJson(json, LANGUAGE_LIST_TYPE)
        } else {
            emptyList()
        }
    }

    private fun saveRecentLanguages(languages: List<Language>) {
        val json = Gson().toJson(languages)
        getSharedPreferences().edit().putString(KEY_RECENT_LANGUAGES, json).apply()
    }

    // 检查是否是第一次进入应用
    fun isFirstTime(): Boolean {
        return getSharedPreferences().getBoolean(PREF_KEY_FIRST_TIME, true)
    }

    // 设置已经不是第一次进入应用了
    fun setNotFirstTime() {
        getSharedPreferences().edit().putBoolean(PREF_KEY_FIRST_TIME, false).apply()
    }


    // 检查是否是第一次进入应用
    fun isFirstAllow(): Boolean {
        return getSharedPreferences().getBoolean(PREF_KEY_FIRST_ALLOW, true)
    }

    // 设置已经不是第一次进入应用了
    fun setNotFirstAllow() {
        getSharedPreferences().edit().putBoolean(PREF_KEY_FIRST_ALLOW, false).apply()
    }




}
