package com.cloudsolutions.HMGPatientApp.PermissionManager;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.LocationManager;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.peng.pennavmap.managers.permissions.managers.BgLocationManager;

public class HostGpsStateManager {
    private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;


    public boolean checkGPSEnabled(Activity activity) {
        LocationManager gpsStateManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
        return gpsStateManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    public static boolean isGpsGranted(Activity activity) {
        return BgLocationManager.isLocationBackgroundGranted(activity)
                || ContextCompat.checkSelfPermission(
                activity,
                Manifest.permission.ACCESS_FINE_LOCATION
        ) == PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(
                activity,
                Manifest.permission.ACCESS_COARSE_LOCATION
        ) == PackageManager.PERMISSION_GRANTED;
    }


    /**
     * Checks if the location permission is granted.
     *
     * @param activity the Activity context
     * @return true if permission is granted, false otherwise
     */
    public static boolean isLocationPermissionGranted(Activity activity) {
        return ContextCompat.checkSelfPermission(
                activity,
                Manifest.permission.ACCESS_FINE_LOCATION
        ) == PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(
                        activity,
                        Manifest.permission.ACCESS_COARSE_LOCATION
                ) == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * Requests the location permission.
     *
     * @param activity the Activity context
     */
    public static void requestLocationPermission(Activity activity) {
        ActivityCompat.requestPermissions(
                activity,
                new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                },
                LOCATION_PERMISSION_REQUEST_CODE
        );
    }
}
