package com.plugin.gcm;

import com.google.android.gcm.GCMBaseIntentService;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

@SuppressLint("NewApi")
public class GCMIntentService extends GCMBaseIntentService {
	public static final int NOTIFICATION_ID = 237;
	private static String TAG = "PushPlugin-GCMIntentService";
	public static final String MESSAGE = "message";
	public static final String COLDSTART = "coldstart";

	public GCMIntentService() {
		super("GCMIntentService");
	}

	@Override
	public void onRegistered(Context context, String regId) {
		Log.v(TAG, "onRegistered: "+ regId);
        	NotificationService.getInstance(context).onRegistered(regId);
	}

	@Override
	public void onUnregistered(Context context, String regId) {
		Log.d(TAG, "onUnregistered - regId: " + regId);
	}

	@Override
	protected void onMessage(Context context, Intent intent) {
		boolean isPushPluginActive = NotificationService.getInstance(context).isActive();
		boolean isPushPluginForeground = NotificationService.getInstance(context).isForeground();

		Bundle extras = intent.getExtras();
		if (extras != null) {
			Log.d(TAG, "onMessage - isPushPluginActive: " + isPushPluginActive);
			Log.d(TAG, "onMessage - isPushPluginForeground: " + isPushPluginForeground);
			if (!isPushPluginActive) {
				extras.putBoolean(COLDSTART, true);
			}
	            
			NotificationService.getInstance(context).onMessage(extras);
			if (!isPushPluginForeground) {
				if (extras.getString(MESSAGE) != null && extras.getString(MESSAGE).length() != 0) {
					createNotification(context, extras);
				}
			}
		}
	}

	public void createNotification(Context context, Bundle extras)
	{
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		String appName = getAppName(this);

		Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
		notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
		notificationIntent.putExtra("pushBundle", extras);

		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

		int defaults = Notification.DEFAULT_ALL;

		if (extras.getString("defaults") != null) {
			try {
				defaults = Integer.parseInt(extras.getString("defaults"));
			}
			catch (NumberFormatException e) {}
		}
		
		Bitmap myBitmap = null;
		String customIconUrl = null;
		customIconUrl = extras.getString("icon");
		Log.d(TAG, "createNotification - icon: " + customIconUrl);
		if (customIconUrl != null) {
			try {
				URL url = new URL(customIconUrl);
				HttpURLConnection connection = (HttpURLConnection) url.openConnection();
				connection.setDoInput(true);
				connection.connect();
				InputStream input = connection.getInputStream();
				myBitmap = BitmapFactory.decodeStream(input);
			} catch (IOException e) {
				e.printStackTrace();
			}	
		}
		
		NotificationCompat.Builder mBuilder =
			new NotificationCompat.Builder(context)
				.setDefaults(defaults)
				.setSmallIcon(context.getApplicationInfo().icon)
				.setLargeIcon(myBitmap)
				.setWhen(System.currentTimeMillis())
				.setContentTitle(extras.getString("title"))
				.setTicker(extras.getString("title"))
				.setContentIntent(contentIntent)
				.setAutoCancel(true);

		String message = extras.getString("message");
		if (message != null) {
			mBuilder.setContentText(message);
		} else {
			mBuilder.setContentText("<missing message content>");
		}

		String msgcnt = extras.getString("msgcnt");
		if (msgcnt != null) {
			mBuilder.setNumber(Integer.parseInt(msgcnt));
		}

		int notId = NOTIFICATION_ID;

		try {
			notId = Integer.parseInt(extras.getString("notId"));
		}
		catch(NumberFormatException e) {
			Log.e(TAG, "Number format exception - Error parsing Notification ID: " + e.getMessage());
		}
		catch(Exception e) {
			Log.e(TAG, "Number format exception - Error parsing Notification ID" + e.getMessage());
		}

		mNotificationManager.notify((String) appName, notId, mBuilder.build());
		
	}

	public static void cancelNotification(Context context)
	{
		NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		mNotificationManager.cancel((String)getAppName(context), NOTIFICATION_ID);
	}

	private static String getAppName(Context context)
	{
		CharSequence appName =
				context
					.getPackageManager()
					.getApplicationLabel(context.getApplicationInfo());

		return (String)appName;
	}

	@Override
	public void onError(Context context, String errorId) {
		Log.e(TAG, "onError - errorId: " + errorId);
	}

}

@@ -1001,54 +1001,8 @@
 %7B%0A%09%09
-Log.v(TAG, %22onRegistered: %22+ regId);%0A        %09
 Noti
@@ -1503,160 +1503,8 @@
 ) %7B%0A
-%09%09%09Log.d(TAG, %22onMessage - isPushPluginActive: %22 + isPushPluginActive);%0A%09%09%09Log.d(TAG, %22onMessage - isPushPluginForeground: %22 + isPushPluginForeground);%0A
 %09%09%09i
@@ -2721,69 +2721,8 @@
 %22);%0A
-%09%09Log.d(TAG, %22createNotification - icon: %22 + customIconUrl);%0A
 %09%09if

Update GCMIntentService.java