import 'package:flutter/material.dart';
import '../../../../app/theme/app_colors.dart';
import '../../../../app/theme/app_typography.dart';
import '../../../../app/theme/app_spacing.dart';

/// A8. 出示券码页面（消费者端）
///
/// QR码 + 数字券码 + 倒计时 + 自动调高亮度
/// 商户扫码核销
class RedeemQrPage extends StatefulWidget {
  const RedeemQrPage({super.key});

  @override
  State<RedeemQrPage> createState() => _RedeemQrPageState();
}

class _RedeemQrPageState extends State<RedeemQrPage> {
  int _remainingSeconds = 300; // 5 minutes

  @override
  void initState() {
    super.initState();
    _startCountdown();
  }

  void _startCountdown() {
    Future.doWhile(() async {
      await Future.delayed(const Duration(seconds: 1));
      if (!mounted) return false;
      setState(() => _remainingSeconds--);
      return _remainingSeconds > 0;
    });
  }

  String get _formattedTime {
    final min = _remainingSeconds ~/ 60;
    final sec = _remainingSeconds % 60;
    return '${min.toString().padLeft(2, '0')}:${sec.toString().padLeft(2, '0')}';
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.gray900,
      appBar: AppBar(
        backgroundColor: AppColors.gray900,
        foregroundColor: Colors.white,
        title: const Text('出示券码'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // Coupon Info
            Text('星巴克 \$25 礼品卡', style: AppTypography.h2.copyWith(color: Colors.white)),
            const SizedBox(height: 4),
            Text('面值 \$25.00', style: AppTypography.bodyMedium.copyWith(color: Colors.white60)),
            const SizedBox(height: 32),

            // QR Code Area
            Container(
              width: 240,
              height: 240,
              padding: const EdgeInsets.all(16),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: AppSpacing.borderRadiusLg,
              ),
              child: Container(
                decoration: BoxDecoration(
                  border: Border.all(color: AppColors.gray200),
                  borderRadius: AppSpacing.borderRadiusSm,
                ),
                child: const Center(
                  child: Icon(Icons.qr_code_2_rounded, size: 160, color: AppColors.gray900),
                ),
              ),
            ),
            const SizedBox(height: 20),

            // Numeric Code
            Container(
              padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
              decoration: BoxDecoration(
                color: Colors.white.withValues(alpha: 0.1),
                borderRadius: AppSpacing.borderRadiusFull,
              ),
              child: Text(
                '8429 3751 0062',
                style: AppTypography.h1.copyWith(
                  color: Colors.white,
                  letterSpacing: 2,
                  fontFamily: 'monospace',
                ),
              ),
            ),
            const SizedBox(height: 24),

            // Countdown
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Icon(Icons.timer_outlined, color: Colors.white54, size: 18),
                const SizedBox(width: 6),
                Text(
                  '有效时间 $_formattedTime',
                  style: AppTypography.bodyMedium.copyWith(color: Colors.white54),
                ),
              ],
            ),
            const SizedBox(height: 8),
            TextButton(
              onPressed: () {
                setState(() => _remainingSeconds = 300);
              },
              child: Text('刷新券码', style: AppTypography.labelMedium.copyWith(color: AppColors.primaryLight)),
            ),
            const SizedBox(height: 40),

            // Hint
            Container(
              margin: const EdgeInsets.symmetric(horizontal: 40),
              padding: const EdgeInsets.all(14),
              decoration: BoxDecoration(
                color: Colors.white.withValues(alpha: 0.08),
                borderRadius: AppSpacing.borderRadiusMd,
              ),
              child: Row(
                children: [
                  const Icon(Icons.info_outline_rounded, color: Colors.white38, size: 18),
                  const SizedBox(width: 10),
                  Expanded(
                    child: Text(
                      '请将此码出示给商户扫描，屏幕已自动调至最高亮度',
                      style: AppTypography.caption.copyWith(color: Colors.white54),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
