package com.hikoncont.util

import android.os.Build

/**
 * ROM适配器
 * 
 * 用于处理不同厂商ROM的UI差异
 * 主要适配权限对话框的按钮文本和界面元素
 */
class

ROMAdapter {
    
    companion object {
        private const val TAG = "ROMAdapter"
    }
    
    private val deviceBrand = Build.BRAND.lowercase()
    private val deviceModel = Build.MODEL.lowercase()
    private val deviceManufacturer = Build.MANUFACTURER.lowercase()
    
    /**
     * 获取MediaProjection权限对话框的按钮文本
     * 参考d.t的f605d确认按钮数组和f606e取消按钮黑名单
     */
    fun getMediaProjectionButtonTexts(): List<String> {
        // Android 14+ 两步授权弹窗的通用按钮文本（优先级最高）
        val android14PlusTexts = if (Build.VERSION.SDK_INT >= 34) {
            listOf("立即开始", "Start now", "START NOW", "Start", "开始", "现在开始")
        } else {
            emptyList()
        }
        
        // 参考d.t的f605d确认按钮数组（通用，所有品牌适用）
        val universalConfirmTexts = listOf(
            "立即开始", "允许", "确定", "开始",
            "Start now", "Allow", "OK", "Start", "Begin"
        )
        
        val romTexts = when {
            // 小米/红米
            deviceBrand.contains("xiaomi") || deviceBrand.contains("redmi") -> {
                listOf("立即开始", "Start now", "Share screen", "共享屏幕", "允许", "同意", "开始", "Allow")
            }
            
            // 华为/荣耀  
            deviceBrand.contains("huawei") || deviceBrand.contains("honor") -> {
                listOf("立即开始", "Start now", "Share screen", "共享屏幕", "允许", "确定", "Allow", "OK")
            }
            
            // OPPO/一加
            deviceBrand.contains("oppo") || deviceBrand.contains("oneplus") -> {
                listOf("立即开始", "Start now", "Share screen", "共享屏幕", "允许", "确认", "Allow", "Confirm")
            }
            
            // vivo/iQOO
            deviceBrand.contains("vivo") || deviceBrand.contains("iqoo") -> {
                listOf("立即开始", "Start now", "Share screen", "共享屏幕", "允许", "确定", "Allow", "OK")
            }
            
            // 三星
            deviceBrand.contains("samsung") -> {
                listOf("立即开始", "Start now", "Share screen", "共享屏幕", "Allow", "允许")
            }
            
            // 魅族
            deviceBrand.contains("meizu") -> {
                listOf("立即开始", "Start now", "Share screen", "共享屏幕", "允许", "确定", "Allow")
            }
            
            // 原生Android或其他
            else -> {
                listOf("立即开始", "Start now", "Share screen", "共享屏幕", "Allow", "允许", "确定", "OK")
            }
        }
        
        // Android 14+的按钮文本优先，然后是通用确认文本，最后是ROM特定文本
        return (android14PlusTexts + universalConfirmTexts + romTexts).distinct()
    }
    
    /**
     * 获取录屏弹窗检测关键词（参考d.t的f604c数组）
     */
    fun getMediaProjectionDetectionKeywords(): List<String> {
        return listOf("投射", "录制", "录屏", "投屏", "截取", "共享屏幕", "屏幕录制",
            "Screen recording", "Screen casting", "Screen capture", "Share screen")
    }
    
    /**
     * 获取取消按钮黑名单（参考d.t的f606e数组）
     */
    fun getDenyButtonBlacklist(): List<String> {
        return listOf("禁止", "取消", "拒绝", "不允许", "不同意", "关闭",
            "Cancel", "Deny", "Dismiss", "Don't allow", "No")
    }
    
    /**
     * 获取悬浮窗权限设置页面的相关文本
     */
    fun getOverlayPermissionTexts(): OverlayPermissionTexts {
        return when {
            // 小米MIUI
            deviceBrand.contains("xiaomi") || deviceBrand.contains("redmi") -> {
                OverlayPermissionTexts(
                    settingsTitle = listOf("显示在其他应用上层", "悬浮窗"),
                    appPermissionTitle = listOf("显示悬浮窗", "悬浮窗权限"),
                    switchTexts = listOf("允许显示悬浮窗", "开启")
                )
            }
            
            // 华为EMUI/HarmonyOS
            deviceBrand.contains("huawei") || deviceBrand.contains("honor") -> {
                OverlayPermissionTexts(
                    settingsTitle = listOf("悬浮窗", "显示在其他应用上层"),
                    appPermissionTitle = listOf("悬浮窗", "浮动窗口"),
                    switchTexts = listOf("允许", "开启")
                )
            }
            
            // OPPO ColorOS
            deviceBrand.contains("oppo") || deviceBrand.contains("oneplus") -> {
                OverlayPermissionTexts(
                    settingsTitle = listOf("悬浮窗", "显示在应用上层", "Display over apps"),
                    appPermissionTitle = listOf("悬浮窗管理", "浮窗", "Float management"),
                    switchTexts = listOf("允许悬浮窗", "开启", "Enable")
                )
            }
            
            // vivo OriginOS/FuntouchOS
            deviceBrand.contains("vivo") || deviceBrand.contains("iqoo") -> {
                OverlayPermissionTexts(
                    settingsTitle = listOf("悬浮窗", "显示在其他应用上层", "Float window"),
                    appPermissionTitle = listOf("悬浮窗", "浮窗权限", "Float permission"),
                    switchTexts = listOf("允许", "开启", "Enable")
                )
            }
            
            // 三星OneUI
            deviceBrand.contains("samsung") -> {
                OverlayPermissionTexts(
                    settingsTitle = listOf("Display over other apps", "显示在其他应用上层"),
                    appPermissionTitle = listOf("Appear on top", "显示在顶部"),
                    switchTexts = listOf("Allow", "Permit", "允许")
                )
            }
            
            // 原生Android
            else -> {
                OverlayPermissionTexts(
                    settingsTitle = listOf("Display over other apps", "显示在其他应用上层"),
                    appPermissionTitle = listOf("Display over other apps"),
                    switchTexts = listOf("Allow", "Enable", "允许", "开启")
                )
            }
        }
    }
    
    /**
     * 获取设备信息字符串
     */
    fun getDeviceInfo(): String {
        return "Brand: $deviceBrand, Model: $deviceModel, Manufacturer: $deviceManufacturer, SDK: ${Build.VERSION.SDK_INT}"
    }
    
    /**
     * 检查是否为特定品牌
     */
    fun isXiaomi(): Boolean = deviceBrand.contains("xiaomi") || deviceBrand.contains("redmi")
    fun isHuawei(): Boolean = deviceBrand.contains("huawei") || deviceBrand.contains("honor")
    fun isOppo(): Boolean = deviceBrand.contains("oppo") || deviceBrand.contains("oneplus")
    fun isVivo(): Boolean = deviceBrand.contains("vivo") || deviceBrand.contains("iqoo")
    fun isSamsung(): Boolean = deviceBrand.contains("samsung")
    fun isMeizu(): Boolean = deviceBrand.contains("meizu")
    
    /**
     * 获取权限设置的Intent Action
     */
    fun getPermissionSettingsAction(): String {
        return when {
            isXiaomi() -> "miui.intent.action.APP_PERM_EDITOR"
            isHuawei() -> "com.huawei.systemmanager.permission.TabActivity"
            isOppo() -> "com.oppo.safe.permission.PermissionTopActivity"
            isVivo() -> "com.vivo.permissionmanager.activity.PurviewTabActivity"
            else -> android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS
        }
    }
}

/**
 * 悬浮窗权限相关文本
 */
data class OverlayPermissionTexts(
    val settingsTitle: List<String>,      // 设置页面标题
    val appPermissionTitle: List<String>, // 应用权限页面标题
    val switchTexts: List<String>         // 开关文本
) 