package com.app.simplitend.apputils;

import static com.app.simplitend.locationupdates.LocationService.LOCATION_INTERVAL_BASE_TIME;
import static com.app.simplitend.locationupdates.LocationService.LOCATION_UPDATE_MIN_INTERVAL;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.app.simplitend.locationupdates.LocationService;

public class BootCompleteReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {

            if (!AppUtil.isPatientLoggedIn(context)){
                // no senior is logged in
                return;
            }

            AppUtil.updateSeniorOutOfGeofence(context, false);

            // starting over the Location updates service again
            Intent locationUpdateIntent = new Intent(context, LocationService.class);
            locationUpdateIntent.setAction(LocationService.ACTION_START_LOCATION_UPDATES);
            locationUpdateIntent.putExtra(LOCATION_UPDATE_MIN_INTERVAL, LOCATION_INTERVAL_BASE_TIME);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(locationUpdateIntent);
            } else {
                context.startService(locationUpdateIntent);
            }

        }
    }

}
