part of 'card.dart';


class _SignupConfirmationCardState extends State<SignupConfirmationCard> {
  static const int _defaultCooldownSeconds = 60;
  late final SignupConfirmationCardController _controller;

  @override
  void initState() {
    super.initState();
    _controller = SignupConfirmationCardController(
      accountProvider: context.read<AccountProvider>(),
      defaultCooldown: const Duration(seconds: _defaultCooldownSeconds),
    );
    _controller.initialize(email: widget.email);
  }

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

  @override
  void didUpdateWidget(covariant SignupConfirmationCard oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.email != widget.email) {
      _controller.updateEmail(widget.email);
    }
  }

  Future<void> _resendVerificationEmail() async {
    final locs = AppLocalizations.of(context)!;
    try {
      final result = await _controller.resendVerificationEmail();
      if (!mounted) return;
      if (result == ResendActionResult.missingEmail) {
        notifyUser(context, locs.errorEmailMissing);
        return;
      }
      if (result == ResendActionResult.sent) {
        final email = widget.email?.trim() ?? '';
        notifyUser(context, locs.signupConfirmationResent(email));
      }
    } catch (e) {
      if (!mounted) return;
      postNotifyUserOfErrorX(
        context: context,
        errorSituation: locs.signupConfirmationResendError,
        exception: e,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final locs = AppLocalizations.of(context)!;
    final email = widget.email?.trim();
    final description = (email != null && email.isNotEmpty)
        ? locs.signupConfirmationDescription(email)
        : locs.signupConfirmationDescriptionNoEmail;

    return AnimatedBuilder(
      animation: _controller,
      builder: (context, _) {
        final availability = _controller.resendAvailability;
        final canResend = availability == ResendAvailability.available;
        final isResending = availability == ResendAvailability.resending;
        final resendLabel = availability == ResendAvailability.cooldown
            ? locs.signupConfirmationResendCooldown(
                formatCooldownSeconds(_controller.cooldownRemainingSeconds),
              )
            : locs.signupConfirmationResend;

        final content = _SignupConfirmationContent(
          email: email,
          description: description,
          canResend: canResend,
          resendLabel: resendLabel,
          isResending: isResending,
          onResend: _resendVerificationEmail,
        );

        if (widget.isEmbedded) return content;

        return Card(
          elevation: 0,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(20),
            side: BorderSide(
              color: theme.dividerColor.withValues(alpha: 0.6),
            ),
          ),
          child: Padding(
            padding: const EdgeInsets.all(28),
            child: content,
          ),
        );
      },
    );
  }
}
