import 'dart:async';

import 'package:nexo_messaging/nexo_messaging.dart';

import 'nexo_notification_client.dart';

/// Production adapter wrapping the platform-channel singleton.
///
/// This is the ONLY file in production that should reference
/// `NexoMessagingPlugin.instance`. Everything else depends on
/// [NexoNotificationClient].
class NexoNotificationPluginClient implements NexoNotificationClient {
  final NexoMessagingPlugin _plugin;

  NexoNotificationPluginClient({NexoMessagingPlugin? plugin})
    : _plugin = plugin ?? NexoMessagingPlugin.instance;

  @override
  Stream<Map<String, dynamic>> get onNotification => _plugin.onNotification;

  @override
  Future<void> initialize() => _plugin.initialize();

  @override
  Future<String?> getDeviceToken() => _plugin.getDeviceToken();

  @override
  Future<void> setAuthToken(
    String serverUrl,
    String token, {
    String? identifier,
  }) => _plugin.setAuthToken(serverUrl, token, identifier: identifier);

  @override
  Future<void> setSigningKey(String serverUrl, String signingKey) =>
      _plugin.setSigningKey(serverUrl, signingKey);

  @override
  set onDeviceTokenReady(Future<void> Function(String token)? callback) {
    _plugin.onDeviceTokenReady = callback == null
        ? null
        : (token) => unawaited(callback(token));
  }

  @override
  set onNavigateToChannel(
    void Function(String serverUrl, String channelId)? callback,
  ) {
    _plugin.onNavigateToChannel = callback;
  }

  @override
  set onNavigateToThread(
    void Function(String serverUrl, String rootId)? callback,
  ) {
    _plugin.onNavigateToThread = callback;
  }
}
