package rasel.lunar.launcher.workers

import android.annotation.SuppressLint
import android.content.Context
import android.location.Location
import androidx.work.WorkerParameters
import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.Priority
import com.google.android.gms.tasks.CancellationTokenSource
import rasel.lunar.launcher.utils.BLog

class LocationGetter(context: Context, workerParams: WorkerParameters) : BaseGetter(context, workerParams) {
    companion object {
        val TAG = "LocationGetter"
        var longitude: Double? = null
        var latitude: Double? = null
    }

    @SuppressLint("MissingPermission")
    override fun realWork(): Result {
        BLog.LOGE("${OpenWeatherGetter.TAG} realWork()")

        LocationServices.getFusedLocationProviderClient(this.applicationContext)
            .getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, CancellationTokenSource().token)
            .addOnSuccessListener{ success: Location? ->
                success?.let {
                    BLog.LOGE("Location >>> $it")
                    BLog.LOGE("Location >>> (latitude)${it.longitude}/(longitude)${it.latitude}")
                    longitude = it.longitude
                    latitude = it.latitude
                }
            }.addOnFailureListener{
                BLog.LOGE("Location error >>> $it")
            }

        return Result.success()
    }

}