part of 'card.dart';


class _SignupConfirmationCardState extends State<SignupConfirmationCard> {
  static const int _defaultCooldownSeconds = 60;

  Timer? _cooldownTimer;
  int _cooldownRemainingSeconds = 0;
  bool _isResending = false;

  @override
  void initState() {
    super.initState();
    _startCooldown(_defaultCooldownSeconds);
  }

  @override
  void dispose() {
    _cooldownTimer?.cancel();
    super.dispose();
  }

  bool get _isCooldownActive => _cooldownRemainingSeconds > 0;

  void _startCooldown(int seconds) {
    _cooldownTimer?.cancel();
    if (seconds <= 0) {
      setState(() => _cooldownRemainingSeconds = 0);
      return;
    }
    setState(() => _cooldownRemainingSeconds = seconds);
    _cooldownTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
      if (!mounted) {
        timer.cancel();
        return;
      }
      if (_cooldownRemainingSeconds <= 1) {
        timer.cancel();
        setState(() => _cooldownRemainingSeconds = 0);
        return;
      }
      setState(() => _cooldownRemainingSeconds -= 1);
    });
  }

  Future<void> _resendVerificationEmail() async {
    final email = widget.email?.trim();
    final locs = AppLocalizations.of(context)!;
    if (email == null || email.isEmpty) {
      notifyUser(context, locs.errorEmailMissing);
      return;
    }
    if (_isResending || _isCooldownActive) return;

    setState(() => _isResending = true);
    try {
      await context.read<AccountProvider>().resendVerificationEmail(email);
      if (!mounted) return;
      notifyUser(context, locs.signupConfirmationResent(email));
      _startCooldown(_defaultCooldownSeconds);
    } catch (e) {
      if (!mounted) return;
      postNotifyUserOfErrorX(
        context: context,
        errorSituation: locs.signupConfirmationResendError,
        exception: e,
      );
    } finally {
      if (mounted) {
        setState(() => _isResending = false);
      }
    }
  }

  String _formatCooldown(int seconds) {
    final minutes = seconds ~/ 60;
    final remainingSeconds = seconds % 60;
    if (minutes > 0) {
      return '$minutes:${remainingSeconds.toString().padLeft(2, '0')}';
    }
    return remainingSeconds.toString();
  }

  @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;
    final canResend = !_isResending && !_isCooldownActive && email != null && email.isNotEmpty;
    final resendLabel = _isCooldownActive
        ? locs.signupConfirmationResendCooldown(_formatCooldown(_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,
      ),
    );
  }
}
