import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../../core/l10n/app_localizations.dart';
import '../../../core/theme/app_colors.dart';
import '../../../data/models/asset/recharge_record.dart';

class DepositDetailScreen extends StatelessWidget {
  const DepositDetailScreen({super.key, required this.record});

  final RechargeRecord record;

  @override
  Widget build(BuildContext context) {
    final cs = Theme.of(context).colorScheme;
    final isDark = Theme.of(context).brightness == Brightness.dark;

    return Scaffold(
      appBar: AppBar(
        title: Text(AppLocalizations.of(context)!.depositDetail, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w600)),
        centerTitle: true,
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // ── 金额部分 ──────────────────────────────
            Container(
              padding: const EdgeInsets.all(20),
              decoration: BoxDecoration(
                color: isDark ? AppColors.darkBgSecondary : AppColors.lightBgSecondary,
                borderRadius: BorderRadius.circular(12),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    AppLocalizations.of(context)!.amountLabel,
                    style: TextStyle(
                      color: cs.onSurface.withAlpha(120),
                      fontSize: 13,
                    ),
                  ),
                  const SizedBox(height: 8),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.baseline,
                    textBaseline: TextBaseline.alphabetic,
                    children: [
                      Text(
                        '+${record.amount}',
                        style: TextStyle(
                          color: AppColors.rise,
                          fontSize: 28,
                          fontWeight: FontWeight.w700,
                        ),
                      ),
                      const SizedBox(width: 8),
                      Text(
                        'USDT',
                        style: TextStyle(
                          color: cs.onSurface.withAlpha(120),
                          fontSize: 14,
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(height: 12),
                  Text(
                    AppLocalizations.of(context)!.completed,
                    style: TextStyle(
                      color: AppColors.rise,
                      fontSize: 12,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ],
              ),
            ),
            const SizedBox(height: 24),

            // ── 详情信息 ──────────────────────────────
            Container(
              padding: const EdgeInsets.all(16),
              decoration: BoxDecoration(
                color: isDark ? AppColors.darkBgSecondary : AppColors.lightBgSecondary,
                borderRadius: BorderRadius.circular(12),
              ),
              child: Column(
                children: [
                  _DetailRow(
                    label: AppLocalizations.of(context)!.depositCurrency,
                    value: 'USDT',
                    cs: cs,
                  ),
                  const SizedBox(height: 16),
                  _DetailRow(
                    label: AppLocalizations.of(context)!.depositAddress,
                    value: record.address,
                    cs: cs,
                    isCopyable: true,
                    copyValue: record.address,
                  ),
                  const SizedBox(height: 16),
                  _DetailRow(
                    label: AppLocalizations.of(context)!.txHash,
                    value: record.txId,
                    cs: cs,
                    isCopyable: record.txId.isNotEmpty,
                    copyValue: record.txId,
                    truncate: true,
                  ),
                  const SizedBox(height: 16),
                  _DetailRow(
                    label: AppLocalizations.of(context)!.applyTime,
                    value: record.createTime,
                    cs: cs,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class _DetailRow extends StatelessWidget {
  const _DetailRow({
    required this.label,
    required this.value,
    required this.cs,
    this.isCopyable = false,
    this.copyValue,
    this.truncate = false,
  });

  final String label;
  final String value;
  final ColorScheme cs;
  final bool isCopyable;
  final String? copyValue;
  final bool truncate;

  @override
  Widget build(BuildContext context) {
    final displayValue = truncate && value.length > 16
        ? '${value.substring(0, 8)}...${value.substring(value.length - 8)}'
        : value;

    return GestureDetector(
      onTap: isCopyable
          ? () {
              if (copyValue != null && copyValue!.isNotEmpty) {
                Clipboard.setData(ClipboardData(text: copyValue!));
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text(AppLocalizations.of(context)!.copied), duration: const Duration(seconds: 1)),
                );
              }
            }
          : null,
      child: Row(
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  label,
                  style: TextStyle(color: cs.onSurface.withAlpha(120), fontSize: 12),
                ),
                const SizedBox(height: 4),
                Text(
                  displayValue,
                  style: TextStyle(color: cs.onSurface, fontSize: 14, fontWeight: FontWeight.w500),
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                ),
              ],
            ),
          ),
          if (isCopyable) ...[
            const SizedBox(width: 8),
            Icon(Icons.content_copy_outlined, size: 16, color: cs.onSurface.withAlpha(120)),
          ],
        ],
      ),
    );
  }
}
