import 'package:flutter/material.dart';
import 'package:loopin/IM/im_service.dart';

class LifecycleHandler with WidgetsBindingObserver {
  static bool isInForeground = true;
  static DateTime appBecameActiveTime = DateTime.now();
  static VoidCallback? onAppResumed; // 添加回调函数

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        isInForeground = true;
        appBecameActiveTime = DateTime.now();
        if (onAppResumed != null) {
          onAppResumed!();
        }
        logger.i("App 回到前台：$appBecameActiveTime");
        break;
      case AppLifecycleState.inactive:
        logger.i("App 进入非活跃状态 (inactive)");
        break;
      case AppLifecycleState.paused:
        isInForeground = false;
        logger.i("App 进入后台 (paused)");
        break;
      case AppLifecycleState.detached:
        // 处理安卓悬浮导航切后台视频会自动开始播放的问题
        isInForeground = false;
        logger.i("App 被分离 (detached)");
        // if (Get.isRegistered<VideoModuleController>()) {
        //   final videoModuleController = Get.find<VideoModuleController>();
        //   logger.w(videoModuleController.videoTabIndex.value);
        //   logger.w(videoModuleController.videoPlayFriend.value);
        //   logger.w(videoModuleController.videoPlayIndex.value);
        //   RecommendModule.playDispose();
        // }

        break;
      case AppLifecycleState.hidden:
        isInForeground = false;
        logger.i("App 被隐藏 (hidden)");
        break;
    }
  }
}
