import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:SEFER/constant/style.dart';
import 'package:SEFER/views/admin/captain/form_captain.dart';

import '../../../constant/colors.dart';
import '../../../controller/home/map_passenger_controller.dart';
import '../../../controller/home/points_for_rider_controller.dart';

class PointsPageForRider extends StatelessWidget {
  PointsPageForRider({
    super.key,
  });
  MapPassengerController mapPassengerController =
      Get.put(MapPassengerController());

  @override
  Widget build(BuildContext context) {
    Get.put(WayPointController());

    return GetBuilder<MapPassengerController>(builder: (controller) {
      return Positioned(
        bottom: 2,
        left: 2,
        right: 2,
        child: AnimatedContainer(
          duration: const Duration(milliseconds: 300),
          height: controller.wayPointSheetHeight,
          decoration: AppStyle.boxDecoration,
          child: ListView(
            children: [
              // const AppBarPointsPageForRider(),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  IconButton(
                    onPressed: () {
                      mapPassengerController.downPoints();
                    },
                    icon: const Icon(Icons.arrow_drop_down_circle_outlined),
                  ),
                  GetBuilder<WayPointController>(builder: (wayPointController) {
                    return Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        ElevatedButton(
                          onPressed: () {
                            wayPointController.addWayPoints();
                            controller.isWayPointStopsSheetUtilGetMap = true;
                          },
                          child: const Text('Add Stops'),
                        ),
                        wayPointController.wayPoints.length > 1
                            ? ElevatedButton(
                                onPressed: () async {
                                  mapPassengerController
                                      .getMapPointsForAllMethods();
                                },
                                child: const Text('Get Direction'),
                              )
                            : const SizedBox()
                      ],
                    );
                  }),
                ],
              ),
              SizedBox(
                height: Get.height * .36,
                child: GetBuilder<WayPointController>(
                    builder: (wayPointController) {
                  return ReorderableListView(
                    // The children of the list are the text fields
                    children: wayPointController.wayPoints
                        .asMap()
                        .entries
                        .map((entry) {
                      final index = entry.key;
                      final wayPoint = entry.value;
                      return Padding(
                        key: ValueKey(index),
                        padding: const EdgeInsets.all(1),
                        child: ListTile(
                            leading: Container(
                                decoration: BoxDecoration(
                                    color: AppColor.deepPurpleAccent,
                                    border: Border.all(),
                                    shape: BoxShape.rectangle),
                                child: Padding(
                                  padding: const EdgeInsets.all(2),
                                  child: Text(
                                    index.toString(),
                                    style: AppStyle.title,
                                  ),
                                )),
                            title: InkWell(
                              onTap: () {
                                // showAddLocationDialog(context);
                                Get.defaultDialog(
                                  content: SizedBox(
                                      width: Get.width,
                                      height: 400,
                                      child: mapPassengerController
                                          .placeListResponse[index]),
                                );
                              },
                              child: Container(
                                decoration: BoxDecoration(
                                    border: Border.all(),
                                    color:
                                        AppColor.accentColor.withOpacity(.5)),
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Text(mapPassengerController
                                        .currentLocationStringAll[index]
                                        .toString()),
                                    const Icon(
                                      Icons.reorder,
                                      size: 20,
                                    ),
                                  ],
                                ),
                              ),
                            ),
                            trailing: index > 0
                                ? IconButton(
                                    icon: const Icon(Icons.close),
                                    onPressed: () {
                                      wayPointController.removeTextField(index);
                                    },
                                  )
                                : IconButton(
                                    icon: const Icon(
                                      Icons.close,
                                      color: AppColor.secondaryColor,
                                    ),
                                    onPressed: () {},
                                  )),
                      );
                    }).toList(),

                    // The callback when the user reorders the text fields
                    onReorder: (int oldIndex, int newIndex) {
                      wayPointController.reorderTextFields(oldIndex, newIndex);
                    },
                  );
                }),
              ),
            ],
          ),
        ),
      );
    });
  }

  // GetBuilder<PointsForRiderController>(
  //     builder: (controller) => Container(
  //           decoration: AppStyle.boxDecoration,
  //           height: Get.height *
  //               .5, //  height: controller.heightPointsPageForRider,
  //           width: Get.width,
  //           child: Column(
  //             children: [
  //               SizedBox(
  //                 height: 300,
  //                 child: ReorderableListView(
  //                   onReorder: (oldIndex, newIndex) {
  //                     if (oldIndex < newIndex) {
  //                       newIndex -= 1;
  //                     }
  //                     pointsForRiderController.locations.insert(
  //                         newIndex,
  //                         pointsForRiderController.locations
  //                             .removeAt(oldIndex));
  //                   },
  //                   children: [
  //                     for (int i = 0;
  //                         i < pointsForRiderController.locations.length;
  //                         i++)
  //                       ListTile(
  //                         key: Key('$i'),
  //                         title: DragTarget<int>(
  //                           onAccept: (int data) {
  //                             pointsForRiderController.locations
  //                                 .insert(i, 'New Text Field');
  //                           },
  //                           builder: (context, candidateData, rejectedData) {
  //                             return Row(
  //                               children: [
  //                                 SizedBox(
  //                                   width: 300,
  //                                   child: TextField(
  //                                     controller: TextEditingController(
  //                                         text: pointsForRiderController
  //                                             .locations[i]),
  //                                     onChanged: (value) {
  //                                       pointsForRiderController
  //                                           .locations[i] = value;
  //                                     },
  //                                     decoration: InputDecoration(
  //                                       prefixIcon: IconButton(
  //                                         onPressed: () {
  //                                           pointsForRiderController
  //                                               .removeLocation(i);
  //                                         },
  //                                         icon: const Icon(Icons.delete),
  //                                       ),
  //                                       labelText: 'Text Field ${i + 1}',
  //                                       border: const OutlineInputBorder(),
  //                                     ),
  //                                   ),
  //                                 ),
  //                                 IconButton(
  //                                   onPressed: () {
  //                                     // pointsForRiderController.onReorder(
  //                                     //     index, newIndex);
  //                                   },
  //                                   icon: const Icon(Icons.reorder),
  //                                 ),
  //                               ],
  //                             );
  //                           },
  //                         ),
  //                       ),
  //                   ],
  //                 ),
  //               ),
  //               ElevatedButton(
  //                 onPressed: () {
  //                   pointsForRiderController.addLocation('location');
  //                 },
  //                 child: const Text('Add Text Field'),
  //               ),
  //             ],
  //           ),
  //         ));
}

