package com.example.daniel.todule_android.activities;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.example.daniel.todule_android.R;
import com.example.daniel.todule_android.provider.ToduleDBContract;
import com.example.daniel.todule_android.utilities.NotificationHelper;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * Created by danieL on 10/10/2017.
 */

public class NotificationReceiver extends BroadcastReceiver {

    String CHANNEL_ID = "todule_channel";

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent != null) {
            String action = intent.getAction();
            if (action != null) {

                if (action.equals("android.intent.action.BOOT_COMPLETED") || action.equals("android.intent.action.REBOOT")) {
                    Log.d("BroadCast Received", "ON BOOT COMPLETE");
                    String select = ToduleDBContract.TodoNotification.COLUMN_NAME_REMINDER_CANCELED + " = ?";
                    String[] selectionArgs = {String.valueOf(ToduleDBContract.TodoNotification.REMINDER_NOT_CANCELED)};
                    Cursor cr = context.getContentResolver().query(ToduleDBContract.TodoNotification.CONTENT_URI, null, select, selectionArgs, null);
                    try {
                        while (cr.moveToNext()) {
                            long toduleId = cr.getLong(cr.getColumnIndexOrThrow(ToduleDBContract.TodoNotification.COLUMN_NAME_TODULE_ID));
                            int reminderTime = cr.getInt(cr.getColumnIndexOrThrow(ToduleDBContract.TodoNotification.COLUMN_NAME_REMINDER_TIME));
                            Uri toduleUri = ContentUris.withAppendedId(ToduleDBContract.TodoNotification.CONTENT_ID_URI_BASE, toduleId);
                            NotificationHelper.setReminder(context, toduleUri, reminderTime);
                        }
                    } finally {
                        cr.close();
                    }
                } else if (action.equals("com.example.daniel.todule_android.REMINDER_NOTIFICATION")) {
                    long entryId = intent.getLongExtra("todule_id", -1L);

                    // Intent to redirect user to app when notification is clicked
                    Intent notificationIntent = new Intent(context, MainActivity.class);
                    notificationIntent.putExtra("todule_id", entryId);
                    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    PendingIntent resultPendingIntent =
                            PendingIntent.getActivity(
                                    context,
                                    0,
                                    notificationIntent,
                                    PendingIntent.FLAG_UPDATE_CURRENT
                            );

                    // Intent to delete reminder after notification is canceled
                    Intent notificationDeleteIntent = new Intent(context, getClass());
                    notificationDeleteIntent.setAction("com.example.daniel.todule_android.DELETE_REMINDER");
                    notificationDeleteIntent.putExtra("todule_id", entryId);

                    PendingIntent notifDeletePendingIntent =
                            PendingIntent.getBroadcast(
                                    context,
                                    0,
                                    notificationDeleteIntent,
                                    PendingIntent.FLAG_CANCEL_CURRENT
                            );

                    // Construct notification
                    NotificationCompat.Builder mBuilder =
                            new NotificationCompat.Builder(context, CHANNEL_ID)
                                    .setSmallIcon(R.drawable.ic_stat_todule)
                                    .setContentTitle("Reminder: " + intent.getStringExtra("todule_title"))
                                    .setContentText(intent.getStringExtra("todule_due_date"))
                                    .setContentIntent(resultPendingIntent)
                                    .setDeleteIntent(notifDeletePendingIntent)
                                    .setAutoCancel(true);

                    mBuilder.setDefaults(Notification.DEFAULT_ALL);

                    // Gets an instance of the NotificationManager service
                    NotificationManager mNotifyMgr =
                            (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

                    // Builds the notification and issues it.
                    mNotifyMgr.notify((int) entryId, mBuilder.build());

                } else if(action.equals("com.example.daniel.todule_android.DELETE_REMINDER")) {
                    long toduleId = intent.getLongExtra("todule_id", -1L);
                    NotificationHelper.cancelReminder(context, toduleId);
                }
            }

        }
    }
}

@@ -233,16 +233,54 @@
 ntUris;%0A
+import android.content.ContentValues;%0A
 import a
@@ -3986,32 +3986,723 @@
             );%0A%0A
+                    // Intent to mark todule as done in notification%0A                    Intent markToduleDoneIntent = new Intent(context, getClass());%0A                    markToduleDoneIntent.setAction(%22com.example.daniel.todule_android.TODULE_DONE%22);%0A                    markToduleDoneIntent.putExtra(%22todule_id%22, entryId);%0A%0A                    PendingIntent markToduleDonePendingIntent =%0A                            PendingIntent.getBroadcast(%0A                                    context,%0A                                    0,%0A                                    markToduleDoneIntent,%0A                                    PendingIntent.FLAG_CANCEL_CURRENT%0A                            );%0A%0A
                 
@@ -4723,32 +4723,32 @@
 ct notification%0A
-
                 
@@ -5427,16 +5427,132 @@
 LT_ALL);
+%0A                    mBuilder.addAction(R.drawable.ic_done_white_18dp, %22Mark as done%22, markToduleDonePendingIntent);
 %0A%0A      
@@ -6074,24 +6074,24 @@
 _id%22, -1L);%0A
-
             
@@ -6144,32 +6144,903 @@
 ext, toduleId);%0A
+                %7D else if(action.equals(%22com.example.daniel.todule_android.TODULE_DONE%22))%7B%0A                    long toduleId = intent.getLongExtra(%22todule_id%22, -1L);%0A                    NotificationManager mNotifyMgr =%0A                            (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);%0A                    mNotifyMgr.cancel((int) toduleId);%0A                    Uri toduleUri = ContentUris.withAppendedId(ToduleDBContract.TodoEntry.CONTENT_ID_URI_BASE, toduleId);%0A                    ContentValues cv = new ContentValues();%0A                    cv.put(ToduleDBContract.TodoEntry.COLUMN_NAME_TASK_DONE, ToduleDBContract.TodoEntry.TASK_COMPLETED);%0A                    cv.put(ToduleDBContract.TodoEntry.COLUMN_NAME_COMPLETED_DATE, System.currentTimeMillis());%0A                    context.getContentResolver().update(toduleUri, cv , null, null);%0A
                 

Add 'mark as done' action in reminder notification