import 'package:SEFER/views/home/profile/feed_back_page.dart';
import 'package:SEFER/views/widgets/elevated_btn.dart';
import 'package:flutter/material.dart';
import 'package:flutter_font_icons/flutter_font_icons.dart';
import 'package:get/get.dart';
import 'package:SEFER/constant/box_name.dart';
import 'package:SEFER/controller/profile/profile_controller.dart';
import 'package:SEFER/main.dart';

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

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

  @override
  Widget build(BuildContext context) {
    ProfileController profileController = Get.put(ProfileController());
    AudioRecorderController audioController =
        Get.put(AudioRecorderController());
    // Get.put(MapPassengerController());
    return GetBuilder<MapPassengerController>(builder: (controller) {
      if (controller.statusRide == 'Begin' || !controller.statusRideFromStart) {
        return Positioned(
          left: 10,
          right: 10,
          bottom: 4,
          child: Container(
            decoration: AppStyle.boxDecoration,
            height: controller.statusRide == 'Begin' ? Get.height * .33 : 0,
            // width: 100,
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      CircleAvatar(
                        radius: 30,
                        backgroundImage: NetworkImage(
                            'https://ride.mobile-app.store/portrate_captain_image/${controller.driverId}.jpg'),
                      ),
                      Column(
                        children: [
                          Container(
                            decoration: AppStyle.boxDecoration1,
                            child: Text(
                              controller.firstName,
                              style: AppStyle.title,
                            ),
                          ),
                          Container(
                            decoration: AppStyle.boxDecoration1,
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: [
                                Text(
                                  controller.make,
                                  style: AppStyle.title,
                                ),
                                Text(
                                  controller.model,
                                  style: AppStyle.title,
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                      Column(
                        children: <Widget>[
                          const Text(''),
                          Container(
                            decoration: AppStyle.boxDecoration1,
                            child: Padding(
                              padding:
                                  const EdgeInsets.symmetric(horizontal: 4),
                              child: Text(
                                controller.licensePlate,
                                style: AppStyle.title,
                              ),
                            ),
                          ),
                        ],
                      ),
                      Column(
                        children: [
                          Container(
                            decoration: AppStyle.boxDecoration1,
                            child: Padding(
                              padding: const EdgeInsets.all(3),
                              child: Text(
                                '${box.read(BoxName.carType)}',
                                style: AppStyle.title,
                              ),
                            ),
                          ),
                          Text(
                            '${controller.driverRate} 📈',
                            style: AppStyle.title,
                          ),
                        ],
                      ),
                    ],
                  ),
                  // SizedBox(
                  //   height: 5,
                  // ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      InkWell(
                        onTap: () {
                          controller.getDialog('Arrival time'.tr,
                              'arrival time to reach your point'.tr, () {});
                        },
                        child: Container(
                          width: Get.width * .15,
                          decoration: AppStyle.boxDecoration1,
                          child: Column(
                            children: [
                              Text(
                                '⏱️',
                                style: AppStyle.title,
                              ),
                              Text(
                                controller.arrivalTime,
                                style: AppStyle.title,
                              ),
                            ],
                          ),
                        ),
                      ),
                      InkWell(
                        onTap: () {
                          controller.getDialog(
                              'Price of trip'.tr,
                              'For Speed and Delivery trips, the price is calculated dynamically. For Comfort trips, the price is based on time and distance'
                                  .tr, () {
                            Get.back();
                          });
                        },
                        child: Container(
                          width: Get.width * .15,
                          decoration: AppStyle.boxDecoration1,
                          child: Column(
                            children: [
                              Text(
                                '💵 ',
                                style: AppStyle.title,
                              ),
                              Text(
                                controller.totalPassenger.toStringAsFixed(2),
                                style: AppStyle.title,
                              ),
                            ],
                          ),
                        ),
                      ),
                      Container(
                        width: Get.width * .15,
                        decoration: AppStyle.boxDecoration1,
                        child: IconButton(
                          onPressed: () => Get.to(
                            () => FeedBackPage(),
                            transition: Transition.downToUp,
                          ),
                          icon: const Icon(
                            Icons.note_add,
                            color: AppColor.redColor,
                          ),
                          tooltip: ' Add Note', // Optional tooltip for clarity
                        ),
                      ),
                      Container(
                        width: Get.width * .15,
                        decoration: AppStyle.boxDecoration1,
                        child: audioController.isRecording == false
                            ? IconButton(
                                onPressed: () async {
                                  await audioController.startRecording();
                                  Toast.show(context, 'Start Record'.tr,
                                      AppColor.greenColor);
                                },
                                icon: const Icon(
                                  Icons.play_circle_fill_outlined,
                                  color: AppColor.greenColor,
                                ),
                                tooltip:
                                    ' Add Note', // Optional tooltip for clarity
                              )
                            : IconButton(
                                onPressed: () async {
                                  await audioController.stopRecording();
                                  Toast.show(context, 'Record saved'.tr,
                                      AppColor.greenColor);
                                },
                                icon: const Icon(
                                  Icons.stop_circle,
                                  color: AppColor.greenColor,
                                ),
                                tooltip:
                                    ' Add Note', // Optional tooltip for clarity
                              ),
                      ),
                    ],
                  ),
                  Stack(
                    children: [
                      // StreamCounter(),
                      LinearProgressIndicator(
                        backgroundColor: AppColor.accentColor,
                        color: controller.remainingTimeTimerRideBegin < 60
                            ? AppColor.redColor
                            : AppColor.greenColor,
                        minHeight: 25,
                        borderRadius: BorderRadius.circular(15),
                        value: controller.progressTimerRideBegin.toDouble(),
                      ),
                      Center(
                        child: Text(
                          controller.stringRemainingTimeRideBegin,
                          style: AppStyle.title,
                        ),
                      )
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Container(
                        decoration: AppStyle.boxDecoration1,
                        width: Get.width * .15,
                        child: IconButton(
                          onPressed: () async {
                            if (box.read(BoxName.sosPhonePassenger) == null) {
                              {
                                await profileController.updatField(
                                    'sosPhone', TextInputType.phone);
                                box.write(BoxName.sosPhonePassenger,
                                    profileController.prfoileData['sosPhone']);
                              }
                            } else {
                              controller
                                  .sendSMS(box.read(BoxName.sosPhonePassenger));
                            }
                          },
                          icon: const Icon(
                            Icons.sos_rounded,
                            color: AppColor.redColor,
                          ),
                        ),
                      ),
                      Container(
                        decoration: AppStyle.boxDecoration1,
                        width: Get.width * .15,
                        child: IconButton(
                          onPressed: () async {
                            print(box.read(BoxName.sosPhonePassenger));
                            if (box.read(BoxName.sosPhonePassenger) == null ||
                                box.read(BoxName.sosPhonePassenger) == 'sos') {
                              {
                                await profileController.updatField(
                                    'sosPhone', TextInputType.phone);
                                box.write(BoxName.sosPhonePassenger,
                                    profileController.prfoileData['sosPhone']);
                              }
                            } else {
                              String phoneNumber = box
                                  .read(BoxName.sosPhonePassenger)
                                  .toString();
                              // phoneNumber = phoneNumber.replaceAll('0', '');
                              print(phoneNumber); // Output: 798583061
                              var phone =
                                  // '+${box.read(BoxName.countryCode)}${box.read(BoxName.sosPhonePassenger)}';
                                  '${box.read(BoxName.sosPhonePassenger)}';
                              controller.sendWhatsapp(phone);
                            }
                          },
                          icon: const Icon(
                            FontAwesome.whatsapp,
                            color: AppColor.greenColor,
                          ),
                        ),
                      ),
                    ],
                  )
                ],
              ),
            ),
          ),
        );
      } else {
        return const SizedBox();
      }
    });
  }
}

class StreamCounter extends StatelessWidget {
  const StreamCounter({Key? key}) : super(key: key);

  @override
  // Build the UI based on the timer value
  Widget build(BuildContext context) {
    Get.put(MapPassengerController());
    return GetBuilder<MapPassengerController>(builder: (controller) {
      return StreamBuilder<int>(
        initialData: 0,
        stream: controller.timerController.stream,
        builder: (context, snapshot) {
          // Calculate the remaining time based on the current tick
          final remainingTime = controller.durationToRide - snapshot.data!;

          // Format the remaining time as a string
          final formattedRemainingTime =
              '${(remainingTime / 60).floor()}:${(remainingTime % 60).toString().padLeft(2, '0')}';

          // Return the UI widgets based on the remaining time
          return Column(
            children: [
              Text(formattedRemainingTime),
              // ElevatedButton(
              //   onPressed: () {
              //     // Handle button press here
              //   },
              // ),
            ],
          );
        },
      );
    });
  }
}
