package com.sigmobile.dawebmail.services;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.NotificationCompat;
import android.text.Html;

import com.sigmobile.dawebmail.LoginActivity;
import com.sigmobile.dawebmail.R;
import com.sigmobile.dawebmail.database.EmailMessage;
import com.sigmobile.dawebmail.database.User;
import com.sigmobile.dawebmail.database.UserSettings;

import java.util.ArrayList;


/**
 * Created by rish on 6/10/15.
 */
public class NotificationMaker {

    public static void showNotification(Context context, User user, String fromName, String subject) {

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setSmallIcon(R.drawable.envelope_m);
        mBuilder.setTicker(context.getString(R.string.notification_ticker_new_webmail));
        if (User.getUsersCount() > 1)
            mBuilder.setContentTitle(fromName + " to " + user.username);
        mBuilder.setContentText(subject);
        mBuilder.setSound(Uri.parse(UserSettings.getNotificationSound(context)));
        mBuilder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
        mBuilder.setAutoCancel(true);

        Intent notificationIntent = new Intent(context, LoginActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(LoginActivity.class);

        stackBuilder.addNextIntent(notificationIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(1002, mBuilder.build());
    }

    public static void sendInboxNotification(int numberToShow, User user, Context context, ArrayList<EmailMessage> newEmails) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, LoginActivity.class), 0);
            Notification.Builder mBuilder = new Notification.Builder(context)
                    .setContentTitle(context.getString(R.string.notification_ticker_new_webmails))
                    .setTicker(context.getString(R.string.notification_ticker_new_webmails))
                    .setContentText(context.getString(R.string.notification_swipe_to_view))
                    .setAutoCancel(true)
                    .setSmallIcon(R.drawable.envelope_m);

            if (User.getUsersCount() > 1)
                mBuilder.setContentTitle(context.getString(R.string.notification_ticker_new_webmails) + " for " + user.username);

            Notification.InboxStyle notification = null;
            notification = new Notification.InboxStyle(mBuilder);

            for (int i = 0; i < numberToShow; i++) {
                String emailFrom = newEmails.get(newEmails.size() - 1 - i).fromName;
                String emailSubject = newEmails.get(newEmails.size() - 1 - i).subject;
                String htmlText = "<b>" + emailFrom + "</b> " + emailSubject;
                notification.addLine(Html.fromHtml(htmlText));
            }

            Intent resultIntent = new Intent(context, LoginActivity.class);

            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(LoginActivity.class);

            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

            mBuilder.setContentIntent(resultPendingIntent);
            mBuilder.setSound(Uri.parse(UserSettings.getNotificationSound(context)));
            mBuilder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            mNotificationManager.notify(1003, mBuilder.build());
        }
    }

    public static void cancelNotification(Context context) {
        NotificationManager nMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        nMgr.cancelAll();
    }
}
@@ -619,17 +619,16 @@
 yList;%0A%0A
-%0A
 /**%0A * C
@@ -795,17 +795,16 @@
 ject) %7B%0A
-%0A
         
@@ -1021,24 +1021,65 @@
 _webmail));%0A
+        String username = user.username;%0A
         if (
@@ -1095,32 +1095,34 @@
 sersCount() %3E 1)
+ %7B
 %0A            mBu
@@ -1122,68 +1122,332 @@
     
-mBuilder.setContentTitle(fromName + %22 to %22 + user.username);
+if (username.indexOf(%22@%22) != -1)%0A                mBuilder.setContentTitle(fromName + %22 to %22 + username.substring(0, username.indexOf(%22@%22)));%0A            else%0A                mBuilder.setContentTitle(fromName + %22 to %22 + username);%0A        %7D else %7B%0A            mBuilder.setContentTitle(fromName + %22 to %22 + username);%0A        %7D
 %0A   
@@ -2304,30 +2304,113 @@
 ify(
-1002, mBuilder.build()
+(int) (System.currentTimeMillis()), mBuilder.build());%0A%0A        UserSettings.setCurrentUser(user, context
 );%0A 
@@ -3254,16 +3254,18 @@
 t() %3E 1)
+ %7B
 %0A       
@@ -3277,121 +3277,418 @@
     
-mBuilder.setContentTitle(context.getString(R.string.notification_ticker_new_webmails) + %22 for %22 + user.username);
+String username = user.username;%0A                if (username.indexOf(%22@%22) != -1)%0A                    mBuilder.setContentTitle(context.getString(R.string.notification_ticker_new_webmails) + %22 for %22 + username.substring(0, username.indexOf(%22@%22)));%0A                else%0A                    mBuilder.setContentTitle(context.getString(R.string.notification_ticker_new_webmails) + %22 for %22 + username);%0A            %7D
 %0A%0A  
@@ -4998,30 +4998,115 @@
 ify(
-1003, mBuilder.build()
+(int) System.currentTimeMillis(), mBuilder.build());%0A%0A            UserSettings.setCurrentUser(user, context
 );%0A 

Edit NotifMaker