// Author: fengshengxiong
// Date: 2024/6/25
// Description: 开屏广告

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:tone_snap/routes/app_routes.dart';
import 'dart:io' show Platform;

import 'package:tone_snap/utils/log_util.dart';

class AppOpenAdManager {
  AppOpenAdManager._();

  static final AppOpenAdManager _instance = AppOpenAdManager._();

  factory AppOpenAdManager() => _instance;

  /// 加载和显示广告之间允许的最长持续时间
  final Duration maxCacheDuration = const Duration(hours: 4);

  /// 跟踪加载时间，这样我们就不会显示过期的广告
  DateTime? _appOpenLoadTime;

  AppOpenAd? _appOpenAd;
  bool _isShowingAd = false;

  /// 开屏广告单元id
  final adUnitId = Platform.isAndroid
      ? (kDebugMode ? 'ca-app-pub-3940256099942544/9257395921' : '')
      : (kDebugMode ? 'ca-app-pub-3940256099942544/5575463023' : 'ca-app-pub-5684307632319406/2523581084');

  /// 加载广告
  void loadAd() async {
    final List<ConnectivityResult> connectivityResult = await (Connectivity().checkConnectivity());
    if (connectivityResult.contains(ConnectivityResult.none)) {
      return;
    }
    if (isAdAvailable) {
      return;
    }
    AppOpenAd.load(
      adUnitId: adUnitId,
      request: const AdRequest(),
      adLoadCallback: AppOpenAdLoadCallback(
        onAdLoaded: (ad) {
          LogUtil.d('开屏广告加载完成');
          _appOpenAd = ad;
          _appOpenLoadTime = DateTime.now();
        },
        onAdFailedToLoad: (error) {
          LogUtil.e('开屏广告加载失败: $error');
          // Handle the error.
        },
      ),
    );
  }

  /// 是否可以播放广告
  bool get isAdAvailable {
    return _appOpenAd != null;
  }

  /// 显示广告（如果存在且尚未显示）
  /// 如果先前缓存的广告已过期，则只加载并缓存新广告
  void showAdIfAvailable() {
    if (!isAdAvailable) {
      LogUtil.d('尝试在可用之前显示广告');
      loadAd();

      _openInitial();
      return;
    }
    if (_isShowingAd) {
      LogUtil.d('尝试在已显示广告的情况下显示广告');
      return;
    }
    if (DateTime.now().subtract(maxCacheDuration).isAfter(_appOpenLoadTime!)) {
      LogUtil.d('超过了最大缓存持续时间。正在加载另一个广告');
      _appOpenAd!.dispose();
      _appOpenAd = null;
      loadAd();

      _openInitial();
      return;
    }
    // 设置 fullScreenContentCallback 并显示广告
    _appOpenAd!.fullScreenContentCallback = FullScreenContentCallback(
      // 暂停应用程序中的活动或记录广告展示的时间
      onAdShowedFullScreenContent: (ad) {
        LogUtil.d('$ad onAdShowedFullScreenContent');
        // 隐藏状态栏
        SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: <SystemUiOverlay>[]);

        _isShowingAd = true;
      },
      // 更适合用于跟踪广告展示的次数和效果，以及执行与广告展示相关的操作
      onAdImpression: (ad) {
        LogUtil.d('$ad onAdImpression');
      },
      onAdFailedToShowFullScreenContent: (ad, error) {
        LogUtil.d('$ad onAdFailedToShowFullScreenContent: $error');
        _isShowingAd = false;
        ad.dispose();
        _appOpenAd = null;

        _openInitial();
      },
      onAdDismissedFullScreenContent: (ad) {
        LogUtil.d('$ad onAdDismissedFullScreenContent');
        // 显示状态栏（用户关闭广告后）
        SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: SystemUiOverlay.values);

        _isShowingAd = false;
        ad.dispose();
        _appOpenAd = null;
        loadAd();

        _openInitial();
      },
      onAdClicked: (ad) {
        LogUtil.d('$ad onAdClicked');
      },
    );
    _appOpenAd!.show();
  }

  /// 打开首个页面
  void _openInitial() {
    Get.offNamed(AppRoutes.initialA);
  }
}