import { NativeModules, NativeEventEmitter, EmitterSubscription } from 'react-native';
import { SmsMessage, NotificationMessage } from './types';

const { SmsNotificationModule } = NativeModules;
const eventEmitter = new NativeEventEmitter(SmsNotificationModule);

/**
 * 检查 SMS 权限
 */
export async function checkSmsPermission(): Promise<boolean> {
    if (!SmsNotificationModule) {
        throw new Error('SmsNotificationModule not available');
    }
    return await SmsNotificationModule.checkSmsPermission();
}

/**
 * 请求 SMS 权限
 */
export async function requestSmsPermission(): Promise<boolean> {
    if (!SmsNotificationModule) {
        throw new Error('SmsNotificationModule not available');
    }
    return await SmsNotificationModule.requestSmsPermission();
}

/**
 * 检查通知监听权限
 */
export async function checkNotificationPermission(): Promise<boolean> {
    if (!SmsNotificationModule) {
        throw new Error('SmsNotificationModule not available');
    }
    return await SmsNotificationModule.checkNotificationPermission();
}

/**
 * 打开通知监听设置页面
 */
export async function openNotificationSettings(): Promise<void> {
    if (!SmsNotificationModule) {
        throw new Error('SmsNotificationModule not available');
    }
    await SmsNotificationModule.openNotificationSettings();
}

/**
 * 启动 SMS 监听
 */
export async function startSmsListener(): Promise<void> {
    if (!SmsNotificationModule) {
        throw new Error('SmsNotificationModule not available');
    }
    await SmsNotificationModule.startSmsListener();
}

/**
 * 停止 SMS 监听
 */
export async function stopSmsListener(): Promise<void> {
    if (!SmsNotificationModule) {
        throw new Error('SmsNotificationModule not available');
    }
    await SmsNotificationModule.stopSmsListener();
}

/**
 * 启动通知监听
 */
export async function startNotificationListener(): Promise<void> {
    if (!SmsNotificationModule) {
        throw new Error('SmsNotificationModule not available');
    }
    await SmsNotificationModule.startNotificationListener();
}

/**
 * 停止通知监听
 */
export async function stopNotificationListener(): Promise<void> {
    if (!SmsNotificationModule) {
        throw new Error('SmsNotificationModule not available');
    }
    await SmsNotificationModule.stopNotificationListener();
}

/**
 * 监听短信消息
 * @param callback 回调函数
 * @returns 取消订阅的函数
 */
export function onSmsMessage(callback: (message: SmsMessage) => void): EmitterSubscription {
    return eventEmitter.addListener('onSmsMessage', callback);
}

/**
 * 监听通知消息
 * @param callback 回调函数
 * @returns 取消订阅的函数
 */
export function onNotificationMessage(callback: (notification: NotificationMessage) => void): EmitterSubscription {
    return eventEmitter.addListener('onNotificationMessage', callback);
}

/**
 * 读取所有短信（历史记录）
 * @param limit 限制数量，0 表示全部
 */
export async function getAllSms(limit: number = 100): Promise<SmsMessage[]> {
    if (!SmsNotificationModule) {
        throw new Error('SmsNotificationModule not available');
    }
    return await SmsNotificationModule.getAllSms(limit);
}

/**
 * 读取所有活跃通知
 */
export async function getAllNotifications(): Promise<NotificationMessage[]> {
    if (!SmsNotificationModule) {
        throw new Error('SmsNotificationModule not available');
    }
    return await SmsNotificationModule.getAllNotifications();
}
