import 'package:SEFER/constant/colors.dart';
import 'package:SEFER/constant/style.dart';
import 'package:SEFER/controller/firebase/firbase_messge.dart';
import 'package:SEFER/controller/home/map_passenger_controller.dart';
import 'package:SEFER/main.dart';
import 'package:SEFER/views/home/Captin/home_captain/widget/call_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

import '../../../constant/box_name.dart';

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

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MapPassengerController>(builder: (controller) {
      if (controller.statusRide == 'Apply' &&
          controller.isSearchingWindow == false) {
        // double _height = Get.height * .2;
        return Positioned(
          bottom: 0,
          left: 0,
          right: 0,
          child: Container(
            decoration: AppStyle.boxDecoration1,
            height: Get.height * .35,
            child: ListView(
              children: [
                InkWell(
                  onTap: () {
                    if (box.read(BoxName.carType) == 'Speed' ||
                        box.read(BoxName.carType) == 'Delivery') {
                      Get.snackbar(
                        'This price is ${controller.totalPassenger.toStringAsFixed(2)}'
                            .tr,
                        'This ride type does not allow changes to the destination or additional stops'
                            .tr,
                        snackPosition: SnackPosition.BOTTOM,
                        duration: const Duration(seconds: 2),
                        backgroundColor: AppColor.yellowColor,
                      );
                    } else {
                      Get.snackbar(
                        'This price may be changed'.tr,
                        'This ride type allows changes, but the price may increase'
                            .tr,
                        snackPosition: SnackPosition.BOTTOM,
                        duration: const Duration(seconds: 2),
                        backgroundColor: AppColor.yellowColor,
                      );
                    }
                  },
                  child: Text.rich(
                    TextSpan(
                      children: [
                        TextSpan(
                          text: '${'The driver accept your order for'.tr} ',
                          style: AppStyle.title,
                        ),
                        TextSpan(
                          text: controller.totalPassenger.toStringAsFixed(2),
                          style: AppStyle.title.copyWith(
                            fontWeight: FontWeight.bold,
                            // fontSize: 22,
                            color: AppColor.redColor,
                          ),
                        ),
                        TextSpan(
                          text: ' ${'LE'.tr}',
                          style: AppStyle.title,
                        ),
                      ],
                    ),
                    textAlign: TextAlign.center,
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    const SizedBox(
                      width: 10,
                    ),
                    Container(
                      height: Get.height * .3,
                      width: Get.width * .9,
                      decoration: AppStyle.boxDecoration1,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Text(
                                // 'Comfort',
                                box.read(BoxName.carType
                                    .toString()), //car type fro box after selected
                                style: AppStyle.title,
                              ),
                              const SizedBox(
                                width: 10,
                              ),
                            ],
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Image.asset(
                                'assets/images/blob.png',
                                width: 100,
                              ),
                              Column(
                                children: [
                                  Text(
                                    // 'Toyota Camry',
                                    controller.model.toString(),
                                    style: AppStyle.title,
                                  ),
                                  Text(
                                    // 'ر ل 2323',
                                    controller.licensePlate.toString(),
                                    style: AppStyle.title,
                                  ),
                                ],
                              ),
                              Text(
                                // 'Black',
                                controller.carColor,
                                style: AppStyle.title,
                              ),
                              const SizedBox(
                                width: 10,
                              ),
                            ],
                          ),
                          Padding(
                            padding: const EdgeInsets.symmetric(horizontal: 10),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                CircleAvatar(
                                  radius: 30,
                                  backgroundImage: NetworkImage(
                                      //   '',
                                      // ),
                                      'https://ride.mobile-app.store/portrate_captain_image/${controller.driverId}.jpg'),
                                ),
                                Column(
                                  children: [
                                    Text(
                                      // 'fadi ahmad',
                                      controller.firstName,
                                      style: AppStyle.title,
                                    ),
                                    Text(
                                      // '⭐ 4.8',
                                      '⭐ ${controller.driverRate}',
                                      style: AppStyle.title,
                                    ),
                                  ],
                                ),
                                IconButton(
                                  onPressed: () async {
                                    Get.defaultDialog(
                                        title: 'Select one message'.tr,
                                        titleStyle: AppStyle.title,
                                        content: Column(
                                          children: [
                                            InkWell(
                                              onTap: () {
                                                FirebaseMessagesController()
                                                    .sendNotificationToAnyWithoutData(
                                                        'message From passenger',
                                                        'Hello, I\'m at the agreed-upon location'
                                                            .tr,
                                                        controller.driverToken);
                                                Get.back();
                                              },
                                              child: Container(
                                                decoration:
                                                    AppStyle.boxDecoration1,
                                                child: Padding(
                                                  padding:
                                                      const EdgeInsets.all(10),
                                                  child: Text(
                                                    'Hello, I\'m at the agreed-upon location'
                                                        .tr,
                                                    style: AppStyle.title,
                                                  ),
                                                ),
                                              ),
                                            ),
                                            InkWell(
                                              onTap: () {
                                                FirebaseMessagesController()
                                                    .sendNotificationToAnyWithoutData(
                                                        'message From passenger',
                                                        'My location is correct. You can search for me using the navigation app'
                                                            .tr,
                                                        controller.driverToken);
                                                Get.back();
                                              },
                                              child: Container(
                                                decoration:
                                                    AppStyle.boxDecoration1,
                                                child: Padding(
                                                  padding:
                                                      const EdgeInsets.all(10),
                                                  child: Text(
                                                    'My location is correct. You can search for me using the navigation app'
                                                        .tr,
                                                    style: AppStyle.title,
                                                  ),
                                                ),
                                              ),
                                            ),
                                            InkWell(
                                              onTap: () {
                                                FirebaseMessagesController()
                                                    .sendNotificationToAnyWithoutData(
                                                        'message From passenger',
                                                        'I\'m waiting for you'
                                                            .tr,
                                                        controller.driverToken);
                                                Get.back();
                                              },
                                              child: Container(
                                                decoration:
                                                    AppStyle.boxDecoration1,
                                                child: Padding(
                                                  padding:
                                                      const EdgeInsets.all(10),
                                                  child: Text(
                                                    'I\'m waiting for you'.tr,
                                                    style: AppStyle.title,
                                                  ),
                                                ),
                                              ),
                                            ),
                                          ],
                                        ));
                                  },
                                  icon: const Icon(
                                    Icons.message,
                                    color: AppColor.blueColor,
                                    size: 35,
                                  ),
                                ),
                                IconButton(
                                  onPressed: () {
                                    HapticFeedback.heavyImpact();
                                    Get.to(() => const CallPage());
                                    // Get.to(() => PassengerCallPage());
                                  },
                                  icon: const Icon(
                                    Icons.call,
                                    color: AppColor.greenColor,
                                    size: 35,
                                  ),
                                ),
                              ],
                            ),
                          ),
                          controller.isDriverArrivePassenger
                              ? const DriverArrivePassengerAndWaitMinute()
                              : const TimeDriverToPassenger()
                        ],
                      ),
                    ),
                    const SizedBox(
                      width: 10,
                    ),
                  ],
                )
              ],
            ),
          ),
        );
      } else {
        return const SizedBox();
      }
    });
  }
}

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

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MapPassengerController>(builder: (controller) {
      return Stack(
        children: [
          LinearProgressIndicator(
            backgroundColor: AppColor.accentColor,
            color: controller.remainingTimeDriverWaitPassenger5Minute < 60
                ? AppColor.redColor
                : AppColor.greenColor,
            minHeight: 25,
            borderRadius: BorderRadius.circular(15),
            value:
                controller.progressTimerDriverWaitPassenger5Minute.toDouble(),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                'The driver waiting you in picked location .'.tr,
                style: AppStyle.subtitle,
                textAlign: TextAlign.center,
              ),
              const SizedBox(
                width: 20,
              ),
              Text(
                controller.stringRemainingTimeDriverWaitPassenger5Minute,
                style: AppStyle.title,
              ),
            ],
          )
        ],
      );
    });
  }
}

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

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MapPassengerController>(builder: (controller) {
      return controller.isDriverInPassengerWay == false ||
              controller.timeToPassengerFromDriverAfterApplied > 0
          ? Container(
              decoration: AppStyle.boxDecoration1,
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 1),
                child: Stack(
                  children: [
                    Container(
                      decoration: AppStyle.boxDecoration1,
                      width: Get.width * .7,
                      height: 35,
                      // color: AppColor.yellowColor,
                    ),
                    Stack(
                      children: [
                        LinearProgressIndicator(
                          backgroundColor: AppColor.accentColor,
                          color: controller
                                      .remainingTimeToPassengerFromDriverAfterApplied <
                                  60
                              ? AppColor.redColor
                              : AppColor.greenColor,
                          minHeight: 25,
                          borderRadius: BorderRadius.circular(15),
                          value: () {
                            // Ensure valid value between 0.0 and 1.0
                            double progress = controller
                                .progressTimerToPassengerFromDriverAfterApplied
                                .toDouble();
                            if (progress.isNaN || progress.isInfinite) {
                              // Handle invalid progress (e.g., set to 0.0)
                              return 0.0;
                            } else {
                              return progress.clamp(
                                  0.0, 1.0); // Clamp to valid range
                            }
                          }(),
                        ),
                        Center(
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text(
                                'The driver on your way'.tr,
                                textAlign: TextAlign.center,
                              ),
                              const SizedBox(
                                width: 20,
                              ),
                              Text(
                                controller.stringRemainingTimeToPassenger,
                                style: AppStyle.title,
                              ),
                            ],
                          ),
                        )
                      ],
                    ),
                  ],
                ),
              ),
            )
          : const SizedBox();
    });
  }
}
