<commit_before>package jp.toastkid.lib.preference

import android.content.Context
import android.content.SharedPreferences
import android.content.res.Configuration
import android.graphics.Color
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import jp.toastkid.lib.R
import jp.toastkid.lib.Urls
import java.io.File
import java.util.Locale
import kotlin.math.max

/**
 * Preferences wrapper.

 * @author toastkidjp
 */
class PreferenceApplier(private val context: Context) {

    @Suppress("unused")
    @Deprecated("These keys are deprecated.")
    private enum class DefunctKey {
        USE_DAILY_ALARM, USE_INTERNAL_BROWSER, MENU_POS, USE_INVERSION, ENABLE_APP_SEARCH,
        LAST_AD_DATE, FULL_SCREEN
    }

    private enum class Key {
        BG_COLOR, FONT_COLOR,
        ENABLE_SUGGESTION, ENABLE_SEARCH_HISTORY, ENABLE_VIEW_HISTORY, ENABLE_URL_MODULE,
        ENABLE_TREND_MODULE, ENABLE_FAVORITE_SEARCH, DISABLE_SEARCH_CATEGORIES,
        BG_IMAGE,
        USE_NOTIFICATION_WIDGET, USE_DAILY_NOTIFICATION, RETAIN_TABS, USE_JS,
        LOAD_IMAGE, SAVE_FORM, USER_AGENT, HOME_URL, USE_COLOR_FILTER, FILTER_COLOR,
        DEFAULT_SEARCH_ENGINE, ENABLE_SEARCH_QUERY_EXTRACT, ENABLE_SEARCH_WITH_CLIP, START_UP, SAVE_VIEW_HISTORY,
        SCREEN_MODE, WIFI_ONLY_MODE, AD_REMOVE, WEB_VIEW_POOL_SIZE,
        EDITOR_BACKGROUND_COLOR, EDITOR_FONT_COLOR, EDITOR_CURSOR_COLOR, EDITOR_HIGHLIGHT_COLOR,
        EDITOR_FONT_SIZE, CAMERA_FAB_BUTTON_POSITION_X, CAMERA_FAB_BUTTON_POSITION_Y,
        MENU_FAB_BUTTON_POSITION_X, MENU_FAB_BUTTON_POSITION_Y,
        WEB_VIEW_BACKGROUND_ALPHA, RSS_READER_TARGETS, IMAGE_VIEWER_EXCLUDED_PATHS,
        IMAGE_VIEWER_SORT_TYPE, BROWSER_DARK_MODE, USE_TITLE_FILTER,
        ARTICLE_LIST_SORT_TYPE, LAST_CLIPPED_WORD,
        NUMBER_PLACE_MASKING_COUNT, NUMBER_PLACE_LAST_GAME_PATH
    }

    private val preferences: SharedPreferences =
            context.getSharedPreferences(javaClass.canonicalName, Context.MODE_PRIVATE)

    var color: Int
        get() = preferences.getInt(Key.BG_COLOR.name, ContextCompat.getColor(context, R.color.colorPrimaryDark))
        set(color) = preferences.edit().putInt(Key.BG_COLOR.name, color).apply()

    var fontColor: Int
        get() = preferences.getInt(Key.FONT_COLOR.name, ContextCompat.getColor(context, R.color.default_text_color))
        set(color) = preferences.edit().putInt(Key.FONT_COLOR.name, color).apply()

    fun colorPair(): ColorPair = ColorPair(color, fontColor)

    val isEnableSuggestion: Boolean
        get() = preferences.getBoolean(Key.ENABLE_SUGGESTION.name, true)

    val isDisableSuggestion: Boolean
        get() = !isEnableSuggestion

    fun switchEnableSuggestion() {
        preferences.edit().putBoolean(Key.ENABLE_SUGGESTION.name, !isEnableSuggestion).apply()
    }

    val isEnableSearchHistory: Boolean
        get() = preferences.getBoolean(Key.ENABLE_SEARCH_HISTORY.name, true)

    fun switchEnableSearchHistory() {
        preferences.edit().putBoolean(Key.ENABLE_SEARCH_HISTORY.name, !isEnableSearchHistory)
                .apply()
    }

    val isEnableFavoriteSearch: Boolean
        get() = preferences.getBoolean(Key.ENABLE_FAVORITE_SEARCH.name, true)

    fun switchEnableFavoriteSearch() {
        preferences.edit().putBoolean(Key.ENABLE_FAVORITE_SEARCH.name, !isEnableFavoriteSearch)
                .apply()
    }

    val isEnableViewHistory: Boolean
        get() = preferences.getBoolean(Key.ENABLE_VIEW_HISTORY.name, true)

