import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tone_snap/ads/app_open_ad_manager.dart';
import 'package:tone_snap/utils/tracking_authorization_util.dart';

class SplashController extends GetxController with GetSingleTickerProviderStateMixin {
  var processValue = 0.0.obs;
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void onInit() {
    super.onInit();
    _controller = AnimationController(
      duration: const Duration(seconds: 10),
      vsync: this,
    );

    // 创建 Tween 并绑定到 AnimationController
    _animation = Tween<double>(begin: 0, end: 1).animate(_controller)
      ..addListener(() {
        processValue.value = _animation.value;
        if (processValue.value >= 1) {
          // 显示开屏广告
          AppOpenAdManager().showAdIfAvailable();
        }
      });

    // 启动动画
    _controller.forward();

    // 延迟3秒执行
    Future.delayed(const Duration(seconds: 3), () {
      // ATT授权
      TrackingAuthorizationUtil.requestTrackingAuthorization();
    });
  }

  @override
  void onClose() {
    _controller.dispose();
    super.onClose();
  }
}
