/*
 * Hermes Node Android
 * Copyright (C) 2026 Stefy Lanza <stefy@nexlab.net>
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package net.nexlab.hermesnodeandroid

import android.content.Context
import android.os.Build

object ConfigStore {
    private const val PREFS = "hermes_node_android"

    fun load(context: Context): NodeConfig {
        val prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
        val defaultName = "android-${Build.MODEL.replace(Regex("\\s+"), "-").lowercase()}"
        return NodeConfig(
            gatewayUrl = prefs.getString("gateway_url", "wss://lisa.nexlab.net:8765") ?: "wss://lisa.nexlab.net:8765",
            token = prefs.getString("token", "") ?: "",
            nodeName = prefs.getString("node_name", defaultName) ?: defaultName,
            enableExec = prefs.getBoolean("enable_exec", false),
            enableComputerControl = prefs.getBoolean("enable_computer_control", true),
            enableDesktopObserve = prefs.getBoolean("enable_desktop_observe", true),
            enableAudioControl = prefs.getBoolean("enable_audio_control", true),
            enableCameraControl = prefs.getBoolean("enable_camera_control", true),
            enableBrowserControl = prefs.getBoolean("enable_browser_control", false),
            enableNotificationAccess = prefs.getBoolean("enable_notification_access", false),
            insecureTls = prefs.getBoolean("insecure_tls", true),
            reconnectIntervalSeconds = prefs.getLong("reconnect_interval_seconds", 5L),
            heartbeatIntervalSeconds = prefs.getLong("heartbeat_interval_seconds", 30L),
        )
    }

    fun save(context: Context, config: NodeConfig) {
        context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit()
            .putString("gateway_url", config.gatewayUrl.trim())
            .putString("token", config.token.trim())
            .putString("node_name", config.nodeName.trim())
            .putBoolean("enable_exec", config.enableExec)
            .putBoolean("enable_computer_control", config.enableComputerControl)
            .putBoolean("enable_desktop_observe", config.enableDesktopObserve)
            .putBoolean("enable_audio_control", config.enableAudioControl)
            .putBoolean("enable_camera_control", config.enableCameraControl)
            .putBoolean("enable_browser_control", config.enableBrowserControl)
            .putBoolean("enable_notification_access", config.enableNotificationAccess)
            .putBoolean("insecure_tls", config.insecureTls)
            .putLong("reconnect_interval_seconds", config.reconnectIntervalSeconds)
            .putLong("heartbeat_interval_seconds", config.heartbeatIntervalSeconds)
            .apply()
    }
}
