package com.hikoncont.util

import android.os.Build
import android.util.Log

/**
 * 设备检测工具类
 * 用于检测设备品牌和型号，为不同设备提供定制化策略
 */
object DeviceDetector {
    private const val TAG = "DeviceDetector"
    
    /**
     * 检测是否为OPPO设备
     */
    fun isOppoDevice(): Boolean {
        return try {
            val brand = Build.BRAND.lowercase()
            val manufacturer = Build.MANUFACTURER.lowercase()
            val product = Build.PRODUCT.lowercase()
            val device = Build.DEVICE.lowercase()
            val model = Build.MODEL.lowercase()
            
            val isOppo = brand.contains("oppo") || 
                        manufacturer.contains("oppo") || 
                        product.contains("oppo") || 
                        device.contains("oppo") || 
                        model.contains("oppo")
            
            Log.d(TAG, "🔍 OPPO设备检测: brand=$brand, manufacturer=$manufacturer, isOppo=$isOppo")
            isOppo
        } catch (e: Exception) {
            Log.e(TAG, "❌ 检测OPPO设备失败", e)
            false
        }
    }
    
    /**
     * 检测设备品牌类型
     */
    fun getDeviceBrand(): String {
        return try {
            val brand = Build.BRAND.lowercase()
            val manufacturer = Build.MANUFACTURER.lowercase()
            
            Log.d(TAG, "🔍 设备品牌检测: brand=$brand, manufacturer=$manufacturer")
            
            when {
                brand.contains("oppo") || manufacturer.contains("oppo") -> "OPPO"
                brand.contains("vivo") || manufacturer.contains("vivo") -> "VIVO"
                brand.contains("xiaomi") || brand.contains("redmi") || manufacturer.contains("xiaomi") -> "XIAOMI"
                brand.contains("huawei") || brand.contains("honor") || manufacturer.contains("huawei") -> "HUAWEI"
                brand.contains("oneplus") || manufacturer.contains("oneplus") -> "ONEPLUS"
                brand.contains("samsung") || manufacturer.contains("samsung") -> "SAMSUNG"
                brand.contains("realme") || manufacturer.contains("realme") -> "REALME"
                else -> "UNKNOWN"
            }
        } catch (e: Exception) {
            Log.e(TAG, "❌ 检测设备品牌失败", e)
            "UNKNOWN"
        }
    }
    
    /**
     * 检测是否需要Activity保活
     * OPPO设备只使用服务保活，不使用Activity保活
     */
    fun shouldUseActivityKeepAlive(): Boolean {
        val brand = getDeviceBrand()
        val shouldUse = when (brand) {
            "OPPO" -> {
                Log.i(TAG, "📱 OPPO设备：禁用Activity保活，仅使用服务保活")
                false
            }
            "VIVO" -> {
                Log.i(TAG, "📱 VIVO设备：禁用Activity保活，仅使用服务保活")
                false
            }
            else -> {
                Log.i(TAG, "📱 ${brand}设备：允许Activity保活")
                true
            }
        }
        
        Log.d(TAG, "🔍 保活策略检测: 品牌=$brand, 使用Activity保活=$shouldUse")
        return shouldUse
    }
    
    /**
     * 检测设备详细信息
     */
    fun getDeviceInfo(): Map<String, String> {
        return try {
            mapOf(
                "brand" to Build.BRAND,
                "manufacturer" to Build.MANUFACTURER,
                "product" to Build.PRODUCT,
                "device" to Build.DEVICE,
                "model" to Build.MODEL,
                "sdk" to Build.VERSION.SDK_INT.toString(),
                "release" to Build.VERSION.RELEASE
            )
        } catch (e: Exception) {
            Log.e(TAG, "❌ 获取设备信息失败", e)
            emptyMap()
        }
    }
}
