import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../../core/constants/app_colors.dart';
import '../../../../core/utils/format_utils.dart';
import '../../../providers/mining_providers.dart';

class AssetCard extends ConsumerWidget {
  final String accountSequence;

  const AssetCard({super.key, required this.accountSequence});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final accountAsync = ref.watch(shareAccountProvider(accountSequence));

    return Container(
      width: double.infinity,
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        gradient: const LinearGradient(
          colors: [AppColors.primary, Color(0xFF16A34A)],
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
        ),
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: AppColors.primary.withOpacity(0.3),
            blurRadius: 20,
            offset: const Offset(0, 10),
          ),
        ],
      ),
      child: accountAsync.when(
        data: (account) {
          if (account == null) {
            return const Center(
              child: Text('暂无数据', style: TextStyle(color: Colors.white70)),
            );
          }
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Text(
                '我的积分股',
                style: TextStyle(color: Colors.white70, fontSize: 14),
              ),
              const SizedBox(height: 8),
              Text(
                formatAmount(account.totalBalance),
                style: const TextStyle(
                  color: Colors.white,
                  fontSize: 36,
                  fontWeight: FontWeight.bold,
                ),
              ),
              const SizedBox(height: 20),
              Row(
                children: [
                  Expanded(
                    child: _buildBalanceItem('挖矿账户', account.miningBalance),
                  ),
                  Expanded(
                    child: _buildBalanceItem('交易账户', account.tradingBalance),
                  ),
                  Expanded(
                    child: _buildBalanceItem('冻结', account.frozenBalance),
                  ),
                ],
              ),
            ],
          );
        },
        loading: () => const SizedBox(
          height: 140,
          child: Center(
            child: CircularProgressIndicator(color: Colors.white),
          ),
        ),
        error: (_, __) => const SizedBox(
          height: 140,
          child: Center(
            child: Text('加载失败', style: TextStyle(color: Colors.white70)),
          ),
        ),
      ),
    );
  }

  Widget _buildBalanceItem(String label, String value) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          label,
          style: const TextStyle(color: Colors.white60, fontSize: 12),
        ),
        const SizedBox(height: 4),
        Text(
          formatAmount(value),
          style: const TextStyle(
            color: Colors.white,
            fontSize: 16,
            fontWeight: FontWeight.w500,
          ),
        ),
      ],
    );
  }
}
