import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tone_snap/data/app_config.dart';
import 'package:tone_snap/data/models/base_model.dart';
import 'package:tone_snap/data/sideb/api/tikustok_api.dart';
import 'package:tone_snap/data/sideb/models/isocode_model.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) {
          _openInitial();
        }
      });

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

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

  /// 获取所在区域、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!;
    }
  }

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

  /// 打开初始页面
  void _openInitial() {
    Get.offNamed(AppRoutes.initialB);
  }
}
