import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ride/constant/box_name.dart';
import 'package:ride/controller/firebase/firbase_messge.dart';
import 'package:ride/main.dart';
import 'package:ride/views/home/Captin/driver_map_page.dart';
import 'package:ride/views/widgets/my_scafold.dart';

import '../../constant/colors.dart';
import '../../constant/links.dart';
import '../../constant/style.dart';
import '../../controller/functions/crud.dart';
import '../../controller/functions/launch.dart';
import '../../controller/home/captin/order_request_controller.dart';
import '../widgets/elevated_btn.dart';

class OrderRequestPage extends StatelessWidget {
  OrderRequestPage({super.key});
  OrderRequestController orderRequestController =
      Get.put(OrderRequestController());
  @override
  Widget build(BuildContext context) {
    final arguments = Get.arguments;
    final myListString = arguments['myListString'];
    final myList = arguments['DriverList'];
    final body = arguments['body'];
    orderRequestController.startTimer(myList[6].toString(), body.toString());
    return MyScafolld(
        title: 'Order Request Page'.tr,
        body: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // Text(message.notification!.body.toString()),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    TextButton.icon(
                        onPressed: () {
                          String mapUrl =
                              'https://www.google.com/maps/dir/${myList[0]}/${myList[1]}/';
                          print(mapUrl);
                          launchUrl1(mapUrl);
                        },
                        icon: const Icon(Icons.map),
                        label: Text('Rouats of Trip'.tr)),
                    myList[13].toString() ==
                            'true' //Visa or Cash Method from notify to driver
                        ? Text(
                            'Visa',
                            style: AppStyle.title,
                          )
                        : Text('Cash', style: AppStyle.title)
                  ],
                ),
              ),

              Padding(
                padding: const EdgeInsets.all(8.0),
                child: RichText(
                  text: TextSpan(
                    text: 'Passenger Name is '.tr,
                    style: AppStyle.title,
                    children: [
                      TextSpan(text: myList[8], style: AppStyle.headtitle2),
                    ],
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: RichText(
                  text: TextSpan(
                    text: 'Total From Passenger is '.tr,
                    style: AppStyle.title,
                    children: [
                      TextSpan(
                          text: double.parse(myList[2]).toStringAsFixed(2),
                          style: AppStyle.headtitle2),
                    ],
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: RichText(
                  text: TextSpan(
                    text: 'Duration To Passenger is '.tr,
                    style: AppStyle.title,
                    children: [
                      TextSpan(
                          text: myList[11].toString(),
                          style: AppStyle.headtitle2),
                    ],
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: RichText(
                  text: TextSpan(
                    text: 'Distance To Passenger is '.tr,
                    style: AppStyle.title,
                    children: [
                      TextSpan(
                          text: myList[12].toString(),
                          style: AppStyle.headtitle2),
                    ],
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: RichText(
                  text: TextSpan(
                    text: 'Total For You is '.tr,
                    style: AppStyle.title,
                    children: [
                      TextSpan(
                          text: double.parse(myList[3]).toStringAsFixed(2),
                          style: AppStyle.headtitle2),
                    ],
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: RichText(
                  text: TextSpan(
                      text: 'Distance is '.tr,
                      style: AppStyle.title,
                      children: [
                        TextSpan(
                            text: myList[5].toString(),
                            style: AppStyle.headtitle2),
                        TextSpan(text: ' KM'.tr, style: AppStyle.title),
                      ]),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: RichText(
                  text: TextSpan(
                      text: 'Duration of Trip is '.tr,
                      style: AppStyle.title,
                      children: [
                        TextSpan(
                            text: myList[4].toString(),
                            style: AppStyle.headtitle2),
                        TextSpan(text: ' Minutes'.tr, style: AppStyle.title),
                      ]),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    MyElevatedButton(
                      title: 'Apply Order'.tr,
                      onPressed: () async {
                        box.write(BoxName.statusDriverLocation, 'on');

                        orderRequestController.changeApplied();
                        await CRUD().postFromDialogue(
                            link: AppLink.addDriverOrder,
                            payload: {
                              'driver_id': myList[6].toString(),
                              // box.read(BoxName.driverID).toString(),
                              'order_id': body.toString(),
                              'status': 'Apply'
                            });
                        // Get.back();
                        List<String> bodyToPassenger = [
                          box.read(BoxName.driverID).toString(),
                          box.read(BoxName.nameDriver).toString(),
                          box.read(BoxName.tokenDriver).toString(),
                        ];
                        // print(bodyToPassenger);
                        // FirebaseMessagesController()
                        //     .sendNotificationToPassengerToken(
                        //   'Apply Ride',
                        //   arguments['DriverList'][9].toString(),
                        //   arguments['DriverList'][9].toString(),
                        //   // box.read(BoxName.tokenDriver).toString(),
                        //   bodyToPassenger,
                        // );
                        Get.back();
                        Get.to(() => PassengerLocationMapPage(), arguments: {
                          'passengerLocation': myList[0].toString(),
                          'Duration': myList[4].toString(),
                          'Distance': myList[5].toString(),
                          'name': myList[8].toString(),
                          'phone': myList[10].toString(),
                          'WalletChecked': myList[13].toString(),
                          'tokenPassenger': myList[9].toString(),
                          'direction':
                              'https://www.google.com/maps/dir/${myList[0]}/${myList[1]}/',
                          'DurationToPassenger': myList[15].toString(),
                          'rideId': myList[16].toString(),
                          'passengerId': myList[7].toString(),
                          'driverId': myList[18].toString(),
                          'durationOfRideValue': myList[19].toString(),
                          'paymentAmount': myList[3].toString(),
                          'paymentMethod': myList[13].toString(),
                        });
                      },
                    ),
                    GetBuilder<OrderRequestController>(
                      builder: (timerController) {
                        final isNearEnd = timerController.remainingTime <=
                            5; // Define a threshold for "near end"

                        return Stack(
                          alignment: Alignment.center,
                          children: [
                            CircularProgressIndicator(
                              value: timerController.progress,
                              // Set the color based on the "isNearEnd" condition
                              color: isNearEnd ? Colors.red : Colors.blue,
                            ),
                            Text(
                              '${timerController.remainingTime}',
                              style: AppStyle.number,
                            ),
                          ],
                        );
                      },
                    ),
                    MyElevatedButton(
                      title: 'Refuse Order'.tr,
                      onPressed: () async {
                        List<String> bodyToPassenger = [
                          box.read(BoxName.driverID).toString(),
                          box.read(BoxName.nameDriver).toString(),
                          box.read(BoxName.tokenDriver).toString(),
                        ];
                        orderRequestController.refuseOrder(
                            myList[16].toString(), body.toString());

                        FirebaseMessagesController()
                            .sendNotificationToPassengerToken(
                          'Refused Ride',
                          arguments['DriverList'][9].toString(),
                          arguments['DriverList'][9].toString(),
                          // box.read(BoxName.tokenDriver).toString(),
                          bodyToPassenger,
                        );
                      },
                      kolor: AppColor.redColor,
                    ),
                  ],
                ),
              )
            ],
          )
        ],
        isleading: true);
  }
}
