import 'package:flutter/material.dart';

import '../widgets/loading_notifier.dart';

mixin OverlayService {
  OverlayEntry? _overlayEntry;

  Future<void> showLoadingNotifier({
    required BuildContext context,
    required String message,
    required VoidCallback onDismiss,
  }) async {
    if (_overlayEntry != null) return;

    final overlayState = Overlay.of(context);
    _overlayEntry = OverlayEntry(
      builder: (context) => Positioned(
        bottom: 85, // Positioned above the NavigationBar
        left: 0,
        right: 0,
        child: Center(
          child: LoadingNotifier(
            message: message,
            onDismissed: () {
              onDismiss();
              removeLoadingNotifier();
            },
          ),
        ),
      ),
    );

    overlayState.insert(_overlayEntry!);
    // overlayState.setState(() {});
  }

  void removeLoadingNotifier() {
    if (_overlayEntry != null) {
      _overlayEntry?.remove();
      _overlayEntry = null;
    }
  }

  void dispose() {
    removeLoadingNotifier();
  }
}
