import 'package:flutter/material.dart';

class DialogUtils {
  static void showCancelConfirmationDialog(
      {required BuildContext context,
      required bool isNew,
      required void Function() onSave,
      String? message,
      void Function(BuildContext context)? onDiscard}) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          List<Widget> actions = [
            TextButton(
              onPressed: () => Navigator.pop(context, 'CANCEL'),
              child: const Text('CANCEL'),
            ),
          ];

          actions.add(isNew
              ? ElevatedButton(
                  onPressed: () => Navigator.pop(context, 'DISCARD'),
                  child: const Text('DISCARD'),
                )
              : TextButton(
                  onPressed: () => Navigator.pop(context, 'DISCARD'),
                  child: const Text('DISCARD'),
                ));

          if (!isNew) {
            actions.add(ElevatedButton(
              onPressed: () => Navigator.pop(context, 'SAVE'),
              child: const Text('SAVE'),
            ));
          }

          return AlertDialog(
            content: Text(message ??
                'You already made some changes. Discard your input?'),
            actions: actions,
          );
        }).then((value) {
      if (value == 'DISCARD') {
        onDiscard != null ? onDiscard(context) : Navigator.pop(context);
      }
      if (value == 'SAVE') {
        onSave();
      }
    });
  }

  static void showConfirmationDialog(
      {required BuildContext context,
      required void Function() onConfirm,
      String message = 'Are you sure you want to delete this record?',
      String confirmationLabel = 'DELETE'}) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            content: Text(message),
            actions: <Widget>[
              TextButton(
                onPressed: () => Navigator.pop(context, 'CANCEL'),
                child: const Text('CANCEL'),
              ),
              ElevatedButton(
                onPressed: () => Navigator.pop(context, 'CONFIRM'),
                child: Text(confirmationLabel),
              ),
            ],
          );
        }).then((value) {
      if (value == 'CONFIRM') {
        onConfirm();
      }
    });
  }
}
