import 'package:flutter/animation.dart';
import 'package:rasadyar_app/presentation/routes/app_pages.dart';
import 'package:rasadyar_auth/data/services/token_storage_service.dart';
import 'package:rasadyar_core/core.dart';

class SplashLogic extends GetxController with GetTickerProviderStateMixin {
  late final AnimationController scaleController;
  late final AnimationController rotateController;
  Rxn<Animation<double>> scaleAnimation = Rxn();
  Rxn<Animation<double>> rotationAnimation = Rxn();

  var tokenService = Get.find<TokenStorageService>();

  @override
  void onInit() {
    super.onInit();
    scaleController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 1500),
    );

    rotateController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 8000),
    );

    scaleAnimation.value = Tween<double>(
      begin: 0.8,
      end: 1.2,
    ).animate(scaleController);

    rotationAnimation.value = Tween<double>(
      begin: 0.0,
      end: 1,
    ).animate(rotateController);

    rotateController.forward();
    rotateController.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        rotateController.repeat();
      } else if (status == AnimationStatus.dismissed) {
        rotateController.forward();
      }
    });

    scaleController.forward();
    scaleController.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        scaleController.reverse();
      } else if (status == AnimationStatus.dismissed) {
        scaleController.forward();
      }
    });
  }

  @override
  void onReady() {
    super.onReady();
    Future.delayed(const Duration(seconds: 1), () async {
      var module = tokenService.appModule.value;
      Get.offAndToNamed(getTargetPage(module));
    });
  }

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