import 'package:siro_rider/print.dart';
import 'package:flutter/material.dart';
import 'package:siro_rider/env/env.dart';
import 'package:get/get.dart';
import 'package:intaleq_maps/intaleq_maps.dart';
import 'package:siro_rider/controller/home/points_for_rider_controller.dart';
import 'package:siro_rider/services/offline_map_service.dart';

import '../../../controller/home/map/location_search_controller.dart';
import '../../../controller/home/map/map_engine_controller.dart';
import '../../../controller/home/map/nearby_drivers_controller.dart';
import '../../../controller/home/map/ride_lifecycle_controller.dart';
import '../../widgets/mycircular.dart';
import '../../widgets/mydialoug.dart';

class GoogleMapPassengerWidget extends StatelessWidget {
  GoogleMapPassengerWidget({super.key});

  final WayPointController wayPointController = Get.find<WayPointController>();

  @override
  Widget build(BuildContext context) {
    final locationSearch = Get.find<LocationSearchController>();
    final rideLifecycle = Get.find<RideLifecycleController>();
    final nearbyDrivers = Get.find<NearbyDriversController>();

    return GetBuilder<MapEngineController>(
      builder: (controller) => rideLifecycle.isLoading
          ? const MyCircularProgressIndicator()
          : Positioned(
              bottom: Get.height * .2,
              top: 0,
              left: 0,
              right: 0,
              child: IntaleqMap(
                apiKey: Env.mapSaasKey,
                styleUrl: Get.isDarkMode
                    ? 'assets/style_dark.json'
                    : 'assets/style.json',
                onMapCreated: controller.onMapCreated,
                onStyleLoaded: controller.onStyleLoaded,
                onCameraMove: locationSearch.onCameraMoveThrottled,
                onCameraIdle: () {
                  if (controller.mapController != null) {
                    final position = controller.mapController!.cameraPosition;
                    if (position != null) {
                      Log.print('✅ onCameraIdle targeted: ${position.target}');
                      locationSearch
                          .updateCurrentLocationFromCamera(position.target);
                      OfflineMapService.instance
                          .downloadRegion(position.target, radiusKm: 1.0);
                    } else {
                      Log.print('⚠️ onCameraIdle: cameraPosition is NULL');
                    }
                  } else {
                    Log.print('⚠️ onCameraIdle: mapController is NULL');
                  }
                },
                markers: controller.markers,
                polylines: controller.polyLines,
                polygons: controller.polygons,
                circles: controller.circles,
                initialCameraPosition: CameraPosition(
                  target: locationSearch.passengerLocation,
                  zoom: nearbyDrivers.lowPerf ? 14.5 : 15,
                ),
                myLocationEnabled: true,
                onTap: (latlng) => controller.hidePlaces(),
                onLongPress: (latlng) {
                  MyDialog().getDialog('Are you want to go to this site'.tr, '',
                      () async {
                    controller.clearPolyline();
                    rideLifecycle.getDirectionMap(
                      '${locationSearch.passengerLocation.latitude},${locationSearch.passengerLocation.longitude}',
                      '${latlng.latitude},${latlng.longitude}',
                    );
                    rideLifecycle.showBottomSheet1();
                  });
                },
              ),
            ),
    );
  }
}
