package com.growthpush.handler;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;

import com.growthbeat.utils.PermissionUtils;
import com.growthpush.GrowthPushConstants;
import com.growthpush.view.AlertActivity;
import com.growthpush.view.DialogType;

public class BaseReceiveHandler implements ReceiveHandler {

    private Callback callback = new Callback();

    public BaseReceiveHandler() {
        super();
    }

    public BaseReceiveHandler(Callback callback) {
        this();
        setCallback(callback);
    }

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

    protected void showAlert(Context context, Intent intent) {

        if (context == null || intent == null || intent.getExtras() == null)
            return;

        if (!intent.getExtras().containsKey("message") && !intent.getExtras().containsKey("dialogType"))
            return;

        if (intent.getExtras().containsKey("message")) {
            String message = intent.getExtras().getString("message");
            if (message == null || message.length() <= 0 || message.equals(""))
                return;
        }

        DialogType dialogType = DialogType.none;
        if (intent.getExtras().containsKey("dialogType")) {
            try {
                dialogType = DialogType.valueOf(intent.getExtras().getString("dialogType"));
            } catch (IllegalArgumentException e) {
            } catch (NullPointerException e) {
            }
        }

        if (dialogType == DialogType.none)
            return;

        Intent alertIntent = new Intent(context, AlertActivity.class);
        alertIntent.putExtras(intent.getExtras());
        alertIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_NEW_TASK);

        context.startActivity(alertIntent);

    }

    protected void addNotification(Context context, Intent intent) {

        if (context == null || intent == null || intent.getExtras() == null)
            return;

        if (!intent.getExtras().containsKey("message"))
            return;

        String message = intent.getExtras().getString("message");
        if (message == null || message.length() <= 0 || message.equals(""))
            return;

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify("GrowthPush" + context.getPackageName(), 1, generateNotification(context, intent.getExtras()));

    }

    private Notification generateNotification(Context context, Bundle extras) {
        PackageManager packageManager = context.getPackageManager();

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

        try {
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);

            int icon = packageManager.getApplicationInfo(context.getPackageName(), 0).icon;
            if (applicationInfo.metaData != null && applicationInfo.metaData.containsKey(GrowthPushConstants.NOTIFICATION_ICON_META_KEY))
                icon = Integer.valueOf(applicationInfo.metaData.getInt(GrowthPushConstants.NOTIFICATION_ICON_META_KEY));
            String title = packageManager.getApplicationLabel(applicationInfo).toString();

            builder.setTicker(title);
            builder.setSmallIcon(icon);
            builder.setContentTitle(title);
            if (applicationInfo.metaData != null
                && applicationInfo.metaData.containsKey(GrowthPushConstants.NOTIFICATION_ICON_BACKGROUND_COLOR_META_KEY)) {
                builder.setColor(ContextCompat.getColor(context,
                    Integer.valueOf(applicationInfo.metaData.getInt(GrowthPushConstants.NOTIFICATION_ICON_BACKGROUND_COLOR_META_KEY))));

            }
        } catch (NameNotFoundException e) {
        }

        String message = extras.getString("message");
        boolean sound = false;
        if (extras.containsKey("sound"))
            sound = Boolean.valueOf(extras.getString("sound"));

        Intent intent = new Intent(context, AlertActivity.class);
        intent.putExtras(extras);
        intent.putExtra("dialogType", DialogType.none.toString());
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        builder.setContentText(message);
        builder.setContentIntent(pendingIntent);
        builder.setWhen(System.currentTimeMillis());
        builder.setAutoCancel(true);

        if (sound && PermissionUtils.permitted(context, "android.permission.VIBRATE"))
            builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);

        return builder.build();

    }

    public Callback getCallback() {
        return callback;
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public static class Callback {

        public void onOpen(Context context, Intent intent) {
            context.startActivity(context.getPackageManager().getLaunchIntentForPackage(context.getPackageName())
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        }

    }

}

@@ -641,16 +641,42 @@
 gType;%0A%0A
+import java.util.Random;%0A%0A
 public c
@@ -2662,32 +2662,223 @@
        return;%0A%0A
+        String endTimestamp = String.valueOf(System.currentTimeMillis());%0A        int maxIdRange = Integer.valueOf(endTimestamp.substring(endTimestamp.length() - 9, endTimestamp.length()));%0A%0A
         Notifica
@@ -3069,9 +3069,40 @@
 (), 
-1
+new Random().nextInt(maxIdRange)
 , ge
@@ -3168,36 +3168,22 @@
    p
-rivate Notification generate
+ublic void add
 Noti
@@ -3212,94 +3212,397 @@
 xt, 
-Bundle extras) %7B%0A        PackageManager packageManager = context.getPackageManager();%0A
+int notificationId, Notification notification) %7B%0A        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);%0A        notificationManager.notify(%22GrowthPush%22 + context.getPackageName(), notificationId, notification);%0A    %7D%0A%0A    public NotificationCompat.Builder defaultNotificationBuilder(Context context, Bundle extras) %7B
 %0A   
@@ -3683,16 +3683,85 @@
 ontext);
+%0A        PackageManager packageManager = context.getPackageManager();
 %0A%0A      
@@ -5448,24 +5448,84 @@
 _CURRENT);%0A%0A
+        builder.setDefaults(Notification.PRIORITY_DEFAULT);%0A
         buil
@@ -5925,16 +5925,163 @@
  builder
+;%0A    %7D%0A%0A    private Notification generateNotification(Context context, Bundle extras) %7B%0A        return defaultNotificationBuilder(context, extras)
 .build()
@@ -6078,25 +6078,24 @@
 s).build();%0A
-%0A
     %7D%0A%0A    p

customize notificaiton builder