import 'package:SEFER/constant/box_name.dart';
import 'package:SEFER/constant/colors.dart';
import 'package:SEFER/constant/style.dart';
import 'package:SEFER/main.dart';
import 'package:SEFER/views/widgets/elevated_btn.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../constant/info.dart';
import '../../../controller/functions/tts.dart';
import '../../../controller/home/map_passenger_controller.dart';

class CarType {
  final String carType;
  final String carDetail;
  final String image;
  bool isSelected = false; // Track selection state

  CarType(
      {required this.carType, required this.carDetail, required this.image});
}

List<CarType> carTypes = [
  CarType(
      carType: 'Comfort',
      carDetail: 'Comfort choice'.tr,
      image: 'assets/images/blob.png'),
  CarType(
      carType: 'Speed',
      carDetail: 'Better for long trips choice'.tr,
      image: 'assets/images/carspeed.png'),
  CarType(
      carType: 'Delivery',
      carDetail: 'Delivery service'.tr,
      image: 'assets/images/moto.png'),
  CarType(
      carType: 'Mashwari',
      carDetail: 'Mashwari without end point'.tr,
      image: 'assets/images/freeRide.png'),
];

class CarDetailsTypeToChoose extends StatelessWidget {
  CarDetailsTypeToChoose({super.key});
  final textToSpeechController = Get.put(TextToSpeechController());
  @override
  Widget build(BuildContext context) {
    return GetBuilder<MapPassengerController>(
        builder: (mapPassengerController) {
      return mapPassengerController.data.isNotEmpty &&
              mapPassengerController.isBottomSheetShown &&
              mapPassengerController.rideConfirm == false
          ? Positioned(
              bottom: 0,
              left: 5,
              right: 5,
              child: Container(
                decoration: const BoxDecoration(
                  color: Color.fromARGB(255, 255, 255, 255),
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(8),
                    topRight: Radius.circular(8),
                  ),
                ),
                height: Get.height * .4,
                child: Column(
                  children: [
                    SizedBox(
                      height: Get.height * .4,
                      child: ListView.builder(
                        itemCount: carTypes.length,
                        itemBuilder: (context, index) {
                          final carType = carTypes[index];
                          return ListTile(
                            title: Container(
                              decoration: BoxDecoration(
                                  color: AppColor.secondaryColor,
                                  borderRadius: const BorderRadius.all(
                                      Radius.circular(20)),
                                  boxShadow: [
                                    carType.isSelected
                                        ? const BoxShadow(
                                            spreadRadius: 3,
                                            blurStyle: BlurStyle.solid,
                                            color: AppColor.accentColor,
                                            blurRadius: 3,
                                            offset: Offset(1, 3))
                                        : const BoxShadow(),
                                  ]),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Image.asset(
                                    carType.image,
                                    width: 60,
                                    height: 60,
                                  ),
                                  SizedBox(
                                    width: Get.width * .5,
                                    child: Column(
                                      children: [
                                        Text(
                                          carType.carType,
                                          style: AppStyle.title.copyWith(
                                              fontWeight: FontWeight.bold,
                                              fontSize: 20),
                                        ),
                                        Text(
                                          carType.carDetail,
                                          style: AppStyle.subtitle,
                                        ),
                                      ],
                                    ),
                                  ),
                                  Column(
                                    children: [
                                      Text(
                                        carType.carType == 'Comfort'
                                            ? mapPassengerController
                                                .totalPassengerComfort
                                                .toStringAsFixed(2)
                                            : carType.carType == 'Speed'
                                                ? mapPassengerController
                                                    .totalPassenger
                                                    .toStringAsFixed(2)
                                                : carType.carType == 'Delivery'
                                                    ? mapPassengerController
                                                        .totalPassengerMotoDelivery
                                                        .toStringAsFixed(2)
                                                    : '50',
                                        style: AppStyle.title
                                            .copyWith(fontSize: 20),
                                      ),
                                      carType.carType == 'Comfort'
                                          ? Row(
                                              children: [
                                                Container(
                                                    decoration:
                                                        const BoxDecoration(
                                                            color: AppColor
                                                                .greenColor,
                                                            borderRadius:
                                                                BorderRadius
                                                                    .all(Radius
                                                                        .circular(
                                                                            5))),
                                                    child: Text(
                                                      '-12%',
                                                      style: AppStyle.subtitle
                                                          .copyWith(
                                                              color: AppColor
                                                                  .secondaryColor),
                                                    )),
                                                const SizedBox(
                                                  width: 10,
                                                ),
                                                Text(
                                                  mapPassengerController
                                                      .totalPassengerComfortDiscount
                                                      .toStringAsFixed(2),
                                                  style:
                                                      AppStyle.title.copyWith(
                                                    decoration: TextDecoration
                                                        .lineThrough, // Strikethrough line
                                                  ),
                                                )
                                              ],
                                            )
                                          : const SizedBox(
                                              width: 3,
                                            ),
                                    ],
                                  ),
                                  const SizedBox(
                                    width: 4,
                                  )
                                ],
                              ),
                            ),

                            selected: mapPassengerController.selectedIndex ==
                                index, // Set selected based on index
                            selectedColor:
                                Colors.blue, // Color for selected item

                            onTap: () {
                              mapPassengerController.selectCarFromList(index);

                              if (mapPassengerController.selectedIndex != -1) {
                                // Get.snackbar('You should select one'.tr, '',
                                //     backgroundColor: AppColor.greenColor);
                                if (mapPassengerController.selectedIndex == 0) {
                                  box.write(BoxName.carType, 'Comfort');
                                  mapPassengerController.totalPassenger =
                                      mapPassengerController
                                          .totalPassengerComfort;
                                  Get.defaultDialog(
                                      title: 'Comfort',
                                      titleStyle: AppStyle.title,
                                      content: CarDialogue(
                                          textToSpeechController:
                                              textToSpeechController,
                                          image: 'assets/images/blob.png',
                                          text:
                                              'Best choice for comfort car and flexible route and stops point'
                                                  .tr),
                                      confirm: MyElevatedButton(
                                          kolor: AppColor.greenColor,
                                          title: 'Next'.tr,
                                          onPressed: () {
                                            Get.back();
                                            mapPassengerController
                                                .isBottomSheetShown = false;
                                            mapPassengerController.update();
                                            mapPassengerController
                                                .changeCashConfirmPageShown();
                                          }),
                                      cancel: MyElevatedButton(
                                          title: 'Cancel'.tr,
                                          kolor: AppColor.redColor,
                                          onPressed: () {
                                            Get.back();
                                          }));
                                } else if (mapPassengerController
                                        .selectedIndex ==
                                    1) {
                                  box.write(BoxName.carType, 'Speed');
                                  mapPassengerController.totalPassenger =
                                      mapPassengerController.totalPassenger;
                                  Get.defaultDialog(
                                      title: 'Speed',
                                      titleStyle: AppStyle.title,
                                      content: CarDialogue(
                                          textToSpeechController:
                                              textToSpeechController,
                                          image: 'assets/images/carspeed.png',
                                          text:
                                              'This trip goes directly from your starting point to your destination for a fixed price. The driver must follow the planned route'
                                                  .tr),
                                      confirm: MyElevatedButton(
                                          kolor: AppColor.greenColor,
                                          title: 'Next'.tr,
                                          onPressed: () {
                                            Get.back();
                                            mapPassengerController
                                                .isBottomSheetShown = false;
                                            mapPassengerController.update();
                                            mapPassengerController
                                                .changeCashConfirmPageShown();
                                          }),
                                      cancel: MyElevatedButton(
                                          title: 'Cancel'.tr,
                                          kolor: AppColor.redColor,
                                          onPressed: () {
                                            Get.back();
                                          }));
                                } else if (mapPassengerController
                                        .selectedIndex ==
                                    2) {
                                  box.write(BoxName.carType, 'Delivery');
                                  mapPassengerController.totalPassenger =
                                      mapPassengerController
                                          .totalPassengerMotoDelivery;
                                  Get.defaultDialog(
                                      title: 'Delivery',
                                      titleStyle: AppStyle.title,
                                      content: CarDialogue(
                                          textToSpeechController:
                                              textToSpeechController,
                                          image: 'assets/images/moto.png',
                                          text:
                                              'This is for delivery or a motorcycle.'
                                                  .tr),
                                      confirm: MyElevatedButton(
                                          kolor: AppColor.greenColor,
                                          title: 'Next'.tr,
                                          onPressed: () {
                                            Get.back();
                                            mapPassengerController
                                                .isBottomSheetShown = false;
                                            mapPassengerController.update();
                                            mapPassengerController
                                                .changeCashConfirmPageShown();
                                          }),
                                      cancel: MyElevatedButton(
                                          title: 'Cancel'.tr,
                                          kolor: AppColor.redColor,
                                          onPressed: () {
                                            Get.back();
                                          }));
                                } else if (mapPassengerController
                                        .selectedIndex ==
                                    3) {
                                  box.write(BoxName.carType, 'Mashwari');
                                  mapPassengerController.totalPassenger = 50;
                                  Get.defaultDialog(
                                      title: 'Mashwari',
                                      titleStyle: AppStyle.title,
                                      content: CarDialogue(
                                          textToSpeechController:
                                              textToSpeechController,
                                          image: 'assets/images/freeRide.png',
                                          text:
                                              'Perfect for adventure seekers who want to experience something new and exciting'
                                                  .tr),
                                      confirm: MyElevatedButton(
                                          kolor: AppColor.greenColor,
                                          title: 'Next'.tr,
                                          onPressed: () {
                                            Get.back();
                                            mapPassengerController
                                                .isBottomSheetShown = false;
                                            mapPassengerController.update();
                                            mapPassengerController
                                                .changeCashConfirmPageShown();
                                          }),
                                      cancel: MyElevatedButton(
                                          title: 'Cancel'.tr,
                                          kolor: AppColor.redColor,
                                          onPressed: () {
                                            Get.back();
                                          }));
                                }
                              } else {
                                Get.snackbar('You should select one'.tr, '',
                                    backgroundColor: AppColor.redColor);
                              }
                            },
                          );
                        },
                      ),
                    ),
                    // MyElevatedButton(
                    //     title: 'Next'.tr,
                    //     onPressed: () {
                    //       if (mapPassengerController.selectedIndex != -1) {
                    //         // Get.snackbar('You should select one'.tr, '',
                    //         //     backgroundColor: AppColor.greenColor);
                    //         if (mapPassengerController.selectedIndex == 0) {
                    //           box.write(BoxName.carType, 'Comfort');
                    //           mapPassengerController.totalPassenger =
                    //               mapPassengerController.totalPassengerComfort;
                    //         } else if (mapPassengerController.selectedIndex ==
                    //             1) {
                    //           box.write(BoxName.carType, 'Speed');
                    //         } else if (mapPassengerController.selectedIndex ==
                    //             2) {
                    //           box.write(BoxName.carType, 'Delivery');
                    //           mapPassengerController.totalPassenger =
                    //               mapPassengerController
                    //                   .totalPassengerMotoDelivery;
                    //         } else if (mapPassengerController.selectedIndex ==
                    //             3) {
                    //           box.write(BoxName.carType, 'FreeRide');
                    //           mapPassengerController.totalPassenger = 50;
                    //         }
                    //         mapPassengerController.isBottomSheetShown = false;
                    //         mapPassengerController.update();
                    //         mapPassengerController.changeCashConfirmPageShown();
                    //         // mapPassengerController.confirmRideForFirstDriver();
                    //       } else {
                    //         Get.snackbar('You should select one'.tr, '',
                    //             backgroundColor: AppColor.redColor);
                    //       }
                    //     })
                  ],
                ),
              ),
            )
          : const SizedBox();
      // ;
    });
  }
}

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

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MapPassengerController>(
      builder: (mapPassengerController) {
        final passengerWallet =
            double.tryParse(box.read(BoxName.passengerWalletTotal)) ??
                0.0; // Handle potential parsing errors

        return mapPassengerController.data.isNotEmpty &&
                mapPassengerController.isBottomSheetShown &&
                !mapPassengerController.rideConfirm
            ? Positioned(
                bottom: Get.height * .41,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    if (passengerWallet < 0.0) // Use if statement for clarity
                      Container(
                        decoration: AppStyle.boxDecoration.copyWith(
                          color: AppColor.redColor.withOpacity(.5),
                        ),
                        height: 50,
                        width: Get.width,
                        child: Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 8),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: [
                              IconButton(
                                  onPressed: () async => await Get.find<
                                          TextToSpeechController>()
                                      .speakText('you have a negative balance of'
                                              .tr +
                                          '${passengerWallet.toStringAsFixed(2)}'
                                                  ' in your'
                                              .tr +
                                          ' ${AppInformation.appName}'
                                                  ' wallet due to a previous trip.'
                                              .tr),
                                  icon: const Icon(Icons.headphones)),
                              Text(
                                '${'you have a negative balance of'.tr}${'${passengerWallet.toStringAsFixed(2)}\n${' in your'.tr}'} ${AppInformation.appName}${' wallet due to a previous trip.'.tr}',
                                textAlign: TextAlign.center,
                                style: AppStyle.subtitle,
                              ),
                            ],
                          ),
                        ),
                      ),
                  ],
                ),
              )
            : const SizedBox();
      },
    );
  }
}

class CarDialogue extends StatelessWidget {
  const CarDialogue({
    super.key,
    required this.textToSpeechController,
    required this.image,
    required this.text,
  });

  final TextToSpeechController textToSpeechController;
  final String image, text;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset(
              image,
              width: 50,
            ),
            IconButton(
                onPressed: () {
                  textToSpeechController.speakText(
                    text,
                  );
                },
                icon: const Icon(Icons.headphones)),
          ],
        ),
        Text(
          text.tr,
          style: AppStyle.title,
          textAlign: TextAlign.center,
        ),
      ],
    );
  }
}
