import 'dart:io';

import 'package:Intaleq/controller/home/trip_monitor_controller.dart';
import 'package:Intaleq/views/widgets/my_scafold.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:vibration/vibration.dart';

import '../../../../constant/colors.dart';
import '../../../../constant/style.dart';
import '../../../widgets/elevated_btn.dart';

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

  @override
  Widget build(BuildContext context) {
    final params = Get.parameters;
    // Use params to initialize your controller or pass data
    Get.put(TripMonitorController()).init();
    return GetBuilder<TripMonitorController>(builder: (tripMonitorController) {
      return MyScafolld(
        title: 'Trip Monitor'.tr,
        body: [
          GoogleMap(
            onMapCreated: tripMonitorController.onMapCreated,
            initialCameraPosition: CameraPosition(
              // bearing: 45,
              target: tripMonitorController.parentLocation,
              zoom: 16,
              tilt: 40,
            ),
            // onCameraMove: (position) {},
            markers: {
              Marker(
                markerId: MarkerId('start'.tr),
                position: tripMonitorController.parentLocation,
                draggable: true,
                icon: tripMonitorController.tripData['message'][0]['model']
                        .contains('دراجة')
                    ? tripMonitorController.motoIcon
                    : tripMonitorController.tripData['message'][0]['model']
                                ['gender'] ==
                            'Male'
                        ? tripMonitorController.carIcon
                        : tripMonitorController.ladyIcon,
                rotation: tripMonitorController.rotation,
              ),
            },
          ),
          speedCircle()
        ],
        isleading: true,
      );
    });
  }
}

GetBuilder<TripMonitorController> speedCircle() {
  if (Get.find<TripMonitorController>().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<TripMonitorController>(
    builder: (tripMonitorController) {
      return Positioned(
        bottom: 25,
        right: 100,
        child: Container(
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            color: tripMonitorController.speed > 100
                ? Colors.red
                : AppColor.secondaryColor,
            border: Border.all(width: 3, color: AppColor.redColor),
          ),
          height: 60,
          width: 60,
          child: Center(
            child: Text(
              tripMonitorController.speed.toStringAsFixed(0),
              style: AppStyle.number,
            ),
          ),
        ),
      );
    },
  );
}
