package com.abbidot.tracker.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Looper;

import androidx.annotation.NonNull;

import com.abbidot.baselibrary.util.LogUtil;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;

/**
 * Created  by .yzq on 2023/10/18/018.
 *
 * @link <a href="https://www.cnblogs.com/CharlesGrant/p/8242413.html"/>
 * @description: 谷歌定位，暂未使用
 */
public class GoogleLocationUtil {
    private LocationRequest mLocationRequest;

    @SuppressLint("MissingPermission")
    public GoogleLocationUtil(Context context) {
        createLocationRequest();

        FusedLocationProviderClient fusedLocationClient =
                LocationServices.getFusedLocationProviderClient(context);

        LogUtil.Companion.e("GoogleLocationUtil");

        // LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        //
        // builder.addLocationRequest(mLocationRequest);

        fusedLocationClient.requestLocationUpdates(mLocationRequest, new LocationCallback() {
            @Override
            public void onLocationResult(@NonNull LocationResult locationResult) {
                super.onLocationResult(locationResult);
                LogUtil.Companion.e("onLocationResult" + locationResult.getLastLocation());
            }
        }, Looper.myLooper());

        fusedLocationClient.getLastLocation().addOnSuccessListener((Activity) context, location -> {
            if (location != null) {
                // Logic to handle location object
                LogUtil.Companion.e("addOnSuccessListener" + location.getLatitude());
            } else {
                // Handle null case or Request periodic location update https://developer.android.com/training/location/receive-location-updates
                LogUtil.Companion.e("addOnSuccessListener,location == null");
            }
        });

    }


    private void createLocationRequest() {
        mLocationRequest = LocationRequest.create();
        //更新间隔
        mLocationRequest.setInterval(2000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    }

}
