// Author: fengshengxiong
// Date: 2024/6/25
// Description: 应用程序生命周期反应器

import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:tone_snap/ads/app_open_ad_manager.dart';
import 'package:tone_snap/ads/interstitial_ad_manager.dart';

class AppLifecycleReactor {
  AppLifecycleReactor._();

  static final AppLifecycleReactor _instance = AppLifecycleReactor._();

  factory AppLifecycleReactor() {
    return _instance;
  }

  void listenToAppStateChanges() {
    AppStateEventNotifier.startListening();
    AppStateEventNotifier.appStateStream.forEach((state) => _onAppStateChanged(state));
  }

  void _onAppStateChanged(AppState appState) {
    // 如果应用程序正在恢复前台，尝试显示开屏广告
    if (appState == AppState.foreground && !InterstitialAdManager().isShowingAd) {
      AppOpenAdManager().showAdIfAvailable();
    }
  }
}