void showAddLocationDialog(BuildContext context, int index) {
  final TextEditingController locationController = TextEditingController();
  // Get.put(WayPointController());
  showDialog(
    context: context,
    builder: (context) {
      return Dialog.fullscreen(
        // title: const Text('Add Location'),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                IconButton(
                  onPressed: () {
                    Get.back();
                  },
                  icon: const Icon(
                    Icons.close,
                    size: 40,
                  ),
                ),
                Text(
                  'Add Location'.tr,
                  style: AppStyle.title,
                ),
                const Icon(
                  Icons.clear,
                  color: AppColor.secondaryColor,
                )
              ],
            ),
            SizedBox(
              width: Get.width,
              child: formSearchCaptain(),
            ),
          ],
        ),
      );
    },
  );
}

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

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        IconButton(
          onPressed: () {},
          icon: const Icon(
            Icons.arrow_back_ios_new_rounded,
            color: AppColor.primaryColor,
          ),
        ),
        Container(
          child: Row(
            children: [
              const CircleAvatar(
                backgroundColor: AppColor.primaryColor,
                maxRadius: 15,
                child: Icon(
                  Icons.person,
                  color: AppColor.secondaryColor,
                ),
              ),
              TextButton(
                onPressed: () {},
                child: Text(
                  "Switch Rider".tr,
                  style: AppStyle.title,
                ),
              ),
            ],
          ),
        ),
        const Icon(
          Icons.clear,
          color: AppColor.secondaryColor,
        )
      ],
    );
  }
}
