import 'package:flutter/material.dart';
import '../game/zentap_game.dart';

/// Manages app lifecycle events for game suspension and resumption
class AppLifecycleManager with WidgetsBindingObserver {
  static AppLifecycleManager? _instance;
  ZenTapGame? _currentGame;

  AppLifecycleManager._();

  static AppLifecycleManager get instance {
    _instance ??= AppLifecycleManager._();
    return _instance!;
  }

  /// Initialize the lifecycle manager
  void initialize() {
    WidgetsBinding.instance.addObserver(this);
  }

  /// Dispose the lifecycle manager
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    _currentGame = null;
  }

  /// Register the current active game instance
  void setCurrentGame(ZenTapGame? game) {
    _currentGame = game;
  }

  /// Get the current active game instance
  ZenTapGame? getCurrentGame() {
    return _currentGame;
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    
    // Handle app lifecycle changes for game suspension
    switch (state) {
      case AppLifecycleState.resumed:
        // App came back to foreground, resume game if there's an active game
        _currentGame?.resumeGame();
        break;
      case AppLifecycleState.inactive:
      case AppLifecycleState.paused:
      case AppLifecycleState.detached:
      case AppLifecycleState.hidden:
        // App went to background, screen turned off, or app is being closed
        // Suspend the game to save battery and resources
        _currentGame?.pauseGame();
        break;
    }
  }
}