import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:siro_rider/constant/table_names.dart';

import '../../../constant/colors.dart';
import '../../../constant/style.dart';
import '../../../controller/home/map/location_search_controller.dart';
import '../../../controller/home/map/map_engine_controller.dart';
import '../../../controller/home/map/ride_lifecycle_controller.dart';
import '../../../main.dart';
import '../../widgets/elevated_btn.dart';
import 'form_search_places_destenation.dart';

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

  @override
  Widget build(BuildContext context) {
    final mapEngine = Get.find<MapEngineController>();
    final locationSearch = Get.find<LocationSearchController>();
    final rideLifecycle = Get.find<RideLifecycleController>();

    return GetBuilder<MapEngineController>(
        builder: (controller) => Positioned(
              bottom: 0,
              left: 0,
              right: 5,
              child: AnimatedContainer(
                duration: const Duration(milliseconds: 300),
                height: controller.heightPickerContainer,
                decoration: BoxDecoration(
                    boxShadow: const [
                      BoxShadow(
                          color: AppColor.accentColor, offset: Offset(2, 2)),
                      BoxShadow(
                          color: AppColor.accentColor, offset: Offset(-2, -2))
                    ],
                    color: AppColor.secondaryColor,
                    borderRadius: const BorderRadius.only(
                      topLeft: Radius.circular(15),
                      topRight: Radius.circular(15),
                    )),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    controller.isPickerShown
                        ? const SizedBox()
                        : Text(
                            'Hi, Where to  '.tr,
                            style: AppStyle.title,
                          ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        const SizedBox(
                          height: 5,
                        ),
                        controller.isPickerShown
                            ? InkWell(
                                onTapDown: (details) {
                                  controller.changePickerShown();
                                  controller.changeHeightPlaces();
                                },
                                child: Container(
                                  height: 7,
                                  width: Get.width * .3,
                                  decoration: BoxDecoration(
                                      color: AppColor.accentColor,
                                      borderRadius: BorderRadius.circular(10),
                                      border: Border.all(
                                        color: AppColor.accentColor,
                                      )),
                                ),
                              )
                            : const SizedBox(),
                        controller.isPickerShown
                            ? InkWell(
                                onTap: () {},
                                child: formSearchPlacesDestenation(),
                              )
                            : Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceEvenly,
                                children: [
                                  TextButton(
                                    onPressed: () {
                                      controller.changePickerShown();
                                    },
                                    child: Text(
                                      "Pick your destination from Map".tr,
                                      style: AppStyle.subtitle,
                                    ),
                                  ),
                                  TextButton(
                                    onPressed: () async {
                                      List favoritePlaces = await sql
                                          .getAllData(TableName.placesFavorite);
                                      Get.defaultDialog(
                                        title: 'Favorite Places'.tr,
                                        content: SizedBox(
                                          width: Get.width * .8,
                                          height: 300,
                                          child: favoritePlaces.isEmpty
                                              ? Center(
                                                  child: Column(
                                                    mainAxisAlignment:
                                                        MainAxisAlignment.center,
                                                    children: [
                                                      const Icon(
                                                        Icons
                                                            .hourglass_empty_rounded,
                                                        size: 99,
                                                        color: AppColor
                                                            .primaryColor,
                                                      ),
                                                      Text(
                                                        'You Dont Have Any places yet !'
                                                            .tr,
                                                        style: AppStyle.title,
                                                      ),
                                                    ],
                                                  ),
                                                )
                                              : ListView.builder(
                                                  itemCount:
                                                      favoritePlaces.length,
                                                  itemBuilder:
                                                      (BuildContext context,
                                                          int index) {
                                                    return Row(
                                                      mainAxisAlignment:
                                                          MainAxisAlignment
                                                              .spaceBetween,
                                                      children: [
                                                        TextButton(
                                                          onPressed: () async {
                                                            await rideLifecycle
                                                                .getDirectionMap(
                                                              '${locationSearch.passengerLocation.latitude},${locationSearch.passengerLocation.longitude}',
                                                              '${favoritePlaces[index]['latitude']},${favoritePlaces[index]['longitude']}',
                                                            );
                                                            controller
                                                                .changePickerShown();
                                                            controller
                                                                .changeBottomSheetShown(
                                                                    forceValue:
                                                                        true);
                                                            rideLifecycle
                                                                .bottomSheet();
                                                            Get.back();
                                                          },
                                                          child: Text(
                                                            favoritePlaces[
                                                                index]['name'],
                                                            style:
                                                                AppStyle.title,
                                                          ),
                                                        ),
                                                        IconButton(
                                                          onPressed: () async {
                                                            await sql.deleteData(
                                                                TableName
                                                                    .placesFavorite,
                                                                favoritePlaces[
                                                                        index]
                                                                    ['id']);
                                                            Get.back();
                                                            Get.snackbar(
                                                                'Deleted ',
                                                                '${'You are Delete'.tr} ${favoritePlaces[index]['name']} from your list',
                                                                backgroundColor:
                                                                    AppColor
                                                                        .accentColor);
                                                          },
                                                          icon: const Icon(Icons
                                                              .favorite_outlined),
                                                        ),
                                                      ],
                                                    );
                                                  },
                                                ),
                                        ),
                                        onCancel: () {},
                                      );
                                    },
                                    child: Text(
                                      "Go To Favorite Places".tr,
                                      style: AppStyle.subtitle,
                                    ),
                                  ),
                                ],
                              ),
                        if (controller.isPickerShown &&
                            locationSearch.placesDestination.isEmpty)
                          MyElevatedButton(
                            title: 'Go to this Target'.tr,
                            onPressed: () async {
                              await rideLifecycle.getDirectionMap(
                                '${locationSearch.passengerLocation.latitude},${locationSearch.passengerLocation.longitude}',
                                '${locationSearch.newMyLocation.latitude},${locationSearch.newMyLocation.longitude}',
                              );
                              controller.changePickerShown();
                              controller.changeBottomSheetShown(
                                  forceValue: true);
                              rideLifecycle.bottomSheet();
                            },
                          ),
                        if (controller.isPickerShown &&
                            locationSearch.placesDestination.isEmpty)
                          const SizedBox(),
                      ],
                    ),
                  ],
                ),
              ),
            ));
  }
}
