import 'dart:async';

import 'package:get/get.dart';
import 'package:hello_wallpaper/applovin_max/applovin_manage.dart';
import 'package:hello_wallpaper/routes/app_pages.dart';

class SplashScreenController extends GetxController {
  Timer? _timer;
  int _timeCount = 15;

  @override
  void onInit() {
    super.onInit();
    ApplovinManage().initializeInterstitialAds(onGoHomeTap: _openHomePage);
    _startTimer();
  }

  @override
  void onClose() {
    _stopTimer();
    super.onClose();
  }

  /// 开始定时器
  void _startTimer() {
    _timer = Timer.periodic(const Duration(seconds: 1), (Timer t) {
      if (_timeCount <= 0) {
        _openHomePage();
        return;
      }
      _timeCount--;
    });
  }

  /// 停止定时器
  void _stopTimer() {
    _timer?.cancel();
    _timer = null;
  }

  /// 打开首页
  void _openHomePage() {
    _stopTimer();
    Get.offAndToNamed(AppPages.home);
  }
}
