package partisan.weforge.xyz.pulse

import android.content.Context
import androidx.core.content.edit
import androidx.preference.PreferenceManager

class Preferences(private val context: Context) {

    companion object {
        private const val REDIRECTION_DELAY = "redirection_delay"
        private const val POPUP_POSITION = "popup_position_y"
        private const val POPUP_ENABLED = "popup_enabled"
        private const val BLACKLISTED_CONTACTS = "blacklisted_contacts"
        private val SERVICE_ORDER_KEY = "service_order"

        private const val DEFAULT_REDIRECTION_DELAY = 2000L
        private const val DEFAULT_POPUP_POSITION = 333
        private const val SERVICE_ENABLED = "service_enabled"
    }

    private val prefs = PreferenceManager.getDefaultSharedPreferences(context)

    // Whether user enabled/disabled the service manually by tiggle button
    var isServiceEnabledByUser: Boolean
        get() = prefs.getBoolean(SERVICE_ENABLED, false)
        set(value) = prefs.edit { putBoolean(SERVICE_ENABLED, value) }

    // True only if all required permissions + toggle are satisfied
    val isEnabled: Boolean
        get() = isServiceEnabledByUser &&
                hasGeneralPermissions(context) &&
                hasDrawOverlays(context) &&
                hasCallRedirectionRole(context)

    var popupEnabled: Boolean
        get() = prefs.getBoolean(POPUP_ENABLED, true)
        set(value) = prefs.edit { putBoolean(POPUP_ENABLED, value) }

    var redirectionDelay: Long
        get() = prefs.getLong(REDIRECTION_DELAY, DEFAULT_REDIRECTION_DELAY)
        set(value) = prefs.edit { putLong(REDIRECTION_DELAY, value) }

    var popupPosition: Int
        get() = prefs.getInt(POPUP_POSITION, DEFAULT_POPUP_POSITION)
        set(value) = prefs.edit { putInt(POPUP_POSITION, value) }

    private fun makeKeyEnabled(mimetype: String) = "enabled_$mimetype"
    private fun makeKeyPriority(mimetype: String) = "priority_$mimetype"

    fun isServiceEnabled(mimetype: String): Boolean {
        return prefs.getBoolean(makeKeyEnabled(mimetype), true)
    }

    fun getServicePriority(mimetype: String): Int {
        val order = getServiceOrder()
        val index = order.indexOf(mimetype)
        return if (index != -1) index else Int.MAX_VALUE
    }

    fun getServiceOrder(): List<String> {
        val stored = prefs.getString(SERVICE_ORDER_KEY, null)
        return stored?.split("|")?.filter { it.isNotBlank() } ?: emptyList()
    }

    fun setServiceOrder(order: List<String>) {
        prefs.edit().putString(SERVICE_ORDER_KEY, order.joinToString("|")).apply()
    }

    fun setServiceEnabled(mimetype: String, enabled: Boolean) {
        prefs.edit().putBoolean(makeKeyEnabled(mimetype), enabled).apply()
    }

    fun setServicePriority(mimetype: String, priority: Int) {
        prefs.edit().putInt(makeKeyPriority(mimetype), priority).apply()
    }

    var blacklistedContacts: Set<String>
        get() = prefs.getStringSet(BLACKLISTED_CONTACTS, emptySet()) ?: emptySet()
        set(value) = prefs.edit { putStringSet(BLACKLISTED_CONTACTS, value) }

    fun isContactWhitelisted(phoneNumber: String): Boolean {
        return !blacklistedContacts.contains(phoneNumber)
    }

    fun setContactWhitelisted(phoneNumber: String, allowed: Boolean) {
        val current = blacklistedContacts.toMutableSet()
        if (!allowed) {
            current.add(phoneNumber)
        } else {
            current.remove(phoneNumber)
        }
        blacklistedContacts = current
    }
}
