import 'package:rasadyar_core/core.dart';

Future<bool> checkLocationPermission({bool request = false}) async {
  try {
    final LocationPermission permission = await Geolocator.checkPermission();

    switch (permission) {
      case LocationPermission.denied:
        final LocationPermission requestResult = await Geolocator.requestPermission();
        return requestResult != LocationPermission.denied &&
            requestResult != LocationPermission.deniedForever;

      case LocationPermission.deniedForever:
        return request ? await Geolocator.openAppSettings() : false;

      case LocationPermission.always:
      case LocationPermission.whileInUse:
        return true;

      default:
        return false;
    }
  } catch (e) {
    eLog(e);
    return await Geolocator.openLocationSettings();
  }
}

Future<Position> determineCurrentPosition() async {
  final position = await Geolocator.getCurrentPosition(
    locationSettings: AndroidSettings(accuracy: LocationAccuracy.best),
  );

  return position;
}

Future<LatLng> determineCurrentLatLng() async {
  var position = await determineCurrentPosition();
  final latLng = LatLng(position.latitude, position.longitude);

  return latLng;
}
