part of '../async_action_button.dart';

class AsyncDialogAction extends StatefulWidget {
  final String confirmLabel;
  final AsyncActionCallback onConfirm;
  final String? cancelLabel;
  final AsyncErrorBuilder? errorBuilder;

  const AsyncDialogAction({
    required this.confirmLabel,
    required this.onConfirm,
    this.cancelLabel = 'Abbrechen',
    this.errorBuilder,
    super.key,
  });

  @override
  State<AsyncDialogAction> createState() => _AsyncDialogActionState();
}

class _AsyncDialogActionState extends State<AsyncDialogAction> {
  final AsyncActionController _controller = AsyncActionController();

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => AnimatedBuilder(
    animation: _controller,
    builder: (context, _) {
      final err = _controller.error;
      return Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          if (err != null)
            Padding(
              padding: const EdgeInsets.only(bottom: 8),
              child: Text(
                err,
                textAlign: TextAlign.center,
                style: _asyncErrorTextStyle(context),
              ),
            ),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              if (widget.cancelLabel != null)
                TextButton(
                  onPressed: _controller.busy
                      ? null
                      : () => Navigator.of(context).pop(),
                  child: Text(widget.cancelLabel!),
                ),
              TextButton(
                onPressed: _controller.busy
                    ? null
                    : () async {
                        final ok = await _controller.run(
                          widget.onConfirm,
                          errorBuilder: widget.errorBuilder,
                        );
                        if (ok && context.mounted) {
                          Navigator.of(context).pop(true);
                        }
                      },
                child: _controller.busy
                    ? Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          AppProgressIndicator.small(
                            color: Theme.of(context).colorScheme.primary,
                          ),
                          const SizedBox(width: 8),
                          Text(widget.confirmLabel),
                        ],
                      )
                    : Text(widget.confirmLabel),
              ),
            ],
          ),
        ],
      );
    },
  );
}