    fun switchEnableViewHistory() {
        preferences.edit().putBoolean(Key.ENABLE_VIEW_HISTORY.name, !isEnableViewHistory)
                .apply()
    }

    fun isEnableUrlModule(): Boolean {
        return preferences.getBoolean(Key.ENABLE_URL_MODULE.name, true)
    }

    fun switchEnableUrlModule() {
        preferences.edit().putBoolean(Key.ENABLE_URL_MODULE.name, !isEnableUrlModule())
                .apply()
    }

    fun isEnableTrendModule(): Boolean {
        return preferences.getBoolean(Key.ENABLE_TREND_MODULE.name, true)
    }

    fun switchEnableTrendModule() {
        preferences.edit().putBoolean(Key.ENABLE_TREND_MODULE.name, !isEnableTrendModule())
                .apply()
    }

    fun addDisableSearchCategory(name: String) {
        val set = readDisableSearchCategory()
        set?.add(name)
        preferences.edit().putStringSet(Key.DISABLE_SEARCH_CATEGORIES.name, set).apply()
    }

    fun removeDisableSearchCategory(name: String) {
        val set = readDisableSearchCategory()
        set?.remove(name)
        preferences.edit().putStringSet(Key.DISABLE_SEARCH_CATEGORIES.name, set).apply()
    }

    fun readDisableSearchCategory(): MutableSet<String>? {
        return preferences.getStringSet(Key.DISABLE_SEARCH_CATEGORIES.name, mutableSetOf())
    }

    var backgroundImagePath: String
        get() = preferences.getString(Key.BG_IMAGE.name, "") ?: ""
        set(path) = preferences.edit().putString(Key.BG_IMAGE.name, path).apply()

    fun removeBackgroundImagePath() {
        preferences.edit().remove(Key.BG_IMAGE.name).apply()
    }

