import 'package:siro_rider/constant/currency.dart';
import 'package:siro_rider/constant/colors.dart';
import 'package:siro_rider/constant/links.dart';
import 'package:siro_rider/constant/style.dart';
import 'package:siro_rider/controller/home/map/ride_lifecycle_controller.dart';
import 'package:siro_rider/controller/home/map/ride_state.dart';
import 'package:siro_rider/controller/voice_call_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

import '../../../constant/box_name.dart';
import '../../../controller/firebase/notification_service.dart';
import '../../../controller/functions/launch.dart';
import '../../../controller/functions/crud.dart';
import '../../../main.dart';

class ApplyOrderWidget extends StatelessWidget {
  const ApplyOrderWidget({super.key});

  @override
  Widget build(BuildContext context) {
    Color parseColor(String colorHex) {
      if (colorHex.isEmpty) return Colors.grey;
      try {
        String processedHex = colorHex.replaceFirst('#', '').trim();
        if (processedHex.length == 6) processedHex = 'FF$processedHex';
        return Color(int.parse('0x$processedHex'));
      } catch (e) {
        return Colors.grey;
      }
    }

    return Obx(() {
      final controller = Get.find<RideLifecycleController>();

      final bool isVisible =
          controller.currentRideState.value == RideState.driverApplied ||
              controller.currentRideState.value == RideState.driverArrived;

      return AnimatedPositioned(
        duration: const Duration(milliseconds: 500),
        curve: Curves.elasticOut,
        // تغيير: جعلنا الإخفاء للأسفل أقل حدة ليكون التحريك أسرع
        bottom: isVisible ? 0 : -400,
        left: 0,
        right: 0,
        child: Container(
          decoration: BoxDecoration(
            color: Theme.of(context).cardColor,
            borderRadius: const BorderRadius.vertical(top: Radius.circular(25)),
            boxShadow: [
              BoxShadow(
                blurRadius: 20,
                spreadRadius: 1,
                color: Colors.black.withOpacity(0.1),
                offset: const Offset(0, -3),
              )
            ],
          ),
          // تغيير: تقليل الحواف الخارجية بشكل كبير
          padding: const EdgeInsets.fromLTRB(16, 8, 16, 16),
          child: GetBuilder<RideLifecycleController>(
            builder: (c) {
              return Column(
                mainAxisSize:
                    MainAxisSize.min, // مهم جداً: يأخذ أقل مساحة ممكنة
                children: [
                  // مقبض صغير
                  Container(
                    width: 40,
                    height: 4,
                    decoration: BoxDecoration(
                      color: Colors.grey.withOpacity(0.3),
                      borderRadius: BorderRadius.circular(10),
                    ),
                  ),
                  const SizedBox(height: 10), // تقليل المسافة

                  // 1. [تغيير جوهري] دمج السعر مع الحالة في صف واحد لتوفير المساحة
                  _buildCompactHeaderRow(context, c),

                  const SizedBox(height: 10), // مسافة مضغوطة

                  // 2. كرت المعلومات المضغوط
                  _buildCompactInfoCard(context, c, parseColor),

                  const SizedBox(height: 10), // مسافة مضغوطة

                  // 3. أزرار الاتصال (Slim)
                  _buildCompactButtonsRow(context, c),

                  const SizedBox(height: 10), // مسافة مضغوطة

                  // 4. شريط الوقت
                  c.currentRideState.value == RideState.driverArrived
                      ? const DriverArrivePassengerAndWaitMinute()
                      : const TimeDriverToPassenger(),
                ],
              );
            },
          ),
        ),
      );
    });
  }

