package com.aplusinformatique.aplusmanager.Classes;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.aplusinformatique.aplusmanager.BuildConfig;
import com.aplusinformatique.aplusmanager.Services.DebutGeolocalisationV3;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingEvent;

import java.util.List;

/**
 * Receiver for geofence transition changes.
 * <p>
 * Receives geofence transition events from Location Services in the form of an Intent containing
 * the transition type and geofence id(s) that triggered the transition. Creates a JobIntentService
 * that will handle the intent in the background.
 */
public class GeofenceBroadcastReceiver extends BroadcastReceiver {

    public final static String ACTION_GEOFENCE_TRIGGER = "com.aplusinformatique.aplusmanager.geofenceTrigger";
    private static final String TAG = "GeofenceBR";

    public static void sendBroadcastGeofence(Context context, int geofenceTransition, List<Geofence> triggeringGeofences) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "sendBroadcastGeofence ");
        }

        for (Geofence currentGeofence : triggeringGeofences) {
            DebutGeolocalisationV3.sendGeofenceTriggered(context, geofenceTransition, currentGeofence.getRequestId(), -1,-1);
        }
    }

    /**
     * Receives incoming intents.
     *
     * @param context the application context.
     * @param intent  sent by Location Services. This Intent is provided to Location
     *                Services (inside a PendingIntent) when addGeofences() is called.
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        if(BuildConfig.DEBUG){
            Log.d(TAG, "onReceive intent " + intent.toString());
        }


        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
        if (geofencingEvent.hasError()) {
//            String errorMessage = GeofenceErrorMessages.getErrorString(this,geofencingEvent.getErrorCode());
            String errorMessage = Integer.toString(geofencingEvent.getErrorCode());
            Log.e(TAG, errorMessage);
            return;
        }

        if (BuildConfig.DEBUG) {
            Log.d(TAG, "onHandleIntent INTENT OK " + geofencingEvent.toString());
        }

        // Get the transition type.
        int geofenceTransition = geofencingEvent.getGeofenceTransition();

        // Test that the reported transition was of interest.
        if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER || geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) { //

            // Get the geofences that were triggered. A single event can trigger
            // multiple geofences.
            List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

            // Get the transition details as a String.
//            String geofenceTransitionDetails = getGeofenceTransitionDetails(
//                    context,
//                    geofenceTransition,
//                    triggeringGeofences
//            );

            // Send notification and log the transition details.
//            sendNotification(geofenceTransitionDetails);
            //NewMessageNotification.notify(context, geofenceTransitionDetails);

            sendBroadcastGeofence(context, geofenceTransition, triggeringGeofences);

            //Log.i(TAG, geofenceTransitionDetails);
        } else {
            // Log the error.
//            Log.e(TAG, getString(R.string.geofence_transition_invalid_type,geofenceTransition));
            Log.e(TAG, "onHandleIntent ERROR");

        }
    }

//    private String getGeofenceTransitionDetails(Context context, int geofenceTransition, List<Geofence> triggeringGeofences) {
//    String returnValue = "";
//
//    returnValue += (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ? "ENTER " : "EXIT ");
//    for (Geofence currentGeofence : triggeringGeofences) {
//        returnValue += currentGeofence.getRequestId() + "|";
//    }
//
//    return returnValue;
//}
}
