import 'dart:async';

import 'package:isolate_manager/isolate_manager.dart';

import '../generate_transactions_task.dart';
import '../show_notifications_task.dart';
import 'background_init_web.dart' if (dart.library.io) 'background_init.dart';

@pragma('vm:entry-point')
@isolateManagerWorker
/// Führt eine Hintergrundtask lokal aus
Future<void> runTask(final Map<String, dynamic> params) async {
  final String taskName = params['taskName'];
  final int initialDelayMinutes = params['initialDelayMinutes'];
  final int frequencyMinutes = params['frequencyMinutes'];

  await Future.delayed(Duration(minutes: initialDelayMinutes));

  await executeTask(taskName, null);

  await Future.delayed(Duration(minutes: frequencyMinutes));

  unawaited(runTask(params));
}

/// Funktion um Hintergrundaufgaben auszuführen
Future<bool> executeTask(
  final String taskName,
  final Map<String, dynamic>? inputData,
) async {
  await initBackground();

  switch (taskName) {
    case 'generate_transactions':
      return GenerateTransactionsTask().execute();
    case 'show_notifications':
      return ShowNotificationsTask().execute();
  }

  return Future.value(true);
}
