import 'dart:collection';

import 'package:anyway/constants.dart';
import 'package:anyway/modules/themed_marker.dart';
import 'package:flutter/material.dart';
import 'package:anyway/structs/landmark.dart';
import 'package:anyway/structs/trip.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:widget_to_marker/widget_to_marker.dart';


class CurrentTripMap extends StatefulWidget {

  final Trip? trip;

  CurrentTripMap({
    this.trip
  });

  @override
  State<CurrentTripMap> createState() => _CurrentTripMapState();
}

class _CurrentTripMapState extends State<CurrentTripMap> {
  late GoogleMapController mapController;

  CameraPosition _cameraPosition = CameraPosition(
    target: LatLng(48.8566, 2.3522),
    zoom: 11.0,
  );
  Set<Marker> mapMarkers = <Marker>{};
  

  void _onMapCreated(GoogleMapController controller) async {
    mapController = controller;
    List<double>? newLocation = widget.trip?.landmarks.firstOrNull?.location;
    if (newLocation != null) {
      CameraUpdate update = CameraUpdate.newLatLng(LatLng(newLocation[0], newLocation[1]));
      controller.moveCamera(update);
    }
    setMapMarkers();
  }

  void _onCameraIdle() {
    // print(mapController.getLatLng(ScreenCoordinate(x: 0, y: 0)));
  }


  void setMapMarkers() async {
    List<Landmark> landmarks = widget.trip?.landmarks.toList() ?? [];
    Set<Marker> newMarkers = <Marker>{};
    for (int i = 0; i < landmarks.length; i++) {
      Landmark landmark = landmarks[i];
      List<double> location = landmark.location;
      Marker marker = Marker(
        markerId: MarkerId(landmark.uuid),
        position: LatLng(location[0], location[1]),
        icon: await ThemedMarker(landmark: landmark, position: i).toBitmapDescriptor(
          logicalSize: const Size(150, 150),
          imageSize: const Size(150, 150)
        ),
      );
      newMarkers.add(marker);
    }
    setState(() {
      mapMarkers = newMarkers;
    });
  }


  @override
  Widget build(BuildContext context) {
    widget.trip?.addListener(setMapMarkers);
    Future<SharedPreferences> preferences = SharedPreferences.getInstance();

    return FutureBuilder(
      future: preferences,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          SharedPreferences prefs = snapshot.data as SharedPreferences;
          bool useLocation = prefs.getBool('useLocation') ?? true;
          return _buildMap(useLocation);
        } else {
          return const CircularProgressIndicator();
        }
      }
    );
  }

  Widget _buildMap(bool useLocation) {
    return GoogleMap(
      onMapCreated: _onMapCreated,
      initialCameraPosition: _cameraPosition,
      onCameraIdle: _onCameraIdle,
      // onLongPress: ,
      markers: mapMarkers,
      cloudMapId: MAP_ID,
      mapToolbarEnabled: false,
      zoomControlsEnabled: false,
      myLocationEnabled: useLocation,
      myLocationButtonEnabled: false,
    );
  }

}
