import 'package:Intaleq/constant/links.dart';
import 'package:Intaleq/views/home/profile/complaint_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_font_icons/flutter_font_icons.dart';
import 'package:get/get.dart';
import 'package:Intaleq/constant/box_name.dart';
import 'package:Intaleq/controller/profile/profile_controller.dart';
import 'package:Intaleq/main.dart';

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

class VipRideBeginPassenger extends StatelessWidget {
  const VipRideBeginPassenger({
    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.statusRideVip == 'Begin' ||
          !controller.statusRideFromStart) {
        return Positioned(
          left: 10,
          right: 10,
          bottom: 10,
          child: Container(
            decoration: AppStyle.boxDecoration,
            height: controller.statusRideVip == '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(
                          '${AppLink.server}/portrate_captain_image/${controller.driverId}.jpg',
                        ),
                        onBackgroundImageError: (_, __) {
                          // Handle error here
                        },
                        backgroundColor: Colors.grey,
                        child: const Icon(
                          Icons.person, // Default icon or placeholder
                          size: 30,
                          color: Colors.white,
                        ), // Placeholder background color
                      ),
                      Column(
                        children: [
                          Container(
                            decoration: AppStyle.boxDecoration,
                            child: Padding(
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 6, vertical: 2),
                              child: Text(
                                controller.driverName,
                                style: AppStyle.title,
                              ),
                            ),
                          ),
                          const SizedBox(
                            height: 10,
                          ),
                          Container(
                            decoration: AppStyle.boxDecoration,
                            child: Padding(
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 6, vertical: 2),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                children: [
                                  Text(
                                    controller.make,
                                    style: AppStyle.title,
                                  ),
                                  const SizedBox(
                                    width: 10,
                                  ),
                                  Text(
                                    controller.model,
                                    style: AppStyle.title,
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                      Column(
                        children: [
                          Container(
                            decoration: AppStyle.boxDecoration,
                            child: Padding(
                              padding: const EdgeInsets.all(3),
                              child: Text(
                                'vip',
                                style: AppStyle.title,
                              ),
                            ),
                          ),
                          Text(
                            '${controller.driverRate} 📈',
                            style: AppStyle.title,
                          ),
                        ],
                      ),
                    ],
                  ),
                  // SizedBox(
                  //   height: 5,
                  // ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Container(
                        width: Get.width * .15,
                        decoration: AppStyle.boxDecoration,
                        child: IconButton(
                          onPressed: () => Get.to(
                            () => ComplaintPage(),
                            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.boxDecoration,
                        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
                              ),
                      ),
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Container(
                        decoration: AppStyle.boxDecoration,
                        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 {
                              makePhoneCall('122');
                              // box.read(BoxName.sosPhonePassenger));
                            }
                          },
                          icon: const Icon(
                            Icons.sos_rounded,
                            color: AppColor.redColor,
                          ),
                        ),
                      ),
                      Container(
                        decoration: AppStyle.boxDecoration,
                        width: Get.width * .15,
                        child: IconButton(
                          onPressed: () async {
                            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', '');
                              var phone = box.read(BoxName.countryCode) ==
                                      'Egypt'
                                  ? '+2${box.read(BoxName.sosPhonePassenger)}'
                                  : '+962${box.read(BoxName.sosPhonePassenger)}';
                              controller.sendWhatsapp(phone);
                            }
                          },
                          icon: const Icon(
                            FontAwesome.whatsapp,
                            color: AppColor.greenColor,
                          ),
                        ),
                      ),
                      Container(
                        decoration: AppStyle.boxDecoration,
                        width: Get.width * .15,
                        child: IconButton(
                          onPressed: () async {
                            await controller.shareTripWithFamily();
                          },
                          icon: const Icon(
                            AntDesign.Safety,
                            color: AppColor.blueColor,
                          ),
                        ),
                      ),
                    ],
                  ),
                  Stack(
                    children: [
                      // StreamCounter(),
                      LinearProgressIndicator(
                          backgroundColor: AppColor.accentColor,
                          color:
                              // controller.remainingTimeTimerRideBegin < 60
                              //     ? AppColor.redColor
                              // :
                              AppColor.greenColor,
                          minHeight: 25,
                          borderRadius: BorderRadius.circular(15),
                          value:
                              24 //controller.progressTimerRideBegin.toDouble(),
                          ),
                      Center(
                        child: Text(
                          controller.stringElapsedTimeRideBeginVip,
                          style: AppStyle.title,
                        ),
                      )
                    ],
                  )
                ],
              ),
            ),
          ),
        );
      } 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
              //   },
              // ),
            ],
          );
        },
      );
    });
  }
}
