import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ride/constant/links.dart';
import 'package:ride/views/widgets/elevated_btn.dart';

import '../../../constant/box_name.dart';
import '../../../constant/colors.dart';
import '../../../constant/style.dart';
import '../../../controller/firebase/firbase_messge.dart';
import '../../../controller/functions/crud.dart';
import '../../../controller/home/map_passenger_controller.dart';
import '../../../main.dart';

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

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MapPassengerController>(builder: (controller) {
      if (controller.remainingTime == 0 &&
          controller.timeToPassengerFromDriverAfterApplied > 0) {
        return Positioned(
          left: 10,
          right: 10,
          bottom: 5,
          child: Container(
            decoration: AppStyle.boxDecoration,
            height: 200,
            // width: 100,
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Column(
                children: [
                  Text(
                    'You Can cancel Ride After Captain did not come in the time'
                        .tr,
                    style: AppStyle.title,
                  ),
                  Stack(
                    children: [
                      LinearProgressIndicator(
                        backgroundColor: AppColor.accentColor,
                        color:
                            controller.timeToPassengerFromDriverAfterApplied <
                                    60
                                ? AppColor.redColor
                                : AppColor.greenColor,
                        minHeight: 50,
                        borderRadius: BorderRadius.circular(15),
                        value: controller
                            .progressTimerToPassengerFromDriverAfterApplied
                            .toDouble(),
                      ),
                      Center(
                        child: Text(
                          controller.stringRemainingTimeToPassenger,
                          style: AppStyle.title,
                        ),
                      )
                    ],
                  ),
                  controller.remainingTimeToPassengerFromDriverAfterApplied < 60
                      ? MyElevatedButton(
                          title: 'If you in Car Now. Press Start The Ride'.tr,
                          onPressed: () async {
                            //todo start the trip and rest all counter ,start new counter of the trip time

                            await CRUD()
                                .post(link: AppLink.updateRides, payload: {
                              'id': controller.rideId,
                              'rideTimeStart': DateTime.now().toString(),
                              'status': 'Applied'
                            });
                            controller.driverArrivePassenger();
                            // Send notification to driver to alert him that trip is begin
                            FirebaseMessagesController()
                                .sendNotificationToAnyWithoutData(
                              'BeginTrip',
                              box.read(BoxName.name).toString(),
                              controller.driverToken.toString(),
                            );
                            print(controller.driverToken.toString());
                            // Get.defaultDialog(
                            //   title: 'The Ride is Begin'.tr,
                            //   backgroundColor: AppColor.greenColor,
                            // );
                          })
                      : const SizedBox()
                ],
              ),
            ),
          ),
        );
      } else {
        return const SizedBox();
      }
    });
  }
}
