import 'package:SEFER/constant/colors.dart';
import 'package:SEFER/constant/style.dart';
import 'package:SEFER/controller/home/map_passenger_controller.dart';
import 'package:SEFER/main.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 = 250;
        return Positioned(
          bottom: 0,
          left: 0,
          right: 0,
          child: Container(
            decoration: AppStyle.boxDecoration1,
            height: _height,
            child: ListView(
              children: [
                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: 200,
                      width: Get.width * .9,
                      decoration: AppStyle.boxDecoration1,
                      child: Column(
                        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: () {},
                                  icon: const Icon(
                                    Icons.message,
                                    color: AppColor.blueColor,
                                    size: 35,
                                  ),
                                ),
                                IconButton(
                                  onPressed: () {
                                    HapticFeedback.heavyImpact();
                                  },
                                  icon: const Icon(
                                    Icons.call,
                                    color: AppColor.greenColor,
                                    size: 35,
                                  ),
                                ),
                              ],
                            ),
                          ),
                          controller.isDriverArrivePassenger
                              ? 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: 30,
            borderRadius: BorderRadius.circular(15),
            value:
                controller.progressTimerDriverWaitPassenger5Minute.toDouble(),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                'The driver waitting you in picked location .'.tr,
                style: AppStyle.title,
                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: controller
                              .progressTimerToPassengerFromDriverAfterApplied
                              .toDouble(),
                        ),
                        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();
    });
  }
}
