import 'dart:async';
import 'dart:io';
import 'dart:ui';

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:injectable/injectable.dart';
import 'package:krow/core/application/di/injectable.dart';
import 'package:krow/core/sevices/background_service/task_registry.dart';
import 'package:krow/features/shifts/domain/services/clockout_checker_bg_task.dart';
import 'package:workmanager/workmanager.dart';

@Singleton()
class BackgroundService {
  Future<void> initializeService() async {
    // FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
    //
    // if (Platform.isIOS) {
    //   final service = FlutterBackgroundService();
    //   await service.configure(
    //     iosConfiguration: IosConfiguration(
    //       autoStart: true,
    //       onForeground: (_) {},
    //       onBackground: iosDispatcher,
    //     ),
    //     androidConfiguration: AndroidConfiguration(
    //       autoStart: false,
    //       onStart: (_) {},
    //       isForegroundMode: false,
    //       autoStartOnBoot: true,
    //     ),
    //   );
    //   service.startService();
    // } else if (Platform.isAndroid) {
    //   Workmanager().initialize(
    //     androidDispatcher, // The top level function, aka callbackDispatcher
    //     isInDebugMode: false,
    //     // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
    //   );
    //   if (Platform.isAndroid) {
    //     Workmanager().registerPeriodicTask(
    //       'androidDispatcher',
    //       'androidDispatcher',
    //       frequency: const Duration(minutes: 15),
    //     );
    //   }
    // }
  }
}

@pragma('vm:entry-point')
Future firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  // if (message.data['type'] == 'location_check') {
  //   await _configDependencies();
  //   await iosDispatcher(null);
  // }
}

@pragma('vm:entry-point')
Future<bool> iosDispatcher(ServiceInstance? service) async {
  // await _configDependencies();
  // TaskRegistry.registerTask(ContinuousClockoutCheckerTask());
  // for (var task in TaskRegistry.getRegisteredTasks()) {
  //   await task.oneTime(service);
  // }
  return true;
}

@pragma('vm:entry-point')
void androidDispatcher() async {
  // Workmanager().executeTask((task, inputData) async {
  //   print('BG Task: executeTask');
  //
  //   try {
  //     await _configDependencies();
  //     TaskRegistry.registerTask(ContinuousClockoutCheckerTask());
  //     for (var task in TaskRegistry.getRegisteredTasks()) {
  //       await task.oneTime(null);
  //     }
  //   }catch (e) {
  //     return Future.error(e);
  //   }
  //   return Future.value(true);
  // });
}

Future<void> _configDependencies() async {
  WidgetsFlutterBinding.ensureInitialized();
  DartPluginRegistrant.ensureInitialized();
  // await initHiveForFlutter(); - do no init hive in background!
  //todo: add env variable
  await dotenv.load(fileName: '.env');
  await Firebase.initializeApp();
  try {
    configureDependencies('background');
  } catch (e) {
    print('Error configuring dependencies: $e');
  }
}
