import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:hmg_patient_app_new/core/api_consts.dart';
import 'package:hmg_patient_app_new/core/enums.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/services/navigation_service.dart';

class LoaderBottomSheet {
  static final NavigationService _navService = GetIt.I<NavigationService>();
  static bool _isVisible = false;

  static void showLoader({String? loadingText}) {
    if (_isVisible) return;

    _isVisible = true;

    showModalBottomSheet(
      context: _navService.navigatorKey.currentContext!,
      isDismissible: (ApiConsts.appEnvironmentType == AppEnvironmentTypeEnum.uat || kDebugMode) ? true : false,
      enableDrag: false,
      backgroundColor: Colors.transparent,
      builder: (_) {
        return Container(
          height: MediaQuery.of(_navService.navigatorKey.currentContext!).size.height * 0.3,
          decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
          ),
          child: Center(
            child: Utils.getLoadingWidget(loadingText: loadingText),
          ),
        );
      },
    ).whenComplete(() {
      // reset state if dismissed by system
      _isVisible = false;
    });
  }

  static void hideLoader() {
    if (_isVisible) {
      Navigator.of(_navService.navigatorKey.currentContext!).pop();
      _isVisible = false;
    }
  }
}
