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 PriceCard extends ConsumerWidget {
  const PriceCard({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final globalState = ref.watch(globalStateProvider);

    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: globalState.when(
          data: (state) {
            if (state == null) {
              return const Center(child: Text('暂无数据'));
            }
            final isPriceUp = state.isPriceUp;
            return Row(
              children: [
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const Text(
                        '当前价格',
                        style: TextStyle(
                          color: AppColors.textSecondary,
                          fontSize: 12,
                        ),
                      ),
                      const SizedBox(height: 4),
                      Text(
                        formatPrice(state.currentPrice),
                        style: TextStyle(
                          fontSize: 24,
                          fontWeight: FontWeight.bold,
                          color: isPriceUp ? AppColors.up : AppColors.down,
                        ),
                      ),
                      const Text(
                        '绿积分/股',
                        style: TextStyle(
                          color: AppColors.textMuted,
                          fontSize: 12,
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
                  decoration: BoxDecoration(
                    color: (isPriceUp ? AppColors.up : AppColors.down).withOpacity(0.1),
                    borderRadius: BorderRadius.circular(20),
                  ),
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Icon(
                        isPriceUp ? Icons.arrow_upward : Icons.arrow_downward,
                        size: 16,
                        color: isPriceUp ? AppColors.up : AppColors.down,
                      ),
                      const SizedBox(width: 4),
                      Text(
                        formatPercent(state.priceChange24h),
                        style: TextStyle(
                          color: isPriceUp ? AppColors.up : AppColors.down,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            );
          },
          loading: () => const SizedBox(
            height: 80,
            child: Center(child: CircularProgressIndicator()),
          ),
          error: (_, __) => const SizedBox(
            height: 80,
            child: Center(child: Text('加载失败')),
          ),
        ),
      ),
    );
  }
}
