// ignore_for_file: empty_catches

import 'dart:convert';
import 'dart:developer';

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

Future<void> firebaseMessageBackgroundHandle(RemoteMessage message) async {
  NotificationService().display(message);
  log("BackGround Message :: ${message.messageId}");
}

class NotificationService {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

  Future<void> initInfo() async {
    await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
      alert: true,
      badge: true,
      sound: true,
    );
    var request = await FirebaseMessaging.instance.requestPermission(
      alert: true,
      announcement: false,
      badge: true,
      carPlay: false,
      criticalAlert: false,
      provisional: false,
      sound: true,
    );

    if (request.authorizationStatus == AuthorizationStatus.authorized || request.authorizationStatus == AuthorizationStatus.provisional) {
      const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');
      var iosInitializationSettings = const DarwinInitializationSettings();
      final InitializationSettings initializationSettings = InitializationSettings(android: initializationSettingsAndroid, iOS: iosInitializationSettings);
      await flutterLocalNotificationsPlugin.initialize(initializationSettings, onDidReceiveNotificationResponse: (payload) {});
      const AndroidNotificationChannel orderChannel = AndroidNotificationChannel(
        'booking_channel',
        'Bookings',
        description: 'Channel for new booking notifications',
        importance: Importance.high,
        playSound: true,
        sound: RawResourceAndroidNotificationSound('booking_notification'),
      );

      const AndroidNotificationChannel defaultChannel = AndroidNotificationChannel(
        'default_channel',
        'General',
        description: 'General notifications',
        importance: Importance.high,
        playSound: true,
      );

      final androidPlugin = flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>();

      await androidPlugin?.createNotificationChannel(orderChannel);
      await androidPlugin?.createNotificationChannel(defaultChannel);

      // Background handler
      FirebaseMessaging.onBackgroundMessage(firebaseMessageBackgroundHandle);

      setupInteractedMessage();
    }

    await FirebaseMessaging.instance.subscribeToTopic("mytaxi-driver");
  }

  Future<void> setupInteractedMessage() async {
    RemoteMessage? initialMessage = await FirebaseMessaging.instance.getInitialMessage();
    if (initialMessage != null) {
      // FirebaseMessaging.onBackgroundMessage((message) => firebaseMessageBackgroundHandle(message));
      log("Initial Message: ${initialMessage.messageId}");
    }

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      log("::::::::::::onMessage:::::::::::::::::");
      if (message.notification != null) {
        log(message.notification.toString());
        display(message);
      }
    });
    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
      log("::::::::::::onMessageOpenedApp:::::::::::::::::");
      if (message.notification != null) {
        log(message.notification.toString());
        // if (message.data['type'] == "chat") {
        //   await FireStoreUtils.getDriverUserProfile(message.data['senderId'] == FireStoreUtils.getCurrentUid() ? message.data['receiverId'] : message.data['senderId']).then((value) {
        //     // UserModel userModel = value!;
        //     // Get.to(const ChatScreen(), arguments: {"receiverModel": userModel});
        //   });
        // } else if (message.data['type'] == "order") {
        //   // OrderModel? orderModel = await FireStoreUtils.getSingleOrder(message.data['orderId']);
        //   // Get.to(() => const MySummaryScreen(), arguments: {"orderModel": orderModel});
        // }
      }
    });
    log("::::::::::::Permission authorized:::::::::::::::::");
  }

  static Future<String> getToken() async {
    String? token;
    try {
      token = await FirebaseMessaging.instance.getToken();
    } catch (e) {}
    return token ?? '';
  }

  void display(RemoteMessage message) async {
    log('Got a message whilst in the foreground!');
    log('Message data: ${message.notification!.body.toString()}');
    try {
      bool isBooking = message.data['isBooking'] == 'true';
      AndroidNotificationDetails androidDetails = AndroidNotificationDetails(
        isBooking ? 'booking_channel' : 'default_channel',
        isBooking ? 'Bookings' : 'General',
        channelDescription: 'Booking Notifications',
        importance: Importance.max,
        priority: Priority.high,
        playSound: true,
        sound: isBooking ? const RawResourceAndroidNotificationSound('booking_notification') : null,
      );

      DarwinNotificationDetails iosDetails = DarwinNotificationDetails(
        presentSound: true,
        sound: isBooking ? 'booking_notification.wav' : null,
      );

      NotificationDetails notificationDetailsBoth = NotificationDetails(android: androidDetails, iOS: iosDetails);
      await flutterLocalNotificationsPlugin.show(
        0,
        message.notification!.title,
        message.notification!.body,
        notificationDetailsBoth,
        payload: jsonEncode(message.data),
      );
    } on Exception catch (e) {
      log(e.toString());
    }
  }
}
