package com.omixlab.lckcontrol.p2p.discovery

import android.content.Context
import android.content.SharedPreferences
import dagger.hilt.android.qualifiers.ApplicationContext
import java.util.UUID
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class P2pPreferences @Inject constructor(
    @ApplicationContext context: Context,
) {
    private val prefs: SharedPreferences =
        context.getSharedPreferences("lck_p2p_prefs", Context.MODE_PRIVATE)

    var lanDiscoveryEnabled: Boolean
        get() = prefs.getBoolean(KEY_LAN_DISCOVERY, true)
        set(value) = prefs.edit().putBoolean(KEY_LAN_DISCOVERY, value).apply()

    val stableDeviceId: String
        get() {
            val existing = prefs.getString(KEY_DEVICE_ID, null)
            if (existing != null) return existing
            val id = UUID.randomUUID().toString()
            prefs.edit().putString(KEY_DEVICE_ID, id).apply()
            return id
        }

    companion object {
        private const val KEY_LAN_DISCOVERY = "lan_discovery_enabled"
        private const val KEY_DEVICE_ID = "stable_device_id"
    }
}
