/*
*  	CHRIS NOTIFICATIONS
*	Description: requires jquery and bootstrap. This is a notification plugin used in the CHRIS system.
*                This plugin was created to only show the current notification. Stacking notifications is not a feature.
*
*  Author: froggr (Joe Lindsay)
*  version: 0.1(19 Nov 2014)
*/


/* 
* example of usage :
* 	$.notification({type: 'bootstrap class', icon: 'glyph icon', title: 'Title Here', message: 'Content Here', delay: #, animate: '.css_class', positon: 'position');
*	class = bootstrap class (danger, warning, succes or info)
*	icon = the appending icon name after 'glyphicon-' so for 'glyphicon-trash' you would use 'trash'
*	Title content is placed in strong tags
*   message content comes second in normal text
*   delay is the amount of time the notification is displayed. if not set, the notification will be persistent until its replaced
*	animate (ie .wobble) applies a class to the notification. eg .wobble
*	position can be bottomleft, bottomright, topleft, topright. actual positioning is based in chris-notifications.less
*   NOTE: you can destroy the onscreen notification by passing {close : 1}
*/


	var setOptions = function (options) {
		options = {
				type : options.type ? options.type : 'info',
				icon : options.icon ? options.icon : '',
				title : options.title ? options.title : '',
				message : options.message ? options.message : '',
				delay : options.delay ? options.delay : '',
				animate : options.animate ? options.animate : '',
				position : options.position ? options.position : 'bottomright'
		};
		if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) {
			options.position = 'topright';
		}
		if (options.icon){
			options.icon = "<span class=\"glyphicon glyphicon-"+ options.icon +"\"></span>";
		}
		if (options.message){
			options.message = "<span class=\"notification_message\"> "+ options.message +"</span>";
		}
		if (options.title){
			options.title = "<strong> "+ options.title +"</strong>";
		}
		
		return(options);
	};


	var buildNotification = function (options) {
		$( "body" ).append( "<div class=\" notification_box "+ options.position +"\"><div class=\"alert alert-"+options.type+" alert-dismissible "+options.animate+"\" role=\"alert\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"><span aria-hidden=\"true\">×</span><span class=\"sr-only\">Close</span></button>"+ options.icon +" "+ options.title +"&nbsp;"+ options.message +"</div></div>" );
		setTimeout(function() {
			$(".notification_box").addClass("showit");
		}, 1);
		return;
	};

	var loading = function (options) {
	
		destroyNotification();
		var centerload = null;

		if(options){
			$(".thepage").addClass('blur');
			centerload = "center-load";
		}
		//$( "body" ).append( "<div class=\"hidden-print notification_box bottomright\"><div style=\"padding-bottom: 20px; padding-left: 45px;\" class=\"loading\"><span class=\"icon-spinner icon-spin icon-3x text-primary\"></span></div></div>" );
		$( "body" ).append( '<div class=\"hidden-print notification_box bottomright\"><div style=\"padding-bottom: 20px; padding-left: 45px;\" class=\"loading\"> <div class="'+centerload+'"><div class="swing"><div class="swing-l"></div><div></div><div></div><div></div><div></div><div></div><div class="swing-r"></div></div><div class="shadow"><div class="shadow-l"></div><div></div><div></div><div></div><div></div><div></div><div class="shadow-r"></div></div>  </div></div></div>' );
	
		setTimeout(function() {
			$(".notification_box").addClass("showit");
		}, 1);
		return;
	};

	var destroyNotification = function () {
		
	   	$(".notification_box").remove();
	   	return;
	};

	var delayDestroyNotification = function (ms) {
		
		setTimeout(function() {
			$(".notification_box").removeClass("showit");
	   		setTimeout(function() {$(".notification_box").remove();}, 250);
	   	}, ms);
	   	return;
	};


	var sleep = function (ms) {

	   	  var r = $.Deferred();
		  setTimeout(function () {
		    r.resolve();
		  }, ms);
		   return $.when(r).done(function(){
        console.log('sleep is done');
        // Both asyncs tasks are done
    	}).promise();
	}; 



	var notification = function ( options ) {

		if(options.close == 1){
			destroyNotification();	
		}
		else if( options.loading == 1 ){
			loading();
		}
		else if( options.loading == 2 ){
			loading(true);
		}

		else {
			options = setOptions(options);
			if( $('.notification_box').length )        
			{
				destroyNotification();
				buildNotification(options);
		   	}
		   	else {
		   		buildNotification(options);
		   	}
		   	if(options.delay){
		   		delayDestroyNotification(options.delay);
		   	}
		}

	};

	$.notification = notification;
ChrisApp.factory('chrisNotification',  function() {
	
	var clear = function() {
		destroyNotification();
	};

	var makeNotification = function(type, extra) {
		var options;
		switch(type) {
			case 'save':
				options = {title:"Saving...", icon: 'save'};
			break;
			case 'saved':
				options = {type:'success', icon:'floppy-saved', title:'SAVED!'};
			break;
			case 'error':
				options = {type:'danger', icon:'warning-sign', title:extra.message, animate:'wobble'};
			break;
			case 'remove':
			options = {type:'danger', icon:'trash', title:'Removed Successfully!'};
			break;

		}

//		options.delay = 5000;
		if(extra !== undefined) {
			if(extra.delay !== undefined) {
				options.delay = extra.delay;
			}
		}

		notification(options);
	};

	return {
		notification: notification,
		makeNotification: makeNotification,
		clear: clear
	};
});

