package fsenablers.android.http;

import fsenablers.android.activity.WebAppActivity;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
import android.widget.Toast;


public class HttpUploadTask extends AsyncTask<HttpUploadRequest,HttpUploadRequest,Boolean> {

	Activity activity;
	public final int notificationId = 82; 
	
	public HttpUploadTask( Activity ctx ) {
		super();
		activity = ctx;
	}
	
	/*
	public void onPreExecute() {
		
		NotificationCompat.Builder mBuilder =
    	        new NotificationCompat.Builder(activity)
    	        .setSmallIcon(android.R.drawable.stat_sys_upload)
    	        .setContentTitle( "File Upload" )
    	        .setContentText( "Uploading..." );
    	
		Intent resultIntent = new Intent( activity, activity.getClass() );

		// The stack builder object will contain an artificial back stack for the
		// started Activity.
		// This ensures that navigating backward from the Activity leads out of
		// your application to the Home screen.
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(activity);
		// Adds the back stack for the Intent (but not the Intent itself)
		stackBuilder.addParentStack(activity.getClass());
		// Adds the Intent that starts the Activity to the top of the stack
		stackBuilder.addNextIntent(resultIntent);
		PendingIntent resultPendingIntent =
		        stackBuilder.getPendingIntent(
		            0,
		            PendingIntent.FLAG_UPDATE_CURRENT
		        );
		mBuilder.setContentIntent(resultPendingIntent);
		NotificationManager mNotificationManager =
		    (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
		// mId allows you to update the notification later on.
		mNotificationManager.notify("UPLOAD", id, mBuilder.build());  	
    	
	}
	*/
	
	public Boolean doInBackground( HttpUploadRequest... requests ) {

		Boolean result = Boolean.FALSE;
		int count = requests.length;
        for (int i = 0; i < count; i++) {
        	publishProgress(requests[i]);
        	requests[i].send();
        	publishProgress(requests[i]);
        	result = requests[i].getResult();
        	// only send one file
        	break;
        }
        return result;		

	}
	
	protected void onProgressUpdate(HttpUploadRequest... progress) {
		
		HttpUploadRequest request = progress[0];
		
	
		NotificationCompat.Builder mBuilder =
    	        new NotificationCompat.Builder(activity)
    	        .setSmallIcon(android.R.drawable.stat_sys_upload)
    	        .setContentTitle( "File Upload" )
    	        .setContentText( "Uploading " + request.getFileName() + "...");
		
		if( request.getComplete() == Boolean.TRUE ) {
			mBuilder.setSmallIcon( android.R.drawable.stat_sys_upload_done );
			mBuilder.setContentText( request.getFileName() + " uploaded" );
			
			WebAppActivity activity = request.getMainActivity();
			String txId = request.getWebTransactionId();
			if( txId != null ) {
				activity.sendResultToWebView( txId, true, "File Uploaded"  );
			}
		}
    	
		Intent resultIntent = new Intent( activity, activity.getClass() );

		// The stack builder object will contain an artificial back stack for the
		// started Activity.
		// This ensures that navigating backward from the Activity leads out of
		// your application to the Home screen.
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(activity);
		// Adds the back stack for the Intent (but not the Intent itself)
		stackBuilder.addParentStack(activity.getClass());
		// Adds the Intent that starts the Activity to the top of the stack
		stackBuilder.addNextIntent(resultIntent);
		PendingIntent resultPendingIntent =
		        stackBuilder.getPendingIntent(
		            0,
		            PendingIntent.FLAG_UPDATE_CURRENT
		        );
		mBuilder.setContentIntent(resultPendingIntent);
		NotificationManager mNotificationManager =
		    (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
		// mId allows you to update the notification later on.
		mNotificationManager.notify( request.getFileName(), notificationId, mBuilder.build());  		
		
		/*
		MainActivity activity = request.getMainActivity();
		String txId = request.getWebTransactionId();
		Log.d( "MyApp", "End of Upload: " + txId);
		if( true ) {
			//Toast.makeText( request.getMainActivity(), "Upload Complete", Toast.LENGTH_SHORT ).show();
			if( txId != null ) {
				activity.sendResultToWebView( txId, true, request.getResponseText()  );
			}
		} else {
			//Toast.makeText( request.getMainActivity(), "Upload Unsuccesfull", Toast.LENGTH_LONG ).show();
			if( txId != null ) {
				activity.sendResultToWebView( txId, false, request.getResponseText() );
			}
		}
		*/
		
		
    }
	
	/*
	protected void onPostExecute( Boolean result) {

		
		Log.d( "MyApp", "Upload onPostExecute" );
		String sText;
		if( result ) {
			sText = "Complete";
			
		} else {
			sText = "Failed";
		}
		
		NotificationCompat.Builder mBuilder =
    	        new NotificationCompat.Builder(activity)
    	        .setSmallIcon(android.R.drawable.stat_sys_upload_done )
    	        .setContentTitle( "File Upload" )
    	        .setContentText( sText );
    	
		Intent resultIntent = new Intent( activity, activity.getClass() );

		// The stack builder object will contain an artificial back stack for the
		// started Activity.
		// This ensures that navigating backward from the Activity leads out of
		// your application to the Home screen.
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(activity);
		// Adds the back stack for the Intent (but not the Intent itself)
		stackBuilder.addParentStack(activity.getClass());
		// Adds the Intent that starts the Activity to the top of the stack
		stackBuilder.addNextIntent(resultIntent);
		PendingIntent resultPendingIntent =
		        stackBuilder.getPendingIntent(
		            0,
		            PendingIntent.FLAG_UPDATE_CURRENT
		        );
		mBuilder.setContentIntent(resultPendingIntent);
		NotificationManager mNotificationManager =
		    (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
		// mId allows you to update the notification later on.
		mNotificationManager.notify("UPLOAD", id, mBuilder.build());  			
	}
	*/
	
}
