package com.isidroid.location.repository

import android.annotation.SuppressLint
import android.content.Context
import androidx.compose.ui.text.intl.Locale
import com.google.android.gms.location.LocationServices
import com.google.android.gms.tasks.Tasks
import com.isidroid.location.ext.calculateDistanceBetweenPoints
import timber.log.Timber

class LocationRepositoryImpl(private val context: Context) : LocationRepository {

    @SuppressLint("MissingPermission")
    override fun getCurrentLocation(): Pair<Double, Double> {
        val client = LocationServices.getFusedLocationProviderClient(context)

        return with(Tasks.await(client.lastLocation)) { Pair(latitude, longitude) }
    }

    override fun findDistance(myLat: Double, myLng: Double, locationLat: Double?, locationLng: Double?): Float? {
        if (locationLat == null || locationLng == null) return null
        val result = calculateDistanceBetweenPoints(Locale.Companion.current, myLat, myLng, locationLat, locationLng)

        return result.toFloat()
    }
}