    fun isFirstLaunch(): Boolean {
        val firstLaunch = File(context.filesDir, "firstLaunch")
        if (firstLaunch.exists()) {
            return false
        }
        firstLaunch.mkdirs()<commit_msg>feature #958 Add setting value.<commit_after>package jp.toastkid.lib.preference

import android.content.Context
import android.content.SharedPreferences
import android.content.res.Configuration
import android.graphics.Color
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import jp.toastkid.lib.R
import jp.toastkid.lib.Urls
import java.io.File
import java.util.Locale
import kotlin.math.max

/**
 * Preferences wrapper.

 * @author toastkidjp
 */
class PreferenceApplier(private val context: Context) {

    @Suppress("unused")
    @Deprecated("These keys are deprecated.")
    private enum class DefunctKey {
        USE_DAILY_ALARM, USE_INTERNAL_BROWSER, MENU_POS, USE_INVERSION, ENABLE_APP_SEARCH,
        LAST_AD_DATE, FULL_SCREEN
    }

    private enum class Key {
        BG_COLOR, FONT_COLOR,
        ENABLE_SUGGESTION, ENABLE_SEARCH_HISTORY, ENABLE_VIEW_HISTORY, ENABLE_URL_MODULE,
        ENABLE_TREND_MODULE, ENABLE_FAVORITE_SEARCH, DISABLE_SEARCH_CATEGORIES,
        BG_IMAGE, SHOW_DISPLAY_EFFECT,
        USE_NOTIFICATION_WIDGET, USE_DAILY_NOTIFICATION, RETAIN_TABS, USE_JS,
        LOAD_IMAGE, SAVE_FORM, USER_AGENT, HOME_URL, USE_COLOR_FILTER, FILTER_COLOR,
        DEFAULT_SEARCH_ENGINE, ENABLE_SEARCH_QUERY_EXTRACT, ENABLE_SEARCH_WITH_CLIP, START_UP, SAVE_VIEW_HISTORY,
        SCREEN_MODE, WIFI_ONLY_MODE, AD_REMOVE, WEB_VIEW_POOL_SIZE,
        EDITOR_BACKGROUND_COLOR, EDITOR_FONT_COLOR, EDITOR_CURSOR_COLOR, EDITOR_HIGHLIGHT_COLOR,
        EDITOR_FONT_SIZE, CAMERA_FAB_BUTTON_POSITION_X, CAMERA_FAB_BUTTON_POSITION_Y,
        MENU_FAB_BUTTON_POSITION_X, MENU_FAB_BUTTON_POSITION_Y,
        WEB_VIEW_BACKGROUND_ALPHA, RSS_READER_TARGETS, IMAGE_VIEWER_EXCLUDED_PATHS,
        IMAGE_VIEWER_SORT_TYPE, BROWSER_DARK_MODE, USE_TITLE_FILTER,
        ARTICLE_LIST_SORT_TYPE, LAST_CLIPPED_WORD,
        NUMBER_PLACE_MASKING_COUNT, NUMBER_PLACE_LAST_GAME_PATH
    }

    private val preferences: SharedPreferences =
            context.getSharedPreferences(javaClass.canonicalName, Context.MODE_PRIVATE)

    var color: Int
        get() = preferences.getInt(Key.BG_COLOR.name, ContextCompat.getColor(context, R.color.colorPrimaryDark))
        set(color) = preferences.edit().putInt(Key.BG_COLOR.name, color).apply()

    var fontColor: Int
        get() = preferences.getInt(Key.FONT_COLOR.name, ContextCompat.getColor(context, R.color.default_text_color))
        set(color) = preferences.edit().putInt(Key.FONT_COLOR.name, color).apply()

    fun colorPair(): ColorPair = ColorPair(color, fontColor)

    val isEnableSuggestion: Boolean
        get() = preferences.getBoolean(Key.ENABLE_SUGGESTION.name, true)

    val isDisableSuggestion: Boolean
        get() = !isEnableSuggestion

    fun switchEnableSuggestion() {
        preferences.edit().putBoolean(Key.ENABLE_SUGGESTION.name, !isEnableSuggestion).apply()
    }

    val isEnableSearchHistory: Boolean
        get() = preferences.getBoolean(Key.ENABLE_SEARCH_HISTORY.name, true)

    fun switchEnableSearchHistory() {
        preferences.edit().putBoolean(Key.ENABLE_SEARCH_HISTORY.name, !isEnableSearchHistory)
                .apply()
    }

    val isEnableFavoriteSearch: Boolean
        get() = preferences.getBoolean(Key.ENABLE_FAVORITE_SEARCH.name, true)

    fun switchEnableFavoriteSearch() {
        preferences.edit().putBoolean(Key.ENABLE_FAVORITE_SEARCH.name, !isEnableFavoriteSearch)
                .apply()
    }

    val isEnableViewHistory: Boolean
        get() = preferences.getBoolean(Key.ENABLE_VIEW_HISTORY.name, true)

    fun switchEnableViewHistory() {
        preferences.edit().putBoolean(Key.ENABLE_VIEW_HISTORY.name, !isEnableViewHistory)
                .apply()
    }

    fun isEnableUrlModule(): Boolean {
        return preferences.getBoolean(Key.ENABLE_URL_MODULE.name, true)
    }

    fun switchEnableUrlModule() {
        preferences.edit().putBoolean(Key.ENABLE_URL_MODULE.name, !isEnableUrlModule())
                .apply()
    }

    fun isEnableTrendModule(): Boolean {
        return preferences.getBoolean(Key.ENABLE_TREND_MODULE.name, true)
    }

    fun switchEnableTrendModule() {
        preferences.edit().putBoolean(Key.ENABLE_TREND_MODULE.name, !isEnableTrendModule())
                .apply()
    }

    fun addDisableSearchCategory(name: String) {
        val set = readDisableSearchCategory()
        set?.add(name)
        preferences.edit().putStringSet(Key.DISABLE_SEARCH_CATEGORIES.name, set).apply()
    }

    fun removeDisableSearchCategory(name: String) {
        val set = readDisableSearchCategory()
        set?.remove(name)
        preferences.edit().putStringSet(Key.DISABLE_SEARCH_CATEGORIES.name, set).apply()
    }

    fun readDisableSearchCategory(): MutableSet<String>? {
        return preferences.getStringSet(Key.DISABLE_SEARCH_CATEGORIES.name, mutableSetOf())
    }

    fun switchShowDisplayEffect() {
        preferences.edit().putBoolean(Key.SHOW_DISPLAY_EFFECT.name, showDisplayEffect().not()).apply()
    }

    fun showDisplayEffect(): Boolean {
        return preferences.getBoolean(Key.SHOW_DISPLAY_EFFECT.name, false)
    }

    var backgroundImagePath: String
        get() = preferences.getString(Key.BG_IMAGE.name, "") ?: ""
        set(path) = preferences.edit().putString(Key.BG_IMAGE.name, path).apply()

    fun removeBackgroundImagePath() {
        preferences.edit().remove(Key.BG_IMAGE.name).apply()
    }

    fun isFirstLaunch(): Boolean {
        val firstLaunch = File(context.filesDir, "firstLaunch")
        if (firstLaunch.exists()) {
            return false
        }
        firstLaunch.mkdirs()
{'fraction_non_alphanumeric': 0.09375, 'fraction_numerical': 0.0002749266862170088, 'mean_word_length': 3.724242424242424, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1463027', 'n_tokens_mistral': 3568, 'n_tokens_neox': 3455, 'n_words': 512}