import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/services/navigation_service.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';

class LoadingUtils {
  static final NavigationService _navigationService = getIt.get<NavigationService>();

  static bool _isLoadingVisible = false;

  static bool get isLoading => _isLoadingVisible;

  static showFullScreenLoader({bool barrierDismissible = true}) {
    if (!_isLoadingVisible) {
      _isLoadingVisible = true;
      final context = _navigationService.navigatorKey.currentContext;
      log("got the context in showFullScreenLoading");
      if (context == null) return;

      showDialog(
        barrierDismissible: barrierDismissible,
        context: context,
        barrierColor: AppColors.blackColor.withOpacity(0.5),
        useRootNavigator: false,
        builder: (BuildContext context) => Center(
            child: CircularProgressIndicator(
          color: AppColors.primaryRedColor,
        )),
      ).then((value) {
        _isLoadingVisible = false;
      });
    }
  }

  static hideFullScreenLoader() {
    if (!_isLoadingVisible) return;

    final context = _navigationService.navigatorKey.currentContext;
    if (context != null) {
      try {
        Navigator.of(context).pop();
      } catch (_) {}
    }
    _isLoadingVisible = false;
  }
}
