import 'dart:io';

import 'package:siro_rider/controller/home/trip_monitor_controller.dart';
import 'package:siro_rider/env/env.dart';
import 'package:siro_rider/views/widgets/my_scafold.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:intaleq_maps/intaleq_maps.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;
    final arguments = Get.arguments as Map<String, dynamic>?;

    // Use params or arguments to initialize your controller
    final controller = Get.put(TripMonitorController());
    controller.init(
      rideId: params['rideId'] ?? arguments?['rideId'],
      driverId: params['driverId'] ?? arguments?['driverId'],
    );
    return GetBuilder<TripMonitorController>(builder: (tripMonitorController) {
      return MyScafolld(
        title: 'Trip Monitor'.tr,
        body: [
          IntaleqMap(
            apiKey: Env.mapSaasKey,
            onMapCreated: tripMonitorController.onMapCreated,
            onStyleLoaded: controller.onStyleLoaded,
            initialCameraPosition: CameraPosition(
              target: tripMonitorController.parentLocation,
              zoom: 16,
              tilt: 40,
            ),
            markers: tripMonitorController.markers,
          ),
          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,
            ),
          ),
        ),
      );
    },
  );
}