  // ---------------------------------------------------------------------------
  // [NEW] 1. صف الرأس المضغوط (يحتوي الحالة + الإحصائيات + السعر)
  // ---------------------------------------------------------------------------
  Widget _buildCompactHeaderRow(
      BuildContext context, RideLifecycleController controller) {
    // تنسيق السعر
    final formatter = NumberFormat("#,###");
    String formattedPrice = formatter.format(controller.totalPassenger);

    // حساب الدقائق من الوقت المتبقي الحي، وليس ETA الأصلي فقط.
    final int secondsToPassenger =
        controller.remainingTimeToPassengerFromDriverAfterApplied > 0
            ? controller.remainingTimeToPassengerFromDriverAfterApplied
            : controller.timeToPassengerFromDriverAfterApplied;
    final int minutes =
        secondsToPassenger <= 0 ? 0 : (secondsToPassenger / 60).ceil();

    // تنسيق المسافة
    String distanceDisplay = "";
    try {
      double distMeters = double.parse(controller.distanceByPassenger);
      if (distMeters >= 1000) {
        distanceDisplay = "${(distMeters / 1000).toStringAsFixed(1)} km";
      } else {
        distanceDisplay = "${distMeters.toInt()} m";
      }
    } catch (e) {
      distanceDisplay = controller.distanceByPassenger;
    }

    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        // القسم الأيسر: الحالة + Chips
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                'Driver is on the way'.tr,
                style: AppStyle.subtitle.copyWith(
                  color: Colors.grey[600],
                  fontWeight: FontWeight.w600,
                  fontSize: 13, // تصغير الخط
                ),
              ),
              const SizedBox(height: 6),
              Row(
                children: [
                  _buildMiniStatChip(
                    icon: Icons.access_time_filled_rounded,
                    text: minutes > 0 ? "$minutes ${'min'.tr}" : "--",
                    color: AppColor.primaryColor,
                    bgColor: AppColor.primaryColor.withOpacity(0.1),
                  ),
                  const SizedBox(width: 8),
                  _buildMiniStatChip(
                    icon: Icons.near_me_rounded,
                    text: distanceDisplay,
                    color: Colors.orange[800]!,
                    bgColor: Colors.orange.withOpacity(0.1),
                  ),
                ],
              ),
            ],
          ),
        ),

        // القسم الأيمن: السعر (كبير وواضح في الزاوية)
        Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Text(
              formattedPrice,
              style: AppStyle.title.copyWith(
                fontSize: 24, // تصغير من 32 إلى 24
                fontWeight: FontWeight.w900,
                color: AppColor.primaryColor,
                height: 1.0,
              ),
            ),
            Text(
              CurrencyHelper.currency,
              style: TextStyle(
                fontSize: 12,
                fontWeight: FontWeight.bold,
                color: Colors.grey[600],
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget _buildMiniStatChip({
    required IconData icon,
    required String text,
    required Color color,
    required Color bgColor,
  }) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: bgColor,
        borderRadius: BorderRadius.circular(12),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(icon, size: 12, color: color), // تصغير الأيقونة
          const SizedBox(width: 4),
          Text(
            text,
            style: TextStyle(
              color: color,
              fontWeight: FontWeight.bold,
              fontSize: 12, // تصغير الخط
            ),
          ),
        ],
      ),
    );
  }

  // ---------------------------------------------------------------------------
  // [MODIFIED] 2. كرت المعلومات المضغوط جداً
  // ---------------------------------------------------------------------------
  Widget _buildCompactInfoCard(BuildContext context,
      RideLifecycleController controller, Color Function(String) parseColor) {
    return Container(
      // تقليل الحواف الداخلية للكرت
      padding: const EdgeInsets.all(10),
      decoration: BoxDecoration(
        color: Theme.of(context).scaffoldBackgroundColor,
        borderRadius: BorderRadius.circular(16),
        border: Border.all(color: Colors.grey.withOpacity(0.1)),
      ),
      child: Column(
        children: [
          // الصف العلوي: سائق + سيارة
          Row(
            children: [
              // صورة السائق (أصغر)
              GestureDetector(
                onTap: () => _showDriverAvatarDialog(context, controller),
                child: Container(
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    border: Border.all(
                        color: AppColor.primaryColor.withOpacity(0.2),
                        width: 2),
                  ),
                  child: CircleAvatar(
                    radius: 22, // تصغير من 28 إلى 22
                    backgroundColor: Colors.grey[200],
                    backgroundImage: NetworkImage(
                        '${AppLink.server}/portrate_captain_image/${controller.driverId}.jpg'),
                    onBackgroundImageError: (_, __) =>
                        const Icon(Icons.person, color: Colors.grey, size: 20),
                  ),
                ),
              ),

              const SizedBox(width: 10),

              // معلومات نصية
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      controller.driverName,
                      style: const TextStyle(
                        fontSize: 15, // تصغير الخط
                        fontWeight: FontWeight.bold,
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                    const SizedBox(height: 2),
                    Row(
                      children: [
                        const Icon(Icons.star_rounded,
                            color: Colors.amber, size: 14),
                        Text(
                          " ${controller.driverRate}  •  ${controller.model}${controller.carColor.isNotEmpty ? '  •  ${controller.carColor.tr}' : ''}",
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.grey[700],
                            fontWeight: FontWeight.w500,
                          ),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ],
                    ),
                    const SizedBox(height: 5),
                    Wrap(
                      spacing: 6,
                      runSpacing: 4,
                      children: [
                        _buildDriverBadge(
                          icon: Icons.verified_rounded,
                          text: controller.driverTier.tr,
                          color: AppColor.primaryColor,
                        ),
                        if (controller.driverCompletedRides != '0')
                          _buildDriverBadge(
                            icon: Icons.route_rounded,
                            text:
                                '${controller.driverCompletedRides} ${'rides'.tr}',
                            color: Colors.teal,
                          ),
                        if (controller.driverRatingCount != '0')
                          _buildDriverBadge(
                            icon: Icons.reviews_rounded,
                            text:
                                '${controller.driverRatingCount} ${'reviews'.tr}',
                            color: Colors.amber.shade800,
                          ),
                      ],
                    ),
                  ],
                ),
              ),

              // أيقونة السيارة (أصغر)
              _buildMicroCarIcon(controller, parseColor),
            ],
          ),

          const SizedBox(height: 8),

          // لوحة السيارة (شريط نحيف جداً)
          _buildSlimLicensePlate(controller.licensePlate),
        ],
      ),
    );
  }

  Widget _buildMicroCarIcon(
      RideLifecycleController controller, Color Function(String) parseColor) {
    Color carColor = parseColor(controller.colorHex);
    final String vehicleText =
        '${controller.model} ${controller.make}'.toLowerCase();
    final bool isBike = vehicleText.contains('scooter') ||
        vehicleText.contains('bike') ||
        vehicleText.contains('دراجة');
    return AnimatedCarIcon(
      child: Container(
        height: 40, // تصغير من 50
        width: 40,
        decoration: BoxDecoration(
          color: carColor.withOpacity(0.1),
          borderRadius: BorderRadius.circular(8),
        ),
        padding: const EdgeInsets.all(4),
        child: ColorFiltered(
          colorFilter: ColorFilter.mode(carColor, BlendMode.srcIn),
          child: Image.asset(
            isBike ||
                    box.read(BoxName.carType) == 'Scooter' ||
                    box.read(BoxName.carType) == 'Pink Bike'
                ? 'assets/images/moto.png'
                : 'assets/images/car3.png',
            fit: BoxFit.contain,
          ),
        ),
      ),
    );
  }

  Widget _buildDriverBadge({
    required IconData icon,
    required String text,
    required Color color,
  }) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 7, vertical: 3),
      decoration: BoxDecoration(
        color: color.withOpacity(0.1),
        borderRadius: BorderRadius.circular(10),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(icon, size: 11, color: color),
          const SizedBox(width: 4),
          Text(
            text,
            style: TextStyle(
              color: color,
              fontSize: 10.5,
              fontWeight: FontWeight.w800,
            ),
          ),
        ],
      ),
    );
  }

  void _showDriverAvatarDialog(
      BuildContext context, RideLifecycleController controller) {
    final imageUrl =
        '${AppLink.server}/portrate_captain_image/${controller.driverId}.jpg';
    Get.dialog(
      Dialog(
        insetPadding: const EdgeInsets.symmetric(horizontal: 38),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18)),
        child: Padding(
          padding: const EdgeInsets.fromLTRB(18, 20, 18, 18),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              CircleAvatar(
                radius: 58,
                backgroundColor: Colors.grey[200],
                backgroundImage: NetworkImage(imageUrl),
                onBackgroundImageError: (_, __) {},
              ),
              const SizedBox(height: 14),
              Text(
                controller.driverName,
                textAlign: TextAlign.center,
                style: AppStyle.title.copyWith(
                  fontSize: 18,
                  fontWeight: FontWeight.w900,
                ),
              ),
              const SizedBox(height: 6),
              Text(
                '${controller.driverTier.tr} • ${controller.driverRate}',
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: Colors.grey[700],
                  fontSize: 13,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ],
          ),
        ),
      ),
      barrierDismissible: true,
    );
  }

  Widget _buildSlimLicensePlate(String plateNumber) {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.symmetric(vertical: 2, horizontal: 8),
      decoration: BoxDecoration(
        color: Get.isDarkMode ? Colors.grey[850] : const Color(0xFFF5F5F5),
        borderRadius: BorderRadius.circular(6),
        border: Border.all(
            color:
                Get.isDarkMode ? Colors.white10 : Colors.grey.withOpacity(0.3)),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            plateNumber,
            style: TextStyle(
              fontFamily: 'RobotoMono',
              fontSize: 18,
              fontWeight: FontWeight.w900,
              color: AppColor.writeColor,
              letterSpacing: 1.5,
            ),
          ),
          Text("SYR",
              style: TextStyle(
                  fontSize: 10,
                  fontWeight: FontWeight.bold,
                  color: AppColor.writeColor.withOpacity(0.6))),
        ],
      ),
    );
  }

  // ---------------------------------------------------------------------------
  // [MODIFIED] 3. أزرار الاتصال (Slim Buttons)
  // ---------------------------------------------------------------------------
  Widget _buildCompactButtonsRow(
      BuildContext context, RideLifecycleController controller) {
    return SizedBox(
      height: 40, // تحديد ارتفاع ثابت وصغير للأزرار
      child: Row(
        children: [
          Expanded(
            child: _buildSlimButton(
              label: 'Message'.tr, // اختصار الكلمة
              icon: Icons.chat_bubble_outline_rounded,
              color: AppColor.blueColor,
              bgColor: AppColor.blueColor.withOpacity(0.08),
              onTap: () => _showContactOptionsDialog(context, controller),
            ),
          ),
          const SizedBox(width: 10), // تقليل المسافة
          Expanded(
            child: _buildSlimButton(
              label: 'Call'.tr, // اختصار الكلمة
              icon: Icons.phone_rounded,
              color: Colors.white,
              bgColor: AppColor.greenColor,
              onTap: () {
                HapticFeedback.heavyImpact();
                _showCallSelectionDialog(context, controller);
              },
              isPrimary: true,
            ),
          ),
        ],
      ),
    );
  }

  void _showCallSelectionDialog(
      BuildContext context, RideLifecycleController controller) {
    Get.dialog(
      Dialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
        child: Padding(
          padding: const EdgeInsets.all(20),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                'Call Options'.tr,
                style: AppStyle.title
                    .copyWith(fontSize: 18, fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 10),
              Text(
                'Choose how you want to call the driver'.tr,
                style: const TextStyle(color: Colors.grey, fontSize: 14),
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 20),
              ListTile(
                leading: CircleAvatar(
                  backgroundColor: AppColor.greenColor.withOpacity(0.1),
                  child: Icon(Icons.phone_android_rounded,
                      color: AppColor.greenColor),
                ),
                title: Text('Standard Call'.tr,
                    style: const TextStyle(fontWeight: FontWeight.bold)),
                subtitle: Text('Uses cellular network'.tr,
                    style: const TextStyle(fontSize: 12)),
                onTap: () {
                  Get.back();
                  makePhoneCall(controller.driverPhone);
                },
              ),
              const Divider(),
              ListTile(
                leading: CircleAvatar(
                  backgroundColor: AppColor.primaryColor.withOpacity(0.1),
                  child: Icon(Icons.wifi_calling_3_rounded,
                      color: AppColor.primaryColor),
                ),
                title: Text('Free Call'.tr,
                    style: const TextStyle(fontWeight: FontWeight.bold)),
                subtitle: Text('Voice call over internet'.tr,
                    style: const TextStyle(fontSize: 12)),
                onTap: () {
                  Get.back();
                  final voiceCtrl = Get.find<VoiceCallController>();
                  final passengerId = box.read(BoxName.passengerID).toString();
                  voiceCtrl.startCall(
                    rideIdVal: controller.rideId,
                    driverId: controller.driverId,
                    passengerId: passengerId,
                    remoteNameVal: controller.driverName,
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildSlimButton({
    required String label,
    required IconData icon,
    required Color color,
    required Color bgColor,
    required VoidCallback onTap,
    bool isPrimary = false,
  }) {
    return ElevatedButton(
      onPressed: onTap,
      style: ElevatedButton.styleFrom(
        backgroundColor: bgColor,
        foregroundColor: color,
        elevation: isPrimary ? 2 : 0,
        padding: EdgeInsets.zero, // إزالة الحواشي الداخلية
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(icon, size: 18, color: color), // تصغير الأيقونة
          const SizedBox(width: 6),
          Text(
            label,
            style: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 14, // تصغير الخط
              color: color,
            ),
          ),
        ],
      ),
    );
  }

  // --- النوافذ المنبثقة للرسائل (نفس الكود السابق مع تحسين بسيط) ---
  void _showContactOptionsDialog(
      BuildContext context, RideLifecycleController controller) {
    Get.bottomSheet(
      Container(
        padding: const EdgeInsets.all(20),
        decoration: BoxDecoration(
          color: Theme.of(context).cardColor,
          borderRadius: const BorderRadius.vertical(top: Radius.circular(20)),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('Quick Message'.tr,
                style: AppStyle.title.copyWith(fontSize: 16)),
            const SizedBox(height: 15),
            ..._buildPredefinedMessages(controller),
            const Divider(height: 20),
            _buildCustomMessageInput(controller, context),
            SizedBox(height: MediaQuery.of(context).viewInsets.bottom),
          ],
        ),
      ),
      isScrollControlled: true,
    );
  }

  List<Widget> _buildPredefinedMessages(RideLifecycleController controller) {
    const messages = [
      'Hello, I\'m at the agreed-upon location',
      'I\'m waiting for you',
      "How much longer will you be?",
    ];

    return messages
        .map((message) => Padding(
              padding: const EdgeInsets.only(bottom: 8.0),
              child: InkWell(
                onTap: () {
                  _sendMessage(controller, message.tr);
                  Get.back();
                },
                child: Container(
                  padding:
                      const EdgeInsets.symmetric(vertical: 10, horizontal: 12),
                  decoration: BoxDecoration(
                    color: Colors.grey.withOpacity(0.08),
                    borderRadius: BorderRadius.circular(8),
                    border: Border.all(color: Colors.grey.withOpacity(0.1)),
                  ),
                  child: Row(
                    children: [
                      Icon(Icons.chat_bubble_outline,
                          size: 16, color: AppColor.primaryColor),
                      const SizedBox(width: 10),
                      Expanded(
                          child: Text(message.tr,
                              style: AppStyle.subtitle.copyWith(fontSize: 13))),
                    ],
                  ),
                ),
              ),
            ))
        .toList();
  }

  Widget _buildCustomMessageInput(
      RideLifecycleController controller, BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: Container(
            height: 40,
            padding: const EdgeInsets.symmetric(horizontal: 12),
            decoration: BoxDecoration(
              color: Colors.grey.withOpacity(0.08),
              borderRadius: BorderRadius.circular(20),
            ),
            child: Form(
              key: controller.messagesFormKey,
              child: TextFormField(
                controller: controller.messageToDriver,
                decoration: InputDecoration(
                  hintText: 'Type your message...'.tr,
                  hintStyle: TextStyle(color: Colors.grey[500], fontSize: 13),
                  border: InputBorder.none,
                  contentPadding: const EdgeInsets.only(bottom: 10),
                ),
              ),
            ),
          ),
        ),
        const SizedBox(width: 8),
        InkWell(
          onTap: () {
            if (controller.messagesFormKey.currentState!.validate()) {
              _sendMessage(controller, controller.messageToDriver.text);
              controller.messageToDriver.clear();
              Get.back();
            }
          },
          child: CircleAvatar(
            backgroundColor: AppColor.primaryColor,
            radius: 20,
            child:
                const Icon(Icons.send_rounded, color: Colors.white, size: 16),
          ),
        ),
      ],
    );
  }

  void _sendMessage(RideLifecycleController controller, String text) async {
    try {
      await CRUD().post(
        link: AppLink.sendChatMessage,
        payload: {
          'ride_id': controller.rideId.toString(),
          'sender_id': box.read(BoxName.passengerID).toString(),
          'receiver_id': controller.driverId.toString(),
          'sender_type': 'passenger',
          'message_content': text.tr,
        },
      );
    } catch (e) {
      // Ignore or log error
    }

    NotificationService.sendNotification(
      category: 'MSG_FROM_PASSENGER',
      target: controller.driverToken.toString(),
      title: text.tr,
      body: text.tr,
      isTopic: false,
      tone: 'ding',
      driverList: [],
    );
  }
}

