package com.renovapp.app.notification;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import com.renovapp.app.LoginActivity;
import com.renovapp.app.R;
import com.renovapp.app.scraper.Book;

/**
 * Created by pablohenrique on 11/16/14.
 */
public class NotificationPublishReceiver extends BroadcastReceiver {

    public static final String EXTRA_BOOK = "EXTRA_BOOK";

    public void onReceive(Context context, Intent intent) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");

        Book b = (Book) intent.getSerializableExtra(EXTRA_BOOK);

        wl.acquire();
        this.callNotification(context, b);
        wl.release();

    }

    private void callNotification(Context context, Book b){
        int notificationId = b.getNotificationId();

        Intent login = new Intent(context, LoginActivity.class);
        PendingIntent loginPendingIntent = PendingIntent.getActivity(context, 0, login, 0);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("Renove seu livro")
                        .setContentText(b.getTitle())
                        .setContentIntent(loginPendingIntent);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(notificationId, notificationBuilder.build());
    }
}

@@ -164,32 +164,94 @@
 content.Intent;%0A
+import android.media.RingtoneManager;%0Aimport android.net.Uri;%0A
 import android.o
@@ -1322,24 +1322,114 @@
 login, 0);%0A%0A
+        Uri soundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);%0A%0A
         Noti
@@ -1529,16 +1529,131 @@
 ontext)%0A
+                        .setPriority(NotificationCompat.PRIORITY_HIGH)%0A                        .setSound(soundURI)%0A
         
@@ -1751,26 +1751,20 @@
 tle(
-%22Renove seu livro%22
+b.getTitle()
 )%0A  
@@ -1801,28 +1801,47 @@
 entText(
-b.getTitle()
+%22Livro com vencimento pr%C3%B3ximo.%22
 )%0A      

change title and text. add notification sound