import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ride/constant/table_names.dart';
import 'package:ride/views/widgets/elevated_btn.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() {
  // 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(
                      suffixIcon: const Icon(Icons.search),
                      hintText: 'Type here Place'.tr,
                      hintMaxLines: 1,
                      prefixIcon: IconButton(
                        onPressed: () {
                          controller.placeController.clear();
                          controller.clearPlaces();
                        },
                        icon: Icon(
                          Icons.clear,
                          color: Colors.red[300],
                        ),
                      ),
                    ),
                    controller: controller.placeController,
                    onChanged: (value) {
                      if (controller.placeController.text.length > 5) {
                        controller.getPlaces();
                        controller.changeHeight();
                      }
                    },
                    // onEditingComplete: () => controller.changeHeight(),
                  ),
                ),
              ),
              Container(
                height: controller.places.isNotEmpty ? controller.height : 0,
                color: AppColor.secondaryColor,
                child: ListView.builder(
                  itemCount: controller.places.length,
                  itemBuilder: (BuildContext context, int index) {
                    var res = controller.places[index];
                    return InkWell(
                      onTap: () {
                        controller.changeHeight();
                        Get.defaultDialog(
                          title:
                              '${'Are You sure to ride to'.tr} ${res['name']}',
                          middleText: '',
                          confirm: MyElevatedButton(
                              title: 'Confirm'.tr,
                              onPressed: () async {
                                controller.clearPolyline();
                                controller.data = [];
                                await controller.getMap(
                                    '${controller.myLocation.latitude.toString()},${controller.myLocation.longitude.toString()}',
                                    "${res['geometry']['location']['lat']},${res['geometry']['location']['lng']}");
                                controller.places = [];
                                controller.placeController.clear();

                                controller.showBottomSheet1();
                                Get.back();
                                controller.showBottomSheet1();
                                controller.changeMainBottomMenuMap();
                              }),
                        );
                      },
                      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.insertData({
                                          '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,
                            )
                          ],
                        ),
                      ),
                    );
                  },
                ),
              )
            ],
          ));
}
