import 'package:diameter/localization_keys.dart';
import 'package:flutter/material.dart';
import 'package:flutter_translate/flutter_translate.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: Text(translate(LocalizationKeys.general_cancel).toUpperCase()),
            ),
          ];

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

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

          return AlertDialog(
            content: Text(message ?? translate(LocalizationKeys.general_confirmDiscard)),
            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,
      String? confirmationLabel}) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            content: Text(message ?? translate(LocalizationKeys.general_confirmDelete)),
            actions: <Widget>[
              TextButton(
                onPressed: () => Navigator.pop(context, 'CANCEL'),
                child: Text(translate(LocalizationKeys.general_cancel).toUpperCase()),
              ),
              ElevatedButton(
                onPressed: () => Navigator.pop(context, 'CONFIRM'),
                child: Text(confirmationLabel ?? translate(LocalizationKeys.general_confirm).toUpperCase()),
              ),
            ],
          );
        }).then((value) {
      if (value == 'CONFIRM') {
        onConfirm();
      }
    });
  }
}
