import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:vibration/vibration.dart';

import '../../../../constant/colors.dart';
import '../../../../constant/style.dart';
import '../../../../controller/home/captin/home_captain_controller.dart';
import '../../../../controller/home/captin/map_driver_controller.dart';
import '../../../widgets/elevated_btn.dart';

GetBuilder<MapDriverController> driverEndRideBar() {
  return GetBuilder<MapDriverController>(
      builder: (mapDriverController) => mapDriverController.isRideStarted
          ? Positioned(
              left: 5,
              top: 5,
              right: 5,
              child: Container(
                decoration: AppStyle.boxDecoration1,
                height: mapDriverController.remainingTimeTimerRideBegin < 60
                    ? mapDriverController.driverEndPage = 190
                    : mapDriverController.carType == 'Mashwari'
                        ? 120
                        : 170,
                // width: 240,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    mapDriverController.carType != 'Mashwari'
                        ? Row(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: [
                              Column(
                                children: [
                                  const Icon(Icons.social_distance),
                                  Text(
                                    '${mapDriverController.distance}  ${'KM'.tr}',
                                    style: AppStyle.title,
                                  ),
                                ],
                              ),
                              Column(
                                children: [
                                  const Icon(Icons.timelapse),
                                  Text(
                                      mapDriverController.hours > 1
                                          ? '${'${'Your Ride Duration is '.tr}${mapDriverController.hours}${' H and'.tr}'} ${mapDriverController.minutes} m'
                                          : '${'Your Ride Duration is '.tr} ${mapDriverController.minutes} ${'m'.tr}',
                                      style: AppStyle.title),
                                ],
                              ),
                              Column(
                                children: [
                                  const Icon(Icons.money_sharp),
                                  Text(
                                      '${mapDriverController.paymentAmount}  ${'\$'.tr}',
                                      style: AppStyle.title),
                                ],
                              ),
                            ],
                          )
                        : const SizedBox(),
                    mapDriverController.carType != 'Speed' &&
                            mapDriverController.carType != 'Delivery'
                        ? Row(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: [
                              Container(
                                  width: Get.width * .2,
                                  decoration: AppStyle.boxDecoration1,
                                  child: Padding(
                                    padding: const EdgeInsets.all(4),
                                    child: Row(
                                      children: [
                                        const Icon(Icons.timer),
                                        Text(
                                          mapDriverController
                                              .stringRemainingTimeRideBegin1,
                                          style: AppStyle.number,
                                        ),
                                      ],
                                    ),
                                  )),
                              Container(
                                width: Get.width * .2,
                                decoration: AppStyle.boxDecoration1,
                                child: Padding(
                                  padding: const EdgeInsets.all(4),
                                  child: Row(
                                    children: [
                                      const Icon(Icons.location_on),
                                      Text(
                                        '${mapDriverController.recentDistanceToDash.toStringAsFixed(0)} ${'KM'.tr}',
                                        style: AppStyle.number,
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                              Container(
                                width: Get.width * .2,
                                decoration: AppStyle.boxDecoration1,
                                child: Padding(
                                  padding: const EdgeInsets.all(4.0),
                                  child: Row(
                                    children: [
                                      const Icon(Icons.attach_money),
                                      Text(
                                        mapDriverController.price
                                            .toStringAsFixed(2),
                                        style: AppStyle.number,
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            ],
                          )
                        : const SizedBox(),
                    (mapDriverController.carType == 'Mashwari' ||
                                mapDriverController.carType == 'Comfort') &&
                            mapDriverController.remainingTimeTimerRideBegin > 60
                        ? Row(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: [
                              MyElevatedButton(
                                title: 'End Ride'.tr,
                                onPressed: () {
                                  mapDriverController.finishRideFromDriver();
                                },
                                kolor: AppColor.redColor,
                              ),
                              Container(
                                decoration: AppStyle.boxDecoration1,
                                child: InkWell(
                                  onTap: () {
                                    print(Get.find<HomeCaptainController>()
                                        .comfortPrice);
                                  },
                                  child: Text(
                                    mapDriverController.carType,
                                    style: AppStyle.title,
                                  ),
                                ),
                              )
                            ],
                          )
                        : const SizedBox(),
                    mapDriverController.remainingTimeTimerRideBegin < 60
                        ? Row(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: [
                              MyElevatedButton(
                                title: 'End Ride'.tr,
                                onPressed: () {
                                  mapDriverController.finishRideFromDriver();
                                },
                                kolor: AppColor.redColor,
                              ),
                              Container(
                                decoration: AppStyle.boxDecoration1,
                                child: Text(
                                  mapDriverController.carType,
                                  style: AppStyle.title,
                                ),
                              )
                            ],
                          )
                        : const SizedBox(),
                    mapDriverController.carType != 'Comfort' &&
                            mapDriverController.carType != 'Mashwari'
                        ? Stack(
                            children: [
                              SizedBox(
                                width: Get.width * .9,
                                child: LinearProgressIndicator(
                                  backgroundColor: AppColor.accentColor,
                                  color: mapDriverController
                                              .remainingTimeTimerRideBegin <
                                          60
                                      ? AppColor.redColor
                                      : AppColor.greenColor,
                                  minHeight: 25,
                                  borderRadius: BorderRadius.circular(6),
                                  value: mapDriverController
                                      .progressTimerRideBegin
                                      .toDouble(),
                                ),
                              ),
                              Center(
                                child: Text(
                                  mapDriverController
                                      .stringRemainingTimeRideBegin,
                                  style: AppStyle.title,
                                ),
                              )
                            ],
                          )
                        : const SizedBox(),
                  ],
                ),
              ))
          : const SizedBox());
}

GetBuilder<MapDriverController> speedCircle() {
  if (Get.find<MapDriverController>().speed > 100) {
    if (Platform.isIOS) {
      HapticFeedback.selectionClick();
    } else {
      Vibration.vibrate(duration: 1000);
    }
    Get.defaultDialog(
      barrierDismissible: false,
      titleStyle: AppStyle.title,
      title: 'Speed Over'.tr,
      middleText: 'Please slow down'.tr,
      middleTextStyle: AppStyle.title,
      confirm: MyElevatedButton(
        title: 'I will slow down'.tr,
        onPressed: () => Get.back(),
      ),
    );
  }
  return GetBuilder<MapDriverController>(
    builder: (mapDriverController) {
      return mapDriverController.isRideStarted
          ? Positioned(
              bottom: 25,
              right: 100,
              child: Container(
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: mapDriverController.speed > 100
                      ? Colors.red
                      : AppColor.secondaryColor,
                  border: Border.all(width: 3, color: AppColor.redColor),
                ),
                height: 60,
                width: 60,
                child: Center(
                  child: Text(
                    mapDriverController.speed.toStringAsFixed(0),
                    style: AppStyle.number,
                  ),
                ),
              ),
            )
          : const SizedBox();
    },
  );
}
