import 'package:flutter/material.dart';
import 'package:flutter_font_icons/flutter_font_icons.dart';
import 'package:get/get.dart';
import 'package:SEFER/constant/info.dart';
import 'package:SEFER/controller/functions/location_controller.dart';
import 'package:SEFER/views/widgets/elevated_btn.dart';
import 'package:SEFER/views/widgets/my_textField.dart';

import '../../../../constant/box_name.dart';
import '../../../../constant/colors.dart';
import '../../../../constant/style.dart';
import '../../../../controller/functions/launch.dart';
import '../../../../controller/home/captin/map_driver_controller.dart';
import '../../../../main.dart';

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

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MapDriverController>(
        builder: (mapDriverController) => mapDriverController.isRideStarted
            ? Positioned(
                left: 5,
                bottom: 20,
                child: Container(
                  decoration: AppStyle.boxDecoration,
                  height: 60,
                  width: 110,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      InkWell(
                        onTap: () {
                          if (box.read(BoxName.sosPhoneDriver) == null) {
                            Get.defaultDialog(
                                title: 'Insert Emergincy Number'.tr,
                                content: Form(
                                  key: mapDriverController.formKey1,
                                  child: MyTextForm(
                                      controller: mapDriverController
                                          .sosEmergincyNumberCotroller,
                                      label: 'Insert Emergency Number'.tr,
                                      hint: 'Insert Emergency Number'.tr,
                                      type: TextInputType.phone),
                                ),
                                confirm: MyElevatedButton(
                                    title: 'Insert'.tr,
                                    onPressed: () {
                                      if (mapDriverController
                                          .formKey1.currentState!
                                          .validate()) {
                                        box.write(
                                            BoxName.sosPhoneDriver,
                                            mapDriverController
                                                .sosEmergincyNumberCotroller
                                                .text);
                                      }
                                    }));
                          }
                          launchCommunication(
                              'phone', box.read(BoxName.sosPhoneDriver), '');
                        },
                        child: const Icon(
                          Icons.sos_sharp,
                          size: 45,
                          color: AppColor.redColor,
                        ),
                      ),
                      InkWell(
                        onTap: () {
                          if (box.read(BoxName.sosPhoneDriver) == null) {
                            Get.defaultDialog(
                                title: 'Insert Emergency Number'.tr,
                                content: Form(
                                  key: mapDriverController.formKey1,
                                  child: MyTextForm(
                                      controller: mapDriverController
                                          .sosEmergincyNumberCotroller,
                                      label: 'Insert Emergency Number'.tr,
                                      hint: 'Insert Emergency Number'.tr,
                                      type: TextInputType.phone),
                                ),
                                confirm: MyElevatedButton(
                                    title: 'Insert'.tr,
                                    onPressed: () {
                                      if (mapDriverController
                                          .formKey1.currentState!
                                          .validate()) {
                                        box.write(
                                            BoxName.sosPhoneDriver,
                                            mapDriverController
                                                .sosEmergincyNumberCotroller
                                                .text);
                                      }
                                    }));
                          } else {
                            launchCommunication(
                                'whatsapp',
                                '+02${box.read(BoxName.sosPhoneDriver)}', //todo  add number from driver
                                "${"Hello this is Driver".tr} ${box.read(BoxName.nameDriver)}.${" My current location is:".tr} https://www.google.com/maps/place/${Get.find<LocationController>().myLocation.latitude},${Get.find<LocationController>().myLocation.longitude}${" \nand I have a trip on".tr} ${AppInformation.appName} ${"App \nwith Passenger ".tr}${mapDriverController.passengerName}");
                          }
                        },
                        child: const Icon(
                          FontAwesome.whatsapp,
                          color: AppColor.greenColor,
                          size: 45,
                        ),
                      ),
                    ],
                  ),
                ))
            : const SizedBox());
  }
}
