import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_tabler_icons/flutter_tabler_icons.dart';
import 'package:hyper_local/config/colors.dart';
import 'package:hyper_local/utils/extensions.dart';
import 'package:hyper_local/utils/widgets/custom_card.dart';
import '../../model/available_orders.dart';
import '../../../../utils/currency_formatter.dart';
import 'package:hyper_local/l10n/app_localizations.dart';
import '../../../../utils/widgets/custom_text.dart';

class StatisticsRow extends StatelessWidget {
  final Orders order;

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

  @override
  Widget build(BuildContext context) {
    final double? distance = order.deliveryRoute?.totalDistance;
    String distanceStr = '0.0km';
    if (distance != null) {
      if (distance > 100) {
        distanceStr = '${(distance / 1000).toStringAsFixed(1)}km';
      } else {
        distanceStr = '${distance.toStringAsFixed(1)}km';
      }
    }

    return CustomCard(
      padding: EdgeInsets.zero,
      width: double.infinity,
      child: IntrinsicHeight(
        child: Row(
          children: [
            // 1. Items Column
            StatisticItem(
              icon: TablerIcons.shopping_bag,
              iconColor: const Color(0xFF1A73E8),
              iconBackgroundColor: const Color(0xFFE8F0FE),
              label: AppLocalizations.of(context)!.items,
              value: '${order.items?.length ?? 0}',
            ),

            // Divider
            Container(width: .5, color: context.isDarkMode ? AppColors.darkBackgroundColor : Colors.grey[200]),

            // 2. Earning Column
            StatisticItem(
              icon: Icons.account_balance_wallet_outlined,
              iconColor: const Color(0xFF137333),
              iconBackgroundColor: const Color(0xFFE6F4EA),
              label: AppLocalizations.of(context)!.earnings,
              value: CurrencyFormatter.formatAmount(context, order.earnings?.total ?? '0'),
            ),

            // // Divider
            // Container(width: .5, color: context.isDarkMode ? AppColors.darkBackgroundColor : Colors.grey[200]),
            //
            // // 3. Distance Column
            // StatisticItem(
            //   icon: Icons.location_on_outlined,
            //   iconColor: const Color(0xFF9333EA),
            //   iconBackgroundColor: const Color(0xFFF3E8FF),
            //   label: AppLocalizations.of(context)!.distance,
            //   value: distanceStr,
            // ),
          ],
        ),
      ),
    );
  }
}

class StatisticItem extends StatelessWidget {
  final IconData icon;
  final Color iconColor;
  final Color iconBackgroundColor;
  final String label;
  final String value;

  const StatisticItem({
    super.key,
    required this.icon,
    required this.iconColor,
    required this.iconBackgroundColor,
    required this.label,
    required this.value,
  });

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Padding(
        padding: const EdgeInsets.symmetric(vertical: 20),
        child: Column(
          spacing: 4,
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              padding: EdgeInsets.all(8.r),
              decoration: BoxDecoration(color: iconBackgroundColor, shape: BoxShape.circle),
              child: Icon(icon, color: iconColor, size: 20.sp),
            ),
            const SizedBox(height: 1),

            CustomText(text: label, fontSize: 11, fontWeight: FontWeight.w500, color: Colors.grey[500]),
            const SizedBox(height: 2),
            CustomText(text: value, fontWeight: FontWeight.bold, color: Theme.of(context).colorScheme.onSurface),
          ],
        ),
      ),
    );
  }
}