// -----------------------------------------------------------------------------
// مؤشرات الانتظار والوقت (مضغوطة)
// -----------------------------------------------------------------------------

class DriverArrivePassengerAndWaitMinute extends StatelessWidget {
  const DriverArrivePassengerAndWaitMinute({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<RideLifecycleController>(builder: (controller) {
      return Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text('Waiting...'.tr,
                  style: const TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 12,
                      color: Colors.orange)),
              Text(
                controller.stringRemainingTimeDriverWaitPassenger5Minute,
                style: const TextStyle(
                    fontWeight: FontWeight.bold,
                    color: Colors.orange,
                    fontSize: 12),
              ),
            ],
          ),
          const SizedBox(height: 4),
          ClipRRect(
            borderRadius: BorderRadius.circular(2),
            child: LinearProgressIndicator(
              backgroundColor: Colors.orange.withOpacity(0.2),
              color: Colors.orange,
              minHeight: 4,
              value:
                  controller.progressTimerDriverWaitPassenger5Minute.toDouble(),
            ),
          ),
        ],
      );
    });
  }
}

class TimeDriverToPassenger extends StatelessWidget {
  const TimeDriverToPassenger({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<RideLifecycleController>(builder: (controller) {
      if (controller.timeToPassengerFromDriverAfterApplied <= 0) {
        return const SizedBox();
      }
      return Column(
        children: [
          // شريط التقدم فقط لأن الوقت والمسافة موجودان بالأعلى
          ClipRRect(
            borderRadius: BorderRadius.circular(2),
            child: LinearProgressIndicator(
              backgroundColor: AppColor.primaryColor.withOpacity(0.1),
              color: AppColor.primaryColor,
              minHeight: 4,
              value: controller.progressTimerToPassengerFromDriverAfterApplied
                  .toDouble()
                  .clamp(0.0, 1.0),
            ),
          ),
        ],
      );
    });
  }
}

class AnimatedCarIcon extends StatefulWidget {
  final Widget child;
  const AnimatedCarIcon({Key? key, required this.child}) : super(key: key);

  @override
  State<AnimatedCarIcon> createState() => _AnimatedCarIconState();
}

class _AnimatedCarIconState extends State<AnimatedCarIcon>
    with SingleTickerProviderStateMixin {
  late final AnimationController _controller;
  late final Animation<Offset> _animation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 2),
    )..repeat(reverse: true);

    _animation = Tween<Offset>(
      begin: const Offset(-0.06, 0.0),
      end: const Offset(0.06, 0.0),
    ).animate(CurvedAnimation(
      parent: _controller,
      curve: Curves.easeInOut,
    ));
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SlideTransition(
      position: _animation,
      child: widget.child,
    );
  }
}
