import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/extensions/route_extensions.dart';
import 'package:hmg_patient_app_new/services/navigation_service.dart';

abstract class DialogService {
  Future<void> showErrorBottomSheet({required String message, Function()? onOkPressed});
}

class DialogServiceImp implements DialogService {
  final NavigationService navigationService;

  DialogServiceImp({required this.navigationService});

  @override
  Future<void> showErrorBottomSheet({required String message, Function()? onOkPressed}) async {
    final context = navigationService.navigatorKey.currentContext;
    if (context == null) return;

    await showModalBottomSheet(
      context: context,
      isScrollControlled: false,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
      ),
      builder: (_) => _ErrorBottomSheet(message: message, onOkPressed: onOkPressed),
    );
  }
}

class _ErrorBottomSheet extends StatelessWidget {
  final String message;
  final Function()? onOkPressed;

  const _ErrorBottomSheet({required this.message, this.onOkPressed});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          const Icon(Icons.error_outline, color: Colors.red, size: 40),
          const SizedBox(height: 12),
          Text(
            "Error",
            style: Theme.of(context).textTheme.titleLarge?.copyWith(
                  color: Colors.red,
                  fontWeight: FontWeight.bold,
                ),
          ),
          const SizedBox(height: 8),
          Text(
            message,
            textAlign: TextAlign.center,
            style: Theme.of(context).textTheme.bodyMedium,
          ),
          const SizedBox(height: 16),
          ElevatedButton(
            onPressed: () {
              if (onOkPressed != null) {
                onOkPressed!();
              } else {
                context.pop();
              }
            },
            style: ElevatedButton.styleFrom(
              backgroundColor: Colors.red,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(8),
              ),
            ),
            child: const Text("OK"),
          ),
        ],
      ),
    );
  }
}
