package com.rnwalletman;

import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

/**
 * 通知监听服务
 */
public class RNWalletNotificationListener extends NotificationListenerService {
    private static final String TAG = "RNWalletNotification";
    private static RNWalletNotificationListener instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        Log.i(TAG, "RNWalletNotificationListener 服务已启动");
        
        // 检查并打印当前通知数量
        try {
            StatusBarNotification[] notifications = getActiveNotifications();
            Log.i(TAG, "服务启动时当前有 " + notifications.length + " 条活跃通知");
        } catch (Exception e) {
            Log.e(TAG, "无法获取活跃通知: " + e.getMessage(), e);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        instance = null;
        Log.i(TAG, "RNWalletNotificationListener stopped");
    }

    /**
     * 获取服务实例
     */
    public static RNWalletNotificationListener getInstance() {
        return instance;
    }

    /**
     * 读取当前所有活跃的通知
     */
    public StatusBarNotification[] readActiveNotifications() {
        try {
            StatusBarNotification[] notifications = super.getActiveNotifications();
            Log.i(TAG, "读取到 " + notifications.length + " 条活跃通知");
            for (StatusBarNotification sbn : notifications) {
                Log.d(TAG, "通知: " + sbn.getPackageName() + " - " + sbn.getId());
            }
            return notifications;
        } catch (Exception e) {
            Log.e(TAG, "读取通知失败: " + e.getMessage(), e);
            return new StatusBarNotification[0];
        }
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        // 检查是否启用监听
        android.content.SharedPreferences prefs = getSharedPreferences("rnwalletman", MODE_PRIVATE);
        boolean isEnabled = prefs.getBoolean("notification_enabled", false);
        
        if (!isEnabled) {
            return;
        }
        
        try {
            String packageName = sbn.getPackageName();
            String title = "";
            String text = "";
            String bigText = "";
            
            if (sbn.getNotification() != null && sbn.getNotification().extras != null) {
                android.os.Bundle extras = sbn.getNotification().extras;
                title = getStringFromExtras(extras, "android.title");
                text = getStringFromExtras(extras, "android.text");
                bigText = getStringFromExtras(extras, "android.bigText");
            }
            
            Log.i(TAG, "收到通知: " + packageName + " - " + title);
            
            // 发送事件到 React Native
            SmsNotificationModule.sendNotificationEvent(
                String.valueOf(sbn.getId()),
                packageName,
                sbn.getTag(),
                sbn.getPostTime(),
                title,
                text,
                bigText
            );
        } catch (Exception e) {
            Log.e(TAG, "处理通知失败", e);
        }
    }
    
    private String getStringFromExtras(android.os.Bundle extras, String key) {
        if (extras == null || key == null) {
            return "";
        }
        Object obj = extras.get(key);
        return obj != null ? obj.toString() : "";
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        // 不需要处理
    }
}
