import 'package:flutter/widgets.dart';
import 'package:hmg_qline/config/dependency_injection.dart';
import 'package:hmg_qline/services/logger_service.dart';

class LifecycleHandler extends WidgetsBindingObserver {
  final void Function()? onResumed;
  final void Function()? onPaused;
  final void Function()? onDetached;
  final void Function()? onInactive;
  final void Function()? onHidden;

  LifecycleHandler({
    this.onResumed,
    this.onPaused,
    this.onDetached,
    this.onInactive,
    this.onHidden,
  });


  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    LoggerService loggerService = getIt.get<LoggerService>();
    loggerService.logInfo("[didChangeAppLifecycleState] : ${state.toString()}");
    switch (state) {
      case AppLifecycleState.resumed:
        onResumed?.call();
        break;
      case AppLifecycleState.paused:
        onPaused?.call();
        break;
      case AppLifecycleState.inactive:
        onInactive?.call();
        break;
      case AppLifecycleState.detached:
        onDetached?.call();
        break;
      case AppLifecycleState.hidden:
        onHidden?.call();
        break;
    }
  }

  void register() {
    WidgetsBinding.instance.addObserver(this);
  }

  void unregister() {
    WidgetsBinding.instance.removeObserver(this);
  }
}
