import 'dart:async';
import 'dart:convert';

import 'package:siro_rider/constant/box_name.dart';
import 'package:siro_rider/constant/colors.dart';
import 'package:siro_rider/constant/links.dart';
import 'package:siro_rider/controller/home/map/ride_lifecycle_controller.dart';
import 'package:siro_rider/main.dart';
import 'package:siro_rider/views/widgets/elevated_btn.dart';
import 'package:siro_rider/views/widgets/my_scafold.dart';
import 'package:siro_rider/views/widgets/mycircular.dart';
import 'package:flutter/material.dart';
import 'package:flutter_font_icons/flutter_font_icons.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

import '../../constant/style.dart';
import '../functions/crud.dart';
import '../functions/encrypt_decrypt.dart';

class VipOrderController extends GetxController {
  RxBool isLoading = false.obs;
  final arguments = Get.arguments;
  RxList<dynamic> tripData = <dynamic>[].obs;
  RxBool isButtonVisible = false.obs;
  RxInt countdown = 60.obs;
  Timer? _countdownTimer;

  @override
  void onInit() {
    super.onInit();
    fetchOrder();
    startCountdown();
  }

  @override
  void onClose() {
    _countdownTimer?.cancel();
    super.onClose();
  }

  Future<void> fetchOrder() async {
    try {
      isLoading.value = true;
      var mapPassengerController = Get.find<RideLifecycleController>();

      var res = await CRUD().get(
        link: AppLink.getMishwari,
        payload: {
          // 'driverId': mapPassengerController.driverIdVip.toString(),
          'driverId': box.read(BoxName.passengerID).toString(),
        },
      );

      if (res != 'failure') {
        var decodedResponse = jsonDecode(res);
        if (decodedResponse['message'] is List) {
          tripData.value = decodedResponse['message'];
        } else {
          tripData.clear(); // Ensure empty list if no data
          // mySnackeBarError('No trip data found');
        }
      } else {
        tripData.clear();
        // mySnackeBarError('Failed to fetch trip data');
      }
    } catch (e) {
      tripData.clear();
      // mySnackeBarError('An error occurred: $e');
    } finally {
      isLoading.value = false;
    }
  }

  void startCountdown() {
    _countdownTimer?.cancel(); // Cancel any existing timer
    countdown.value = 60;

    _countdownTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
      if (countdown.value > 0) {
        countdown.value--;
      } else {
        timer.cancel();
        isButtonVisible.value = true;
      }
    });
  }

  void sendToDriverAgain() {
    // Reset states
    isButtonVisible.value = false;
    fetchOrder(); // Refresh order
    startCountdown(); // Restart countdown
  }
}

class VipWaittingPage extends StatelessWidget {
  VipWaittingPage({super.key});
  final VipOrderController vipOrderController = Get.put(VipOrderController());

