import 'package:flutter/material.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:provider/provider.dart';
import '../../../core/theme/app_spacing.dart';
import '../../../providers/asset_provider.dart';
import '../../../data/models/order_models.dart';
import 'fund_order_card.dart';

class FundOrdersList extends StatelessWidget {
  final AssetProvider provider;

  const FundOrdersList({super.key, required this.provider});

  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);
    final orders = provider.fundOrders;

    if (orders.isEmpty) {
      return _EmptyState(
        icon: LucideIcons.receipt,
        message: '暂无充提记录',
      );
    }

    return RefreshIndicator(
      onRefresh: () => provider.loadFundOrders(),
      color: theme.colorScheme.primary,
      child: ListView.separated(
        physics: const AlwaysScrollableScrollPhysics(),
        padding: AppSpacing.pagePadding,
        itemCount: orders.length,
        separatorBuilder: (_, __) => Divider(color: theme.colorScheme.border, height: 1),
        itemBuilder: (context, index) {
          final order = orders[index];
          return FundOrderCard(order: order);
        },
      ),
    );
  }
}

/// 空状态组件
class _EmptyState extends StatelessWidget {
  final IconData icon;
  final String message;

  const _EmptyState({required this.icon, required this.message});

  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);

    return Center(
      child: Padding(
        padding: EdgeInsets.all(AppSpacing.xl),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(icon, size: 48, color: theme.colorScheme.mutedForeground),
            SizedBox(height: AppSpacing.sm + AppSpacing.xs),
            Text(message, style: theme.textTheme.muted),
          ],
        ),
      ),
    );
  }
}

/// 充值订单卡片 - 公开类
class FundOrderCard extends StatelessWidget {
  final OrderFund order;

  const FundOrderCard({super.key, required this.order});

  @override
  Widget build(BuildContext context) {
    // 直接使用 _FundOrderCard 的实现
    return _FundOrderCardContent(order: order);
  }
}

/// 订单卡片内容
class _FundOrderCardContent extends StatelessWidget {
  final OrderFund order;

  const _FundOrderCardContent({required this.order});

  Color _getStatusColor(int status, bool isDeposit) {
    if (isDeposit) {
      switch (status) {
        case 1:
          return const Color(0xFFFF9800);
        case 2:
          return const Color(0xFF2196F3);
        case 3:
          return const Color(0xFFafffd1);
        case 4:
          return const Color(0xFFff716c);
        case 5:
          return const Color(0xFFa9abb3);
        default:
          return const Color(0xFFa9abb3);
      }
    } else {
      switch (status) {
        case 1:
          return const Color(0xFFFF9800);
        case 2:
          return const Color(0xFFafffd1);
        case 3:
          return const Color(0xFFff716c);
        case 4:
          return const Color(0xFFa9abb3);
        default:
          return const Color(0xFFa9abb3);
      }
    }
  }

  String _getStatusText(int status, bool isDeposit) {
    if (isDeposit) {
      switch (status) {
        case 1:
          return '待付款';
        case 2:
          return '待确认';
        case 3:
          return '已完成';
        case 4:
          return '已驳回';
        case 5:
          return '已取消';
        default:
          return '未知';
      }
    } else {
      switch (status) {
        case 1:
          return '待审批';
        case 2:
          return '已完成';
        case 3:
          return '已驳回';
        case 4:
          return '已取消';
        default:
          return '未知';
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);
    final isDeposit = order.type == 1;
    final statusColor = _getStatusColor(order.status, isDeposit);

    return ShadCard(
      padding: AppSpacing.cardPadding,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '${isDeposit ? '+' : '-'}${order.amount} USDT',
                style: theme.textTheme.large.copyWith(
                  color: statusColor,
                  fontWeight: FontWeight.bold,
                ),
              ),
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                decoration: BoxDecoration(
                  color: statusColor.withValues(alpha: 0.1),
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Text(
                  _getStatusText(order.status, isDeposit),
                  style: theme.textTheme.small.copyWith(color: statusColor),
                ),
              ),
            ],
          ),
          SizedBox(height: AppSpacing.sm),
          Row(
            children: [
              Text('订单号: ', style: theme.textTheme.muted),
              Text(order.orderNo, style: theme.textTheme.small),
            ],
          ),
          SizedBox(height: AppSpacing.xs),
          Row(
            children: [
              Text('创建时间: ', style: theme.textTheme.muted),
              Text(
                order.createTime?.toString() ?? '无',
                style: theme.textTheme.small,
              ),
            ],
          ),
        ],
      ),
    );
  }
}

