package org.deke.risk.riskahead.helper;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.format.DateUtils;
import android.util.Log;

import com.google.firebase.crashlytics.FirebaseCrashlytics;

/**
 *
 * (C) Copyright 2016 RiskAhead (http://www.riskahead.net)
 * Developer & Contributor: Dennis Thießen (dennis.thiessen@riskahead.net)
 * Date of Release: 10.02.2016
 *
 */
public class RABroadcastReceiver extends BroadcastReceiver {
    private final static String TAG = RABroadcastReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Log.d(TAG, "BroadcastReceiver received intent");

            Intent wtdSServiceIntent = new Intent(context, NotificationService.class);
            PendingIntent wtdSServicePendingIntent = PendingIntent.
                    getService(context, 0, wtdSServiceIntent, 0);

            long interval = DateUtils.MINUTE_IN_MILLIS * 2;
            long firstStart = System.currentTimeMillis() + interval;

            AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            if(am != null){
                am.setInexactRepeating(AlarmManager.RTC, firstStart,interval, wtdSServicePendingIntent);
            }else{
                FirebaseCrashlytics.getInstance().log("Could not get AlarmManager");
            }

            Log.v(TAG, "AlarmManager gesetzt");
        }
    }
}
