import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../../../routes/route_paths.dart';
import '../../../../routes/app_router.dart';

/// 创建成功页面 - 显示钱包创建成功信息
/// 展示序列号和三个链的钱包地址
class WalletCreatedPage extends ConsumerWidget {
  /// KAVA 钱包地址
  final String kavaAddress;
  /// DST 钱包地址
  final String dstAddress;
  /// BSC 钱包地址
  final String bscAddress;
  /// 用户序列号
  final String userSerialNum;
  /// 推荐码
  final String? referralCode;

  const WalletCreatedPage({
    super.key,
    required this.kavaAddress,
    required this.dstAddress,
    required this.bscAddress,
    required this.userSerialNum,
    this.referralCode,
  });

  /// 复制内容到剪贴板
  void _copyToClipboard(BuildContext context, String text, String label) {
    Clipboard.setData(ClipboardData(text: text));
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text('$label 已复制'),
        backgroundColor: const Color(0xFFD4A84B),
        duration: const Duration(seconds: 2),
      ),
    );
  }

  /// 进入钱包（龙虎榜）
  void _enterWallet(BuildContext context) {
    context.go(RoutePaths.ranking);
  }

  /// 分享给好友 - 跳转到分享页面
  void _shareToFriends(BuildContext context) {
    // 构建分享链接 (带推荐码)
    final shareLink = referralCode != null
        ? 'https://rwaapi.szaiai.com/invite?code=$referralCode'
        : 'https://rwaapi.szaiai.com/invite?seq=$userSerialNum';

    context.push(
      RoutePaths.share,
      extra: SharePageParams(
        shareLink: shareLink,
        referralCode: referralCode,
      ),
    );
  }

  /// 格式化地址显示 (显示前6位和后4位)
  String _formatAddress(String address) {
    if (address.length <= 10) return address;
    return '${address.substring(0, 6)}...${address.substring(address.length - 4)}';
  }

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        // 渐变背景 - 从浅米色到金黄色
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [
              Color(0xFFFFF8F0), // 更浅的米色顶部
              Color(0xFFFFE4B5), // 金黄色底部
            ],
          ),
        ),
        child: SafeArea(
          child: Column(
            children: [
              // 内容区域
              Expanded(
                child: SingleChildScrollView(
                  padding: const EdgeInsets.symmetric(horizontal: 20),
                  child: Column(
                    children: [
                      const SizedBox(height: 48),
                      // 成功图标
                      _buildSuccessIcon(),
                      const SizedBox(height: 20),
                      // 标题和副标题
                      _buildTitle(),
                      const SizedBox(height: 28),
                      // 钱包信息卡片
                      _buildWalletInfoCard(context),
                    ],
                  ),
                ),
              ),
              // 底部按钮区域
              _buildBottomSection(context),
            ],
          ),
        ),
      ),
    );
  }

  /// 构建成功图标
  Widget _buildSuccessIcon() {
    return Container(
      width: 120,
      height: 120,
      decoration: const BoxDecoration(
        color: Color(0xFFFFF3D6), // 浅黄色背景
        shape: BoxShape.circle,
      ),
      child: Center(
        child: Container(
          width: 64,
          height: 64,
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            border: Border.all(
              color: const Color(0xFFD4A84B),
              width: 3,
            ),
          ),
          child: const Center(
            child: Icon(
              Icons.check,
              size: 36,
              color: Color(0xFFD4A84B),
            ),
          ),
        ),
      ),
    );
  }

  /// 构建标题
  Widget _buildTitle() {
    return const Column(
      children: [
        Text(
          '账号创建成功',
          style: TextStyle(
            fontSize: 26,
            fontFamily: 'Inter',
            fontWeight: FontWeight.w700,
            height: 1.25,
            letterSpacing: -0.5,
            color: Color(0xFF5D4037),
          ),
        ),
        SizedBox(height: 8),
        Text(
          '已为您创建钱包与序列号',
          style: TextStyle(
            fontSize: 15,
            fontFamily: 'Inter',
            fontWeight: FontWeight.w500,
            height: 1.5,
            color: Color(0xFF8B7355),
          ),
        ),
      ],
    );
  }

  /// 构建钱包信息卡片
  Widget _buildWalletInfoCard(BuildContext context) {
    return Container(
      width: double.infinity,
      decoration: BoxDecoration(
        color: const Color(0xFFFFFDF5),
        borderRadius: BorderRadius.circular(16),
        boxShadow: const [
          BoxShadow(
            color: Color(0x0D000000),
            blurRadius: 4,
            offset: Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        children: [
          // 序列号 - 放在最上面
          _buildInfoItem(
            context: context,
            iconWidget: _buildKeyIcon(),
            label: '序列号',
            value: userSerialNum,
            isAddress: false,
            showDivider: true,
          ),
          // KAVA 地址
          _buildInfoItem(
            context: context,
            iconWidget: _buildWalletIcon(),
            label: 'KAVA',
            value: kavaAddress,
            isAddress: true,
            showDivider: true,
          ),
          // DST 地址
          _buildInfoItem(
            context: context,
            iconWidget: _buildWalletIcon(),
            label: 'DST',
            value: dstAddress,
            isAddress: true,
            showDivider: true,
          ),
          // BSC 地址
          _buildInfoItem(
            context: context,
            iconWidget: _buildWalletIcon(),
            label: 'BSC',
            value: bscAddress,
            isAddress: true,
            showDivider: false,
          ),
        ],
      ),
    );
  }

  /// 构建钥匙图标 (序列号)
  Widget _buildKeyIcon() {
    return Container(
      width: 48,
      height: 48,
      decoration: BoxDecoration(
        color: const Color(0xFFFFF3D6),
        borderRadius: BorderRadius.circular(10),
      ),
      child: const Center(
        child: Icon(
          Icons.vpn_key_outlined,
          color: Color(0xFFD4A84B),
          size: 24,
        ),
      ),
    );
  }

  /// 构建钱包图标 (地址)
  Widget _buildWalletIcon() {
    return Container(
      width: 48,
      height: 48,
      decoration: BoxDecoration(
        color: const Color(0xFFFFF3D6),
        borderRadius: BorderRadius.circular(10),
      ),
      child: const Center(
        child: Icon(
          Icons.account_balance_wallet_outlined,
          color: Color(0xFFD4A84B),
          size: 24,
        ),
      ),
    );
  }

  /// 构建信息项
  Widget _buildInfoItem({
    required BuildContext context,
    required Widget iconWidget,
    required String label,
    required String value,
    required bool isAddress,
    required bool showDivider,
  }) {
    // 格式化显示值
    final displayValue = isAddress ? _formatAddress(value) : value;

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
      decoration: BoxDecoration(
        border: showDivider
            ? const Border(
                bottom: BorderSide(
                  color: Color(0x1A5D4037),
                  width: 1,
                ),
              )
            : null,
      ),
      child: Row(
        children: [
          // 图标
          iconWidget,
          const SizedBox(width: 14),
          // 标签和值 - 单行显示 "标签: 值"
          Expanded(
            child: FittedBox(
              fit: BoxFit.scaleDown,
              alignment: Alignment.centerLeft,
              child: Text(
                '$label: $displayValue',
                style: const TextStyle(
                  fontSize: 16,
                  fontFamily: 'Inter',
                  fontWeight: FontWeight.w500,
                  height: 1.5,
                  color: Color(0xFF5D4037),
                ),
              ),
            ),
          ),
          // 复制按钮
          GestureDetector(
            onTap: () => _copyToClipboard(context, value, label),
            child: Container(
              padding: const EdgeInsets.all(8),
              child: const Icon(
                Icons.copy_outlined,
                color: Color(0xFFD4A84B),
                size: 22,
              ),
            ),
          ),
        ],
      ),
    );
  }

  /// 构建底部区域 (按钮 + 提示文字)
  Widget _buildBottomSection(BuildContext context) {
    return Container(
      padding: const EdgeInsets.fromLTRB(20, 16, 20, 24),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // 进入我的钱包按钮 - 金黄色
          GestureDetector(
            onTap: () => _enterWallet(context),
            child: Container(
              width: double.infinity,
              height: 52,
              decoration: BoxDecoration(
                color: const Color(0xFFD4A84B),
                borderRadius: BorderRadius.circular(12),
                boxShadow: const [
                  BoxShadow(
                    color: Color(0x33D4A84B),
                    blurRadius: 8,
                    offset: Offset(0, 4),
                  ),
                ],
              ),
              child: const Center(
                child: Text(
                  '进入我的钱包',
                  style: TextStyle(
                    fontSize: 17,
                    fontFamily: 'Inter',
                    fontWeight: FontWeight.w700,
                    height: 1.5,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ),
          const SizedBox(height: 12),
          // 分享给好友按钮 - 深棕色
          GestureDetector(
            onTap: () => _shareToFriends(context),
            child: Container(
              width: double.infinity,
              height: 52,
              decoration: BoxDecoration(
                color: const Color(0xFF5D4037),
                borderRadius: BorderRadius.circular(12),
              ),
              child: const Center(
                child: Text(
                  '分享给好友',
                  style: TextStyle(
                    fontSize: 17,
                    fontFamily: 'Inter',
                    fontWeight: FontWeight.w600,
                    height: 1.5,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ),
          const SizedBox(height: 16),
          // 提示文字
          const Text(
            '助记词仅显示一次，请妥善保管。',
            style: TextStyle(
              fontSize: 13,
              fontFamily: 'Inter',
              fontWeight: FontWeight.w400,
              height: 1.5,
              color: Color(0xFF8B7355),
            ),
            textAlign: TextAlign.center,
          ),
        ],
      ),
    );
  }
}
