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 '../../../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'.tr,
      carDetail: 'Comfort choice'.tr,
      image: 'assets/images/blob.png'),
  CarType(
      carType: 'Speed'.tr,
      carDetail: 'Better for long trips choice'.tr,
      image: 'assets/images/carspeed.png'),
  CarType(
      carType: 'Delivery'.tr,
      carDetail: 'Delivery service'.tr,
      image: 'assets/images/moto.png'),
];

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

  @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 * .3,
                      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)
                                                : mapPassengerController
                                                    .totalPassengerMotoDelivery
                                                    .toStringAsFixed(2),
                                        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);
                            },
                          );
                        },
                      ),
                    ),
                    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;
                            }
                            mapPassengerController.isBottomSheetShown = false;
                            mapPassengerController.update();
                            mapPassengerController.changeCashConfirmPageShown();
                            // mapPassengerController.confirmRideForFirstDriver();
                          } else {
                            Get.snackbar('You should select one'.tr, '',
                                backgroundColor: AppColor.redColor);
                          }
                        })
                  ],
                ),
              ),
            )
          : const SizedBox();
      // ;
    });
  }
}
