
import 'package:geolocator/geolocator.dart';

class GeolocationService {
  late double latitude;
  late double longitude;
  late bool hasLocation;

  

  Future<void> getCurrentLocation() async {

    bool serviceEnabled = false;
    LocationPermission permission;

    try {
      // 1. Prüfen, ob Standortdienste aktiviert sind
      serviceEnabled = await Geolocator.isLocationServiceEnabled();
      if (!serviceEnabled) {
        return Future.error('Standortdienste sind deaktiviert.');
      }

      hasLocation = serviceEnabled;

      // 2. Berechtigungen prüfen
      permission = await Geolocator.checkPermission();
      if (permission == LocationPermission.denied) {
        permission = await Geolocator.requestPermission();
        hasLocation = true;
        if (permission == LocationPermission.denied) {
          hasLocation = false;
          return Future.error('Berechtigung verweigert.');
        }
      }      

      // 3. Position abrufen
      Position position = await Geolocator.getCurrentPosition(
        locationSettings: const LocationSettings(
          accuracy: LocationAccuracy.high,
        ),
      );

      // 4. Standort über Backend-Proxy abrufen
      latitude = position.latitude;
      longitude = position.longitude;
      if(latitude > 0 && longitude > 0) {
        hasLocation = true;
      }
    } catch (e) {
      print("Fehler beim Abrufen des Standorts: $e");
      hasLocation = false;
    }
  }
}
