package de.ypchat.android.core

import android.content.Context

data class SavedProfile(
    val nickname: String = "",
    val gender: String = "",
    val age: Int = 18,
    val country: String = "Germany"
)

class ProfileStore(context: Context) {
    private val preferences = context.getSharedPreferences("ypchat_profile", Context.MODE_PRIVATE)

    fun read(): SavedProfile = SavedProfile(
        nickname = preferences.getString(KEY_NICKNAME, "").orEmpty(),
        gender = preferences.getString(KEY_GENDER, "").orEmpty(),
        age = preferences.getInt(KEY_AGE, 18),
        country = preferences.getString(KEY_COUNTRY, "Germany").orEmpty()
    )

    fun write(profile: SavedProfile) {
        preferences.edit()
            .putString(KEY_NICKNAME, profile.nickname.trim())
            .putString(KEY_GENDER, profile.gender)
            .putInt(KEY_AGE, profile.age)
            .putString(KEY_COUNTRY, profile.country)
            .apply()
    }

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

    private companion object {
        const val KEY_NICKNAME = "nickname"
        const val KEY_GENDER = "gender"
        const val KEY_AGE = "age"
        const val KEY_COUNTRY = "country"
    }
}
