package com.example.godeye.utils

import android.content.Context

/**
 * Constants - константы приложения согласно ТЗ
 */
object Constants {

    // Настройки сервера согласно ТЗ
    const val DEFAULT_SERVER_URL = "http://192.168.219.108:3001"
    const val LOCALHOST_SERVER_URL = "http://localhost:3001"
    const val LOCAL_NETWORK_SERVER_URL = "http://192.168.219.108:3001"
    const val WEBSOCKET_SERVER_URL = "ws://192.168.219.108:3000"
    const val SIGNALING_SERVER_URL = "ws://192.168.219.108:8765"

    // Настройки Socket.IO
    const val SOCKET_TIMEOUT = 10000L
    const val SOCKET_RECONNECTION_ATTEMPTS = 5
    const val SOCKET_RECONNECTION_DELAY = 1000L

    // Настройки WebRTC согласно ТЗ
    const val WEBRTC_VIDEO_WIDTH = 1920
    const val WEBRTC_VIDEO_HEIGHT = 1080
    const val WEBRTC_VIDEO_FPS = 30

    // STUN серверы согласно ТЗ
    val STUN_SERVERS = listOf(
        "stun:stun.l.google.com:19302",
        "stun:stun1.l.google.com:19302"
    )

    // Типы камер согласно ТЗ
    object CameraTypes {
        const val BACK = "back"
        const val FRONT = "front"
        const val ULTRA_WIDE = "ultra_wide"
        const val TELEPHOTO = "telephoto"

        val ALL_TYPES = listOf(BACK, FRONT, ULTRA_WIDE, TELEPHOTO)
    }

    // События Socket.IO согласно ТЗ
    object SocketEvents {
        // Исходящие события
        const val REGISTER_ANDROID = "register:android"
        const val CAMERA_RESPONSE = "camera:response"
        const val WEBRTC_OFFER = "webrtc:offer"
        const val WEBRTC_ANSWER = "webrtc:answer"
        const val WEBRTC_ICE_CANDIDATE = "webrtc:ice-candidate"

        // Входящие события
        const val REGISTER_SUCCESS = "register:success"
        const val CAMERA_REQUEST = "camera:request"
        const val CAMERA_DISCONNECT = "camera:disconnect"
        const val CAMERA_SWITCH = "camera:switch"
        const val WEBRTC_OFFER_RECEIVED = "webrtc:offer"
        const val WEBRTC_ANSWER_RECEIVED = "webrtc:answer"
        const val WEBRTC_ICE_RECEIVED = "webrtc:ice-candidate"

        const val CONNECT = "connect"
        const val DISCONNECT = "disconnect"
        const val DEVICE_REGISTER = "device-register"
        const val KEEP_ALIVE = "keep-alive"
    }

    // SharedPreferences ключи согласно ТЗ
    object PreferenceKeys {
        const val SERVER_URL = "server_url"
        const val DEVICE_ID = "device_id"
        const val AUTO_ACCEPT_REQUESTS = "auto_accept_requests"
        const val CAMERA_QUALITY = "camera_quality"
        const val NOTIFICATION_ENABLED = "notification_enabled"
        const val REMEMBER_OPERATORS = "remember_operators"
    }

    // Настройки уведомлений
    const val NOTIFICATION_CHANNEL_ID = "godeye_service_channel"
    const val SERVICE_NOTIFICATION_ID = 1001
    const val CAMERA_REQUEST_NOTIFICATION_ID = 1002

    // Таймауты
    const val CAMERA_OPEN_TIMEOUT = 2500L
    const val WEBRTC_CONNECTION_TIMEOUT = 10000L
    const val SOCKET_CONNECTION_TIMEOUT = 5000L

    // Версии API
    const val MIN_SDK_VERSION = 24 // Android 7.0+ согласно ТЗ
    const val TARGET_SDK_VERSION = 34

    // Качество видео
    object VideoQuality {
        const val HD_WIDTH = 1280
        const val HD_HEIGHT = 720
        const val FULL_HD_WIDTH = 1920
        const val FULL_HD_HEIGHT = 1080
        const val FPS_30 = 30
        const val FPS_60 = 60
    }

    // НОВОЕ: Добавляем автоматическое определение IP сервера
    fun getServerUrlForCurrentNetwork(context: Context): String {
        val localIP = context.getLocalIpAddress()
        // Проверяем, находимся ли мы в подсети 192.168.219.x
        return if (localIP.startsWith("192.168.219.")) {
            "http://192.168.219.108:3001" // Ваш сервер
        } else {
            // Fallback для других сетей
            val serverIP = localIP.substringBeforeLast(".") + ".108"
            "http://$serverIP:3001"
        }
    }

    fun getWebSocketUrlForCurrentNetwork(context: Context): String {
        val localIP = context.getLocalIpAddress()
        return if (localIP.startsWith("192.168.219.")) {
            "ws://192.168.219.108:3000" // Ваш сервер
        } else {
            val serverIP = localIP.substringBeforeLast(".") + ".108"
            "ws://$serverIP:3000"
        }
    }

    fun getSignalingUrlForCurrentNetwork(context: Context): String {
        val localIP = context.getLocalIpAddress()
        return if (localIP.startsWith("192.168.219.")) {
            "ws://192.168.219.108:8765" // Ваш сервер
        } else {
            val serverIP = localIP.substringBeforeLast(".") + ".108"
            "ws://$serverIP:8765"
        }
    }
}