  @override
  Widget build(BuildContext context) {
    return MyScafolld(
      title: "Waiting VIP".tr,
      body: [
        Obx(() {
          // Loading state
          if (vipOrderController.isLoading.value) {
            return const Center(child: MyCircularProgressIndicator());
          }

          // No data state
          if (vipOrderController.tripData.isEmpty) {
            return Center(
              child: Text(
                'No trip data available'.tr,
                style: AppStyle.title,
              ),
            );
          }

          // Data available
          var data = vipOrderController.tripData[0];

          // Function to get the localized status string
          String getLocalizedStatus(String status) {
            switch (status) {
              case 'pending':
                return 'pending'.tr;
              case 'accepted':
                return 'accepted'.tr;
              case 'begin':
                return 'begin'.tr;
              case 'rejected':
                return 'rejected'.tr;
              case 'cancelled':
                return 'cancelled'.tr;
              default:
                return 'unknown'.tr;
            }
          }

          // Function to get the appropriate status color
          Color getStatusColor(String status) {
            switch (status) {
              case 'pending':
                return Colors.yellow;
              case 'accepted':
                return Colors.green;
              case 'begin':
                return Colors.green;
              case 'rejected':
                return Colors.red;
              case 'cancelled':
                return Colors.red;
              default:
                return Colors.grey;
            }
          }

          return Card(
            elevation: 4,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(12),
            ),
            margin: const EdgeInsets.all(16),
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    "${'Driver Name:'.tr} ${data['name']}",
                    style: AppStyle.title,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            "${'Car Plate:'.tr} ${data['car_plate']}",
                            style: AppStyle.title,
                          ),
                          Text(
                            "${'Car Make:'.tr} ${data['make']}",
                            style: AppStyle.title,
                          ),
                          Text(
                            "${'Car Model:'.tr} ${data['model']}",
                            style: AppStyle.title,
                          ),
                          Text(
                            "${"Car Color:".tr} ${data['color']}",
                            style: AppStyle.title,
                          ),
                        ],
                      ),
                      SizedBox(
                        width: 100,
                        height: 100,
                        child: Icon(
                          Fontisto.car,
                          size: 80,
                          color: Color(
                            int.parse(
                              data['color_hex'].replaceFirst('#', '0xff'),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(height: 12),
                  const Divider(),
                  const SizedBox(height: 12),
                  Container(
                    color: getStatusColor(data['status']),
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Text(
                        "${'Trip Status:'.tr} ${getLocalizedStatus(data['status'])}",
                        style: const TextStyle(fontSize: 16),
                      ),
                    ),
                  ),
                  Text(
                    "${'Scheduled Time:'.tr} ${DateFormat('yyyy-MM-dd hh:mm a').format(DateTime.parse(data['timeSelected']))}",
                    style: const TextStyle(fontSize: 16),
                  ),
                  const SizedBox(height: 12),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      data['status'].toString() != 'begin'
                          ? MyElevatedButton(
                              title: "Cancel Trip".tr,
                              kolor: AppColor.redColor,
                              onPressed: () {
                                Get.find<RideLifecycleController>().cancelVip(
                                  data['token'].toString(),
                                  data['id'].toString(),
                                );
                              },
                            )
                          : const SizedBox(),
                      Obx(() {
                        // If countdown is still running, show countdown
                        if (!vipOrderController.isButtonVisible.value) {
                          return Column(
                            children: [
                              CircularProgressIndicator(
                                value: 1 -
                                    (vipOrderController.countdown.value / 60),
                                strokeWidth: 6.0,
                                color: AppColor.greenColor,
                                backgroundColor: AppColor.accentColor,
                              ),
                              const SizedBox(height: 10),
                              Text(
                                "${vipOrderController.countdown.value}s ${'remaining'.tr}",
                                style: const TextStyle(fontSize: 16),
                              ),
                            ],
                          );
                        }

                        // Once countdown is complete, show "Send to Driver Again" button
                        return MyElevatedButton(
                          title: "Send to Driver Again".tr,
                          kolor: AppColor.greenColor,
                          onPressed: () {
                            Get.find<RideLifecycleController>()
                                .sendToDriverAgain(data['token']);
                            vipOrderController.fetchOrder();
                          },
                        );
                      }),
                    ],
                  ),
                  const SizedBox(
                    height: 30,
                  ),
                  data['status'].toString() == 'begin'
                      ? MyElevatedButton(
                          title: "Click here to begin your trip\n\nGood luck, "
                                  .tr +
                              (box.read(BoxName.name).toString().split(' ')[0])
                                  .toString(),
                          kolor: AppColor.greenColor,
                          onPressed: () {
                            final mapPassengerController =
                                Get.find<RideLifecycleController>();
                            mapPassengerController.make = data['make'];
                            mapPassengerController.licensePlate =
                                data['car_plate'];
                            mapPassengerController.model = data['model'];
                            mapPassengerController.driverId = data['driverId'];
                            mapPassengerController.carColor = data['color'];
                            mapPassengerController.driverRate = data['rating'];
                            mapPassengerController.colorHex = data['color_hex'];
                            mapPassengerController.driverPhone = data['phone'];
                            mapPassengerController.driverToken = data['token'];
                            mapPassengerController.driverName =
                                data['name'].toString().split(' ')[0];

                            Get.back();

                            mapPassengerController.begiVIPTripFromPassenger();
                          },
                        )
                      : const SizedBox()
                ],
              ),
            ),
          );
        }),
      ],
      isleading: true,
    );
  }
}
