import 'package:flutter/material.dart';

import 'package:fotodocumentation/main.dart' show logger;
import 'package:fotodocumentation/l10n/app_localizations.dart';
import 'package:fotodocumentation/pages/ui_utils/dialog/snackbar_utils.dart';
import 'package:fotodocumentation/utils/di_container.dart';

class DeleteDialog extends StatelessWidget {
  static SnackbarUtils get _snackbarUtils => DiContainer.get();

  const DeleteDialog({super.key});

  static Future<void> show(BuildContext context, Future<DeleteDialogResult> Function() doDelete) async {
    await _openDialog(context).then((value) async {
      if (value != null && value && context.mounted) {
        logger.d("Delete popup result $value");
        var result = await doDelete();
        if (context.mounted && result.msg.isNotEmpty) {
          _snackbarUtils.showSnackbar(context, result.msg, result.warning);
        }
      }
    });
  }

  static Future<bool?> _openDialog(BuildContext context) async {
    return showDialog<bool>(
      context: context,
      barrierDismissible: false, // user must tap button!
      builder: (BuildContext context) {
        return const DeleteDialog(
          key: Key("delete_dialog"),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;

    return AlertDialog(
      backgroundColor: Colors.white,
      scrollable: false,
      titlePadding: const EdgeInsets.all(16.0),
      contentPadding: const EdgeInsets.fromLTRB(16.0, 0, 16.0, 16.0),
      title: _titleWidget(context, loc),
      content: _content(context, loc),
    );
  }

  Widget _titleWidget(BuildContext context, AppLocalizations loc) {
    return Card(
      elevation: 4,
      margin: EdgeInsets.zero,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
        side: BorderSide(
          color: Colors.grey[300]!,
          width: 1,
        ),
      ),
      child: Padding(
        padding: const EdgeInsets.all(12.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                Icon(
                  Icons.warning_amber_rounded,
                  size: 32,
                  color: Colors.orange[700],
                ),
                const SizedBox(width: 16),
                Text(
                  loc.deleteDialogTitle,
                  style: Theme.of(context).textTheme.headlineSmall?.copyWith(
                        fontWeight: FontWeight.bold,
                      ),
                ),
              ],
            ),
            IconButton(
              key: const Key("close_button"),
              icon: const Icon(Icons.close),
              onPressed: () => Navigator.pop(context, false),
            ),
          ],
        ),
      ),
    );
  }

  Widget _content(BuildContext context, AppLocalizations loc) {
    return SizedBox(
      width: 400,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Card(
            elevation: 2,
            margin: EdgeInsets.zero,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(12),
              side: BorderSide(
                color: Colors.grey[300]!,
                width: 1,
              ),
            ),
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: Row(
                children: [
                  Icon(
                    Icons.warning_amber_rounded,
                    size: 48,
                    color: Colors.orange[700],
                  ),
                  const SizedBox(width: 16),
                  Expanded(
                    child: Text(
                      loc.deleteDialogText,
                      style: Theme.of(context).textTheme.bodyLarge,
                    ),
                  ),
                ],
              ),
            ),
          ),
          const SizedBox(height: 16),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              TextButton(
                key: const Key("delete_dialog:cancel"),
                style: TextButton.styleFrom(
                  padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(8),
                  ),
                ),
                onPressed: () => Navigator.pop(context, false),
                child: Text(loc.deleteDialogButtonCancel),
              ),
              const SizedBox(width: 8),
              ElevatedButton.icon(
                key: const Key("delete_dialog:approve"),
                style: ElevatedButton.styleFrom(
                  padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
                  backgroundColor: Colors.red[600],
                  foregroundColor: Colors.white,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(8),
                  ),
                ),
                onPressed: () => Navigator.pop(context, true),
                icon: const Icon(Icons.delete, size: 20),
                label: Text(loc.deleteDialogButtonApprove),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

class DeleteDialogResult {
  final String msg;
  final bool warning;

  DeleteDialogResult({required this.msg, required this.warning});
}
