package com.phicdy.totoanticipation.model.scheduler;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.util.Log;

import java.util.Calendar;
import java.util.Date;

public class DeadlineAlarm {

    private final Context context;
    static final String ACTION = "deadlineAlarm";

    public DeadlineAlarm(@NonNull Context context) {
        this.context = context;
    }

    public void set5hoursBefore(@NonNull Date deadline) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(deadline);
        calendar.add(Calendar.HOUR_OF_DAY, -5);
        AlarmManager alm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Log.d("Toto alarm", "Set alarm : " + deadline.toString());
        alm.set(AlarmManager.RTC_WAKEUP, deadline.getTime(), generateIntent());
    }

    public void cancel() {
        AlarmManager alm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alm.cancel(generateIntent());
    }

    private PendingIntent generateIntent() {
        Intent i = new Intent(context, DeadlineAlarmReceiver.class);
        i.setAction(ACTION);
        return PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
    }
}

@@ -835,24 +835,34 @@
 m : %22 + 
-deadline
+calendar.getTime()
 .toStrin
@@ -912,24 +912,32 @@
 UP, 
-deadline.getTime
+calendar.getTimeInMillis
 (), 

Fix the bug that calendar was not used for alarm