import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tone_snap/data/api/tikustok_api.dart';
import 'package:tone_snap/data/enum/app_side_enum.dart';
import 'package:tone_snap/data/models/base_model.dart';
import 'package:tone_snap/data/models/isocode_model.dart';
import 'package:tone_snap/global/app_config.dart';
import 'package:tone_snap/global/app_tracking_transparency_manager.dart';
import 'package:tone_snap/routes/app_routes.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: 1),
      vsync: this,
    );

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

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

    AppTrackingTransparencyManager().requestATT();
  }

  @override
  void onReady() {
    super.onReady();
    _getIp();
  }

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

  /// 获取所在区域、ip
  Future<void> _getIp() async {
    BaseModel<IosCodeModel>? model = await TikUsTokApi.getIp();
    if (model != null && model.success && model.data?.isoCode != null) {
      AppConfig.isoCode = model.data!.isoCode!;
    }
  }

  /// 打开首个页面
  void _openInitial() {
    Get.offNamed(AppConfig.appSideEnum == AppSideEnum.sideA ? AppRoutes.initialA : AppRoutes.initialB);
  }
}
