package com.jishisongfu.duobibi.util

import android.content.Context

object SettingsPrefs {

    private const val PREFS = "app_settings"
    private const val KEY_FLOATING = "floating_enabled"
    private const val KEY_FLOAT_X = "floating_x"
    private const val KEY_FLOAT_Y = "floating_y"

    fun floatingEnabled(ctx: Context): Boolean =
        ctx.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getBoolean(KEY_FLOATING, true)

    fun setFloatingEnabled(ctx: Context, value: Boolean) {
        ctx.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
            .edit()
            .putBoolean(KEY_FLOATING, value)
            .apply()
    }

    fun savedPosition(ctx: Context): Pair<Int, Int>? {
        val prefs = ctx.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
        if (!prefs.contains(KEY_FLOAT_X) || !prefs.contains(KEY_FLOAT_Y)) return null
        return prefs.getInt(KEY_FLOAT_X, 0) to prefs.getInt(KEY_FLOAT_Y, 0)
    }

    fun savePosition(ctx: Context, x: Int, y: Int) {
        ctx.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
            .edit()
            .putInt(KEY_FLOAT_X, x)
            .putInt(KEY_FLOAT_Y, y)
            .apply()
    }
}
