import 'package:flutter/foundation.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:test_sa/views/app_style/colors.dart';

class NotificationManger {
  // private constructor to avoid create object
  NotificationManger._();

  static FlutterLocalNotificationsPlugin localNotificationsPlugin = FlutterLocalNotificationsPlugin();

  /// initialisation setting for all platform
  /// onNotificationPressed action when notification pressed to open tap
  /// onIOSNotificationPressed action when notification pressed
  /// to open tap in iOS versions older than 10
  static initialisation(Function(NotificationResponse) onNotificationPressed, DidReceiveLocalNotificationCallback onIOSNotificationPressed) async {
    FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    // initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
    const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings(
      'app_icon',
    );

    final DarwinInitializationSettings initializationSettingsDarwin = DarwinInitializationSettings(onDidReceiveLocalNotification: onIOSNotificationPressed);

    final InitializationSettings initializationSettings = InitializationSettings(android: initializationSettingsAndroid, iOS: initializationSettingsDarwin, macOS: initializationSettingsDarwin);

    await flutterLocalNotificationsPlugin.initialize(initializationSettings, onDidReceiveNotificationResponse: onNotificationPressed);
  }

  // push new notification
  static Future showNotification({@required String title, @required String subtext, @required int hashcode, String payload}) async {
    const AndroidNotificationDetails androidChannel = AndroidNotificationDetails(
      'com.newtrack.testsa',
      'Test SA',
      channelDescription: 'Push notification service for Test SA',
      importance: Importance.max,
      priority: Priority.max,
      playSound: true,
      channelShowBadge: true,
      enableLights: true,
      visibility: NotificationVisibility.public,
      ledColor: AColors.secondaryColor,
      ledOnMs: 1,
      ledOffMs: 0,
      enableVibration: true,
      groupKey: 'com.newtrack.testsa',
    );

    const DarwinNotificationDetails iosNotificationDetails = DarwinNotificationDetails(
      categoryIdentifier: "testSA",
    );

    const platformChannel = NotificationDetails(
      android: androidChannel,
      iOS: iosNotificationDetails,
      macOS: iosNotificationDetails,
    );

    await localNotificationsPlugin.show(
      hashcode,
      title,
      subtext,
      platformChannel,
      payload: payload,
    );
  }
}
