import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:frontend_eccp_mobile/app/core/notification/fcm_config.dart';

typedef NotificationTapCallback = void Function(String? payload);

class LocalNotificationService {
  LocalNotificationService._();

  static final FlutterLocalNotificationsPlugin _plugin =
      FlutterLocalNotificationsPlugin();

  static Future<void> init({
    NotificationTapCallback? onSelectNotification,
  }) async {
    const android = AndroidInitializationSettings('@mipmap/ic_launcher');
    const ios = DarwinInitializationSettings();

    const settings = InitializationSettings(
      android: android,
      iOS: ios,
    );

    await _plugin.initialize(
      settings: settings,
      onDidReceiveNotificationResponse: (response) {
        onSelectNotification?.call(response.payload);
      },
    );
  }

  static Future<void> show({
    required String title,
    required String body,
    Map<String, dynamic>? payload,
  }) async {
    const androidDetails = AndroidNotificationDetails(
      FcmConfig.defaultChannelId,
      FcmConfig.defaultChannelName,
      channelDescription: FcmConfig.defaultChannelDesc,
      importance: Importance.high,
      priority: Priority.high,
    );

    const details = NotificationDetails(android: androidDetails);

    await _plugin.show(
      id: DateTime.now().millisecondsSinceEpoch ~/ 1000,
      title: title,
      body: body,
      notificationDetails: details,
      payload: payload?.toString(),
    );
  }
}
