package com.adealink.weparty.profile.datasource.local

import android.content.Context
import com.adealink.frame.storage.sp.TypeDelegationPrefs
import com.adealink.frame.util.AppUtil
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.storage.PREF_SEARCH

object SearchLocalService : TypeDelegationPrefs(
    prefs = {
        AppUtil.appContext.getSharedPreferences(PREF_SEARCH, Context.MODE_PRIVATE)
    },
    userId = {
        ProfileModule.getMyUid()
    }
) {

    const val MAX_HISTORY_COUNT = 10

    /**
     *
     */
    var searchHistory: String by PrefUserKey("key_search_history", "")

    fun getSearchHistory(): List<String> {
        return searchHistory.split(",").filter { it.isNotEmpty() }.toList()
    }

    fun saveSearchHistory(history: List<String>) {
        //最大保留10条历史记录
        searchHistory = history.take(MAX_HISTORY_COUNT).joinToString(separator = ",")
    }

    fun clearSearchHistory() {
        searchHistory = ""
    }

}