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

import '../../../constant/colors.dart';
import '../../../constant/style.dart';
import '../../../controller/functions/toast.dart';
import '../../../controller/home/map_passenger_controller.dart';
import '../../../main.dart';

GetBuilder<MapPassengerController> formSearchPlaces(int index) {
  // DbSql sql = DbSql.instance;
  return GetBuilder<MapPassengerController>(
      builder: (controller) => Column(
            children: [
              Padding(
                padding: const EdgeInsets.all(16),
                child: Container(
                  decoration:
                      const BoxDecoration(color: AppColor.secondaryColor),
                  child: TextField(
                    decoration: InputDecoration(
                      border: const OutlineInputBorder(
                          borderRadius: BorderRadius.only(),
                          gapPadding: 4,
                          borderSide: BorderSide(
                            color: AppColor.redColor,
                            width: 2,
                          )),
                      suffixIcon: const Icon(Icons.search),
                      hintText: controller.hintTextwayPoint0.tr,
                      hintStyle: AppStyle.title,
                      hintMaxLines: 1,
                      prefixIcon: IconButton(
                        onPressed: () {
                          controller.allTextEditingPlaces[index].clear();
                          controller.clearPlaces(index);
                        },
                        icon: Icon(
                          Icons.clear,
                          color: Colors.red[300],
                        ),
                      ),
                    ),
                    controller: controller.allTextEditingPlaces[index],
                    onChanged: (value) {
                      if (controller.allTextEditingPlaces[index].text.length >
                          5) {
                        controller.getPlacesListsWayPoint(index);
                        controller.changeHeightPlacesAll(index);
                      }
                    },
                    // onEditingComplete: () => controller.changeHeight(),
                  ),
                ),
              ),
              controller.placeListResponseAll[index].isEmpty
                  ? InkWell(
                      onTap: () {
                        controller.startLocationFromMapAll[index] = true;
                        controller.wayPointIndex = index;
                        Get.back();
                        // controller.changeMainBottomMenuMap();
                        controller.changeWayPointStopsSheet();
                        controller.changePickerShown();
                      },
                      child: Text(
                        'Choose from Map'.tr + ' $index'.tr,
                        style:
                            AppStyle.title.copyWith(color: AppColor.blueColor),
                      ),
                    )
                  : const SizedBox(),
              Container(
                height: controller.placeListResponseAll[index].isNotEmpty
                    ? controller.height
                    : 0,
                color: AppColor.secondaryColor,
                child: ListView.builder(
                  itemCount: controller.placeListResponseAll[index].length,
                  itemBuilder: (BuildContext context, int i) {
                    var res = controller.placeListResponseAll[index][i];
                    return InkWell(
                      onTap: () async {
                        controller.changeHeightPlaces();
                        if (controller.currentLocationToFormPlacesAll[index] ==
                            true) {
                          controller.newStartPointLocation =
                              controller.passengerLocation;
                        } else {
                          controller.passengerLocation =
                              controller.newStartPointLocation;
                        }

                        controller.convertHintTextPlaces(index, res);
                      },
                      child: Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 10),
                        child: Column(
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Column(
                                  children: [
                                    Image.network(
                                      res['icon'],
                                      width: 20,
                                    ),
                                    IconButton(
                                      onPressed: () async {
                                        await sql.insertMapLocation({
                                          'latitude': res['geometry']
                                              ['location']['lat'],
                                          'longitude': res['geometry']
                                              ['location']['lng'],
                                          'name': res['name'].toString(),
                                          'rate': res['rating'].toString(),
                                        }, TableName.placesFavorite);
                                        Toast.show(
                                            context,
                                            '${res['name']} ${'Saved Sucssefully'.tr}',
                                            AppColor.primaryColor);
                                      },
                                      icon: const Icon(Icons.favorite_border),
                                    ),
                                  ],
                                ),
                                Column(
                                  children: [
                                    Text(
                                      res['name'].toString(),
                                      style: AppStyle.title,
                                    ),
                                    Text(
                                      res['vicinity'].toString(),
                                      style: AppStyle.subtitle,
                                    ),
                                  ],
                                ),
                                Column(
                                  children: [
                                    Text(
                                      'rate',
                                      style: AppStyle.subtitle,
                                    ),
                                    Text(
                                      res['rating'].toString(),
                                      style: AppStyle.subtitle,
                                    ),
                                  ],
                                ),
                              ],
                            ),
                            const Divider(
                              thickness: 1,
                            )
                          ],
                        ),
                      ),
                    );
                  },
                ),
              )
            ],
          ));
}
