import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intaleq_maps/intaleq_maps.dart';
import 'package:siro_driver/constant/api_key.dart';

import '../../../../controller/functions/location_controller.dart';
import '../../../../controller/home/captin/map_driver_controller.dart';
import '../../../../controller/profile/setting_controller.dart';

class GoogleDriverMap extends StatelessWidget {
  const GoogleDriverMap({
    super.key,
    required this.locationController,
  });

  final LocationController locationController;

  @override
  Widget build(BuildContext context) {
    final MapDriverController controller = Get.put(MapDriverController());

    // New: تحديد قيمة الـ padding لتحريك مركز الخريطة للأعلى
    final double mapPaddingBottom = MediaQuery.of(context).size.height * 0.3;

    return GetBuilder<MapDriverController>(
      builder: (controller) => Listener(
        onPointerDown: (_) => controller.onUserMapInteraction(),
        child: IntaleqMap(
          apiKey: AK.mapAPIKEY,
          onMapCreated: (mapController) {
            controller.onMapCreated(mapController);
          },
          mapType: Get.isRegistered<SettingController>()
              ? (Get.find<SettingController>().isMapDarkMode
                  ? IntaleqMapType.normal
                  : IntaleqMapType.light)
              : IntaleqMapType.light,
          zoomControlsEnabled: false,
          initialCameraPosition: CameraPosition(
            target:
                controller.smoothedLocation ?? locationController.myLocation,
            zoom: 17,
            bearing: controller.smoothedHeading,
            tilt: 60,
          ),
          // padding: EdgeInsets.only(bottom: 50, top: Get.height * 0.7),
          // minMaxZoomPreference: const MinMaxZoomPreference(8, 18),
          myLocationEnabled: false,
          myLocationButtonEnabled: false,
          compassEnabled: false,
          polylines: controller.polyLines.toSet(),
          markers: {
            // 🔥 Car icon — always visible, moves with GPS location on map.
            // MarkerId matches exactly with updateMarker() in controller.
            Marker(
              markerId: const MarkerId('MyLocation'),
              position: controller.smoothedLocation ?? controller.myLocation,
              rotation: controller.smoothedHeading,
              flat: true,
              anchor: const Offset(0.5, 0.5),
              icon: controller.carIcon,
              zIndex: 100,
            ),
            if (!controller.isRideStarted &&
                controller.latLngPassengerLocation.latitude != 0)
              Marker(
                markerId: const MarkerId('start'),
                position: controller.latLngPassengerLocation,
                icon: controller.startIcon,
              ),
            if (!controller.isRideStarted &&
                controller.upcomingPathPoints.isNotEmpty &&
                controller.walkIcon != null)
              Marker(
                markerId: const MarkerId('walk_end_marker'),
                position: controller.upcomingPathPoints.last,
                icon: controller.walkIcon!,
                anchor: const Offset(0.5, 0.5),
              ),
            if (controller.latLngPassengerDestination.latitude != 0 ||
                controller.latLngPassengerDestination.longitude != 0)
              Marker(
                markerId: const MarkerId('end'),
                position: controller.latLngPassengerDestination,
                icon: controller.endIcon,
              ),
          },
        ),
      ),
    );
  }
}
