import 'dart:async';

import 'package:SEFER/constant/colors.dart';
import 'package:SEFER/constant/style.dart';
import 'package:SEFER/controller/home/map_passenger_controller.dart';
import 'package:SEFER/views/widgets/elevated_btn.dart';
import 'package:SEFER/views/widgets/my_textField.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

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

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MapPassengerController>(
      builder: (mapPassengerController) {
        return mapPassengerController.isSearchingWindow
            ? Positioned(
                bottom: 0,
                left: 0,
                right: 0,
                child: Container(
                  decoration: AppStyle.boxDecoration1,
                  height: Get.height * .2,
                  child: Column(
                    // Use Stack for overlapping widgets
                    children: [
                      // Text elements
                      SizedBox(
                        width: Get.width * .7,
                        child: const LinearProgressIndicator(
                          minHeight: 6,
                          backgroundColor: AppColor.yellowColor,
                          color: AppColor.secondaryColor,
                        ),
                      ),
                      Text(
                        'We search nearst Driver to you'.tr,
                        style: AppStyle.headTitle2,
                      ),
                      Text(
                        'please wait till driver accept your order'.tr,
                        style: AppStyle.title,
                      ), // Timer logic
                      _buildTimer(mapPassengerController),
                    ],
                  ),
                ),
              )
            : const SizedBox();
      },
    );
  }
}

Widget _buildTimer(MapPassengerController mapPassengerController) {
  // Start timer at 0
  Timer? timer;

  return StreamBuilder<int>(
    // Use StreamBuilder for timer updates
    stream: Stream.periodic(const Duration(seconds: 1))
        .map((_) => ++mapPassengerController.currentTimeSearchingCaptainWindow),
    initialData: 0,
    builder: (context, snapshot) {
      if (snapshot.hasData && snapshot.data! > 30) {
        timer?.cancel(); // Cancel timer after 60 seconds
        return GestureDetector(
          onTap: () {
            Get.defaultDialog(
              barrierDismissible: false,
              title: "Increase Your Trip Fee (Optional)".tr,
              titleStyle: AppStyle.title,
              content: Column(
                children: [
                  Text(
                    "We haven't found any drivers yet. Consider increasing your trip fee to make your offer more attractive to drivers."
                        .tr,
                    style: AppStyle.title,
                    textAlign: TextAlign.center,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      IconButton(
                        onPressed: () {
                          mapPassengerController.increasFeeFromPassenger.text =
                              (mapPassengerController.totalPassenger + 3)
                                  .toStringAsFixed(1);
                          // mapPassengerController.increasFeeFromPassenger.text =
                          //     mapPassengerController.totalPassenger
                          //         .toStringAsFixed(1);
                          mapPassengerController.update();
                        },
                        icon: Column(
                          children: [
                            Text(
                              '3',
                              style: AppStyle.number,
                            ),
                            Container(
                              decoration: const BoxDecoration(
                                  shape: BoxShape.circle,
                                  color: AppColor.greenColor),
                              child: const Icon(
                                Icons.arrow_circle_up,
                                size: 30,
                                color: AppColor.secondaryColor,
                              ),
                            ),
                          ],
                        ),
                      ),
                      SizedBox(
                        width: 100,
                        child: Form(
                          key: mapPassengerController.increasFeeFormKey,
                          child: MyTextForm(
                              controller: mapPassengerController
                                  .increasFeeFromPassenger,
                              label: mapPassengerController.totalPassenger
                                  .toStringAsFixed(2),
                              hint: mapPassengerController.totalPassenger
                                  .toStringAsFixed(2),
                              type: TextInputType.number),
                        ),
                      ),
                      IconButton(
                        onPressed: () {
                          // if ((double.parse(mapPassengerController
                          //         .increasFeeFromPassenger.text) >
                          //     totalPassenger)) {}
                          mapPassengerController.increasFeeFromPassenger.text =
                              (mapPassengerController.totalPassenger - 3)
                                  .toStringAsFixed(1);
                          // mapPassengerController.increasFeeFromPassenger.text =
                          //     mapPassengerController.totalPassenger
                          //         .toStringAsFixed(1);
                          mapPassengerController.update();
                        },
                        icon: Column(
                          children: [
                            Text(
                              '3',
                              style: AppStyle.number,
                            ),
                            Container(
                              decoration: const BoxDecoration(
                                  shape: BoxShape.circle,
                                  color: AppColor.redColor),
                              child: const Icon(
                                Icons.arrow_drop_down_circle_outlined,
                                size: 30,
                                color: AppColor.secondaryColor,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  )
                ],
              ),
              actions: [
                MyElevatedButton(
                    title: "No, thanks",
                    onPressed: () {
                      Get.back();
                    }),
                MyElevatedButton(
                    title: "Increase Fee".tr,
                    onPressed: () {
                      mapPassengerController.increaseFeeByPassengerAndReOrder();
                    })
              ],
            );
          },
          child: Text(
            "No accepted orders? Try raising your trip fee to attract riders."
                .tr,
            style: AppStyle.title.copyWith(color: AppColor.blueColor),
          ),
        );
      }

      // Update progress for circular indicator (0.0 to 1.0)
      final double progress =
          snapshot.data!.toDouble() / 30.0; // Normalize progress

      return Stack(
        children: [
          Center(
            child: CircularProgressIndicator(
              value: progress, // Use calculated progress
              color: AppColor.blueColor,
              backgroundColor: AppColor.yellowColor,
            ),
          ),
          Center(
            child: Text(
              '${snapshot.data} ', // Display elapsed time
              style: AppStyle.title.copyWith(
                  color: AppColor.greenColor), // Adjust color for timer
            ),
          )
        ],
      );
    },
  );
}
