import 'dart:async';

import 'package:get/get.dart';
import 'package:vitalapp/pages/redirect/redirect.dart';
import 'package:vitalapp/routes/nav_ids.dart';
import 'package:vitalapp/routes/routes.dart';

abstract class NavGotoHelper {
  static Timer? _timer;

  static void goto(String name) {
    print("❤goto: $name");
    if (_timer != null) {
      _timer!.cancel();
      _timer = null;
    }

    _timer = Timer.periodic(const Duration(milliseconds: 100), (timer) {
      _check(name);
    });
  }

  static void _check(String name) {
    if (RedirectPage.isBusy) {
      return;
    }

    _timer!.cancel();
    _timer = null;

    print("GOTO $name");
    Routes.parameters["name"] = name;
    Get.offNamed('/redirect', id: NavIds.HOME);
  }
}
