import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sigap/src/utils/loaders/circular_loader.dart';

import '../constants/colors.dart';
import '../helpers/helper_functions.dart';

/// A utility class for managing a full-screen loading dialog.
class TCircularFullScreenLoader {
  /// Show a full-screen loader with optional [text] below the spinner.
  static void openLoadingDialog({String? text}) {
    final isDark = THelperFunctions.isDarkMode(Get.context!);
    WidgetsBinding.instance.addPostFrameCallback((_) {
      showDialog(
        context: Get.overlayContext!,
        barrierDismissible: false,
        builder:
            (_) => PopScope(
              canPop: false,
              child: Container(
                color: isDark ? TColors.black : TColors.white,
                width: double.infinity,
                height: double.infinity,
                child: Center(
                  child: IntrinsicWidth(
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        TCircularLoader(
                          foregroundColor: TColors.primary,
                          backgroundColor:
                              isDark ? TColors.black : TColors.white,
                        ),
                        if (text != null && text.isNotEmpty) ...[
                          const SizedBox(height: 24),
                          ConstrainedBox(
                            constraints: const BoxConstraints(maxWidth: 220),
                            child: Text(
                              text,
                              style: TextStyle(
                                fontSize: 16,
                                color: isDark ? Colors.white : Colors.black,
                                fontWeight: FontWeight.w500,
                                decoration:
                                    TextDecoration.none, // Hapus underline
                              ),
                              textAlign: TextAlign.center,
                              softWrap: true,
                              overflow: TextOverflow.visible,
                            ),
                          ),
                        ],
                      ],
                    ),
                  ),
                ),
              ),
            ),
      );
    });
  }

  /// Stop the currently open loading dialog.
  static stopLoading() {
    Navigator.of(Get.overlayContext!).pop();
  }
}
