package com.sshproxy.vpn.data

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

class PrefsRepository(context: Context) {
    private val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)

    fun load(): TunnelConfig = TunnelConfig(
        host = prefs.getString(KEY_HOST, "") ?: "",
        port = prefs.getInt(KEY_PORT, 22),
        username = prefs.getString(KEY_USERNAME, "") ?: "",
        authMethod = prefs.getString(KEY_AUTH_METHOD, SshAuthMethod.PUBLIC_KEY.name)
            ?.let { runCatching { SshAuthMethod.valueOf(it) }.getOrNull() }
            ?: SshAuthMethod.PUBLIC_KEY,
        privateKeyPath = prefs.getString(KEY_KEY_PATH, "") ?: "",
        keyPassphrase = prefs.getString(KEY_PASSPHRASE, "") ?: "",
        password = prefs.getString(KEY_PASSWORD, "") ?: "",
        socksPort = prefs.getInt(KEY_SOCKS_PORT, TunnelConfig.DEFAULT_LOCAL_SOCKS_PORT),
        remoteSocksHost = prefs.getString(KEY_REMOTE_SOCKS_HOST, TunnelConfig.DEFAULT_REMOTE_SOCKS_HOST)
            ?: TunnelConfig.DEFAULT_REMOTE_SOCKS_HOST,
        remoteSocksPort = prefs.getInt(KEY_REMOTE_SOCKS_PORT, TunnelConfig.DEFAULT_REMOTE_SOCKS_PORT),
        remoteSocksUsername = prefs.getString(KEY_REMOTE_SOCKS_USERNAME, "") ?: "",
        remoteSocksPassword = prefs.getString(KEY_REMOTE_SOCKS_PASSWORD, "") ?: "",
        acceptUnknownHostKey = prefs.getBoolean(KEY_ACCEPT_UNKNOWN, true),
        keepAliveIntervalSec = prefs.getInt(
            KEY_KEEP_ALIVE_INTERVAL,
            TunnelConfig.DEFAULT_KEEP_ALIVE_INTERVAL_SEC
        ),
        autoReconnect = prefs.getBoolean(KEY_AUTO_RECONNECT, true)
    )

    fun save(config: TunnelConfig) {
        prefs.edit {
            putString(KEY_HOST, config.host)
            putInt(KEY_PORT, config.port)
            putString(KEY_USERNAME, config.username)
            putString(KEY_AUTH_METHOD, config.authMethod.name)
            putString(KEY_KEY_PATH, config.privateKeyPath)
            putString(KEY_PASSPHRASE, config.keyPassphrase)
            putString(KEY_PASSWORD, config.password)
            putInt(KEY_SOCKS_PORT, config.socksPort)
            putString(KEY_REMOTE_SOCKS_HOST, config.remoteSocksHost)
            putInt(KEY_REMOTE_SOCKS_PORT, config.remoteSocksPort)
            putString(KEY_REMOTE_SOCKS_USERNAME, config.remoteSocksUsername)
            putString(KEY_REMOTE_SOCKS_PASSWORD, config.remoteSocksPassword)
            putBoolean(KEY_ACCEPT_UNKNOWN, config.acceptUnknownHostKey)
            putInt(KEY_KEEP_ALIVE_INTERVAL, config.keepAliveIntervalSec)
            putBoolean(KEY_AUTO_RECONNECT, config.autoReconnect)
        }
    }

    fun getExcludedPackages(): Set<String> {
        return prefs.getStringSet(KEY_EXCLUDED_PACKAGES, emptySet()) ?: emptySet()
    }

    fun setExcludedPackages(packages: Set<String>) {
        prefs.edit {
            putStringSet(KEY_EXCLUDED_PACKAGES, packages.toHashSet())
        }
    }

    fun isExcluded(packageName: String): Boolean {
        return getExcludedPackages().contains(packageName)
    }

    fun setExcluded(packageName: String, excluded: Boolean) {
        val updated = getExcludedPackages().toMutableSet()
        if (excluded) {
            updated.add(packageName)
        } else {
            updated.remove(packageName)
        }
        setExcludedPackages(updated)
    }

    companion object {
        private const val PREFS_NAME = "ssh_proxy_vpn"
        private const val KEY_HOST = "host"
        private const val KEY_PORT = "port"
        private const val KEY_USERNAME = "username"
        private const val KEY_AUTH_METHOD = "auth_method"
        private const val KEY_KEY_PATH = "key_path"
        private const val KEY_PASSPHRASE = "passphrase"
        private const val KEY_PASSWORD = "password"
        private const val KEY_SOCKS_PORT = "socks_port"
        private const val KEY_REMOTE_SOCKS_HOST = "remote_socks_host"
        private const val KEY_REMOTE_SOCKS_PORT = "remote_socks_port"
        private const val KEY_REMOTE_SOCKS_USERNAME = "remote_socks_username"
        private const val KEY_REMOTE_SOCKS_PASSWORD = "remote_socks_password"
        private const val KEY_ACCEPT_UNKNOWN = "accept_unknown"
        private const val KEY_KEEP_ALIVE_INTERVAL = "keep_alive_interval"
        private const val KEY_AUTO_RECONNECT = "auto_reconnect"
        private const val KEY_EXCLUDED_PACKAGES = "excluded_packages"
    }
}
