var app=angular.module("mozzApp",['cfp.hotkeys','pubnub.angular.service','ngFileUpload']);

//////////////////////////////////////////////////////////////////////////////
//////////////////////MODULE CONFIGURATION DATA///////////////////////////////
//////////////////////////////////////////////////////////////////////////////
app.value('config',{
	ajaxModule:'../admin/',
	globalInterval:10000,	//10 seconds
	globalTimeout:10000,	//10 seconds
	pageSearchTimer:1050,   //1050 ms
	messageSize:765,		//765 for english sms
	unicodeMessageSize:335,	//335 for urdu sms
	pageLimit:50,
	currentPage:0,
	responseNumRows:0,
	totalPageAmount:0,
	showLog:true,
	showBtnBack:false,
	showBtnNext:false,
	btnClickedSave:false,
	btnClickedSearch:false,
	btnClickedSubmit:false,
	btnClickedProcess:false,
	sortAsc:false,
	sortedAsc:false,
	sortDesc:false,
	sortedDesc:false,
	isAscii:true,
	sortKey:'',
	btnTextSave:' Save ',
	btnTextSearch:' Search ',
	btnTextSubmit:' Submit',
	btnTextProcess:' Process',
	colorWarning:'#FF7043',
	colorDanger:'#2196F3',
	colorSuccess:'#66BB6A',
	globalError:'Please try again after some time...',
	postConfig:{headers:{'Content-Type': 'application/x-www-form-urlencoded'}},
});

//////////////////////////////////////////////////////////////////////////////
//////////////////////APPLICATION DIRECTIVES/////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
app.directive('scrollToBottom', function($timeout, $window) {
    return {
        scope: {
            scrollToBottom: "="
        },
        restrict: 'A',
        link: function(scope, element, attr) {
            scope.$watchCollection('scrollToBottom', function(newVal) {
                if (newVal) {
                    $timeout(function() {
                        element[0].scrollTop =  element[0].scrollHeight;
                    }, 0);
                }

            });
        }
    };
});
app.directive('currentTime', ['$interval', 'dateFilter', function($interval, dateFilter) {

  function link(scope, element, attrs) {
    var format,
        timeoutId;

    function updateTime() {
      element.text(dateFilter(new Date(), format));
    }

    scope.$watch(attrs.currentTime, function(value) {
      format = value;
      updateTime();
    });

    element.on('$destroy', function() {
      $interval.cancel(timeoutId);
    });

    // start the UI update process; save the timeoutId for canceling
    timeoutId = $interval(function() {
      updateTime(); // update DOM
    }, 1000);
  }

  return {
    link: link
  };
}]);
app.directive('bsPopover', function() {
    return function(scope, element, attrs) {
        element.find("[rel=popover]").popover({ html: true, placement: "top"});
    };
});
//////////////////////////////////////////////////////////////////////////////
//////////////////////APPLICATION FILTERS/////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
////ROUND MESSAGE
app.filter('roundMsg', function (){ 
    return function (input, is_ascii){
        var len=1;var ascii_size=160;var ascii_divider=153;var unicode_size=70;var unicode_divider=67;
		if(is_ascii){
			if(input>ascii_size){len=Math.ceil(input/ascii_divider);}			
		}else{			
			if(input>unicode_size){len=Math.ceil(input/unicode_divider);}
		}
        return len;
    };
});

//////////////////////////////////////////////////////////////////////////////
//////////////////////GLOBAL FUNCTIONS & VARIABLES////////////////////////////
//////////////////////////////////////////////////////////////////////////////
app.run(function($rootScope,config){
	$rootScope.format='hh:mm:ss a';
	////////////////SYSTEM MAIN FUNCTIONS///////////////////////////////
    $rootScope.validatePagination=function (){
        var maxLimit=config.pageLimit*(config.currentPage+1);
        if(maxLimit < config.responseNumRows){
        	config.showBtnNext=true;}else{config.showBtnNext=false;}
        if(config.currentPage > 0 ){config.showBtnBack=true;}else{config.showBtnBack=false;} 
    };
    $rootScope.resetPagination=function (){
        config.currentPage=0;config.showBtnBack=false;config.showBtnNext=false; 
    };
    $rootScope.cancelTimeout=function (promise){timeout.cancel(promise);};
    $rootScope.enableButtons= function (){
        config.btnClickedSave=false;config.btnClickedSearch=false;config.btnClickedSend=false;
        config.btnClickedSendLater=false;
        config.btnTextSearch=' Search ';config.btnTextSave=' Save ';config.btnTextSend=' Send SMS ';
        config.btnTextSendLater=' Schedule SMS ';
    };
    $rootScope.disableButtons= function (){
        config.btnClickedSave=true;config.btnClickedSearch=true;config.btnClickedSend=true;
        config.btnClickedSendLater=true;
        config.btnTextSearch=' Please Wait... ';config.btnTextSave=' Please Wait... ';
        config.btnTextSend=' Please Wait... ';config.btnTextSendLater=' Please Wait... ';
    };    
    $rootScope.moveNext=function (){
        config.currentPage++;
    };
    $rootScope.moveBack=function (){
        config.currentPage--;          
    }; 
    $rootScope.addPageAmount=function (amount){
        config.totalPageAmount+=amount;          
    }; 
    $rootScope.isEmpty=function (data){
    	if (data == null){ return true;}
    	if (data == undefined){ return true;}
    	if (data == 'null'){ return true;}
    	if (data == 'undefined' ){ return true;}
    	if (typeof data == undefined ){ return true;}
    	if (typeof data == 'number'){ return data === 0;}
    	if (typeof data == 'string'){ return this.isEmptyString(data);}
    	if (typeof data == 'boolean'){ return data === false;}
    	if (Array.isArray(data)) {return angular.equals(data,[]);}
    	if (typeof data == 'object'){ return angular.equals(data,{});}
    	return false;
    }; 
    $rootScope.isEmptyString=function (str){
    	if(str == null || typeof str == 'null' || str == undefined || typeof str == 'undefined' || !str || str.length ===0 || str === "" || !/[^\s]/.test(str) || /^\s*$/.test(str) || str.toString().replace(/\s/g) ==="" ){
    		return true;
    	}
    	return false;
    }; 
    $rootScope.isNumber=function (str){
    	if (typeof str == 'number'){ return true;}
    	if (typeof str == "string" ){
		  return !isNaN(str) && !isNaN(parseFloat(str));    		
    	}
    	return false;
    };
    $rootScope.isNaturalNumber=function (str){
    	if( !this.isNumber(str)) return false;
    	if( Number(str) >0) return true;
    	return false;
    };
	/////////////////////////////Alerts/////////////////////////////////
    $rootScope.successAlert=function (data){
        var html='<div class="alert alert-success alert-styled-left alert-arrow-left alert-bordered">'+
            '<button type="button" class="close" data-dismiss="alert"><span>&times;</span><span class="sr-only">Close</span></button>'+
            '<span class="text-semibold">Success! </span> '+data+'</div>';
        return html;
    };    
    $rootScope.dangerAlert=function (data){
        var html='<div class="alert alert-danger alert-styled-left alert-arrow-left alert-bordered">'+
            '<button type="button" class="close" data-dismiss="alert"><span>&times;</span><span class="sr-only">Close</span></button>'+
            '<span class="text-semibold">Error! </span> '+data+'</div>';
        return html;
    };    
    $rootScope.warningAlert=function (data){
        var html='<div class="alert alert-warning alert-styled-left alert-arrow-left alert-bordered">'+
            '<button type="button" class="close" data-dismiss="alert"><span>&times;</span><span class="sr-only">Close</span></button>'+
            '<span class="text-semibold">Warning! </span> '+data+'</div>';
        return html;
    };
    $rootScope.infoAlert=function (data){
        var html='<div class="alert alert-info alert-styled-left alert-arrow-left alert-bordered">'+
            '<button type="button" class="close" data-dismiss="alert"><span> &times; </span><span class="sr-only">Close</span></button>'+
            '<span class="text-semibold">Info! </span> '+data+'</div>';
        return html;
    };
    /////////////////////////////Sorting/////////////////////////////////
	$rootScope.processSort=function (key){
		var sort='';
		if(config.sortedAsc===true && config.sortedDesc===true){
			config.sortAsc=false;config.sortedAsc=false;config.sortDesc=false;config.sortedDesc=false;	
			$rootScope.sortKey='';
		}else{
			$rootScope.sortKey=key;
			if(config.sortAsc===false){
				config.sortAsc=true;config.sortDesc=false;config.sortedAsc=true;
				sort=key+' ASC';
				
			}else if(config.sortDesc===false){
				config.sortDesc=true;config.sortAsc=false;config.sortedDesc=true;
				sort=key+' DESC';
			}
		}
		return sort;
    };
	//////////////////block ui////////////////////////////////////
	$rootScope.blockPageUI=function(){
		$.blockUI({ 
			message: '<span class="font-weight-semibold" ><i class="icon-spinner4 spinner mr-2"></i>&nbsp; Please Wait...</span>',
			overlayCSS: {
				backgroundColor: '#000',
				// backgroundColor: '#1b2024',
				opacity: 0.9,
			},
			css: {
				border: 0,
				color: '#fff',
				padding: 0,
				backgroundColor: 'transparent'
			}
		});
	}
	$rootScope.unblockPageUI=function(){
		$.unblockUI();	
	}
	$rootScope.blockElementUI=function(elmnt,message){
		if(message===undefined){message='Please Wait...';}
		var block = $(elmnt);		
		$(block).block({ 
			message: '<span class="font-weight-semibold" ><i class="icon-spinner4 spinner mr-2"></i>&nbsp; '+message+'</span>',
			overlayCSS: {
				backgroundColor: '#000',
				opacity: 0.7,
				cursor: 'wait'
				
			},
			// z-index for the blocking overlay 
			centerX: false, 
			css: {
				textAlign:'top', 
				border: 0,
				padding: 0,
				color: '#fff',
				backgroundColor: 'transparent'
			}
		});
		/* 
		$('.blockOverlay').on('click', function() {
			$(block).unblock();
		}); */
	}	
	$rootScope.unblockElementUI=function(elmnt){
		var block = $(elmnt);	
		$(block).unblock();	
	}
    //////////////////////////////////////////////////////////////
	$rootScope.setSwaDefaults=function(){
		swal.setDefaults({
			buttonsStyling: true,
			confirmButtonClass: 'btn btn-warning',
			cancelButtonClass: 'btn btn-light'
		});
	}
	//////////////////////////////////////////////////////////////
	$rootScope.speak=function(speech){
		window.speechSynthesis.speak(new SpeechSynthesisUtterance(speech));
	}
	//////////////////////////////////////////////////////////////
	$rootScope.isAscii=function(message){
		if(typeof(message)!=='string'){return false;}
		for(var i=0;i<message.length;i++){
			if(message.charCodeAt(i)>127){return false;}
		}
		return true;
	}
	//////////////////////////////////////////////////////////////
	$rootScope.pnotify=function(message="",type="success",title="",sticky=true){
		switch(type){
			case 'error' : {
				if(title.length===0){title='System Error!';}
				new PNotify({title: title, text: message, icon: 'icon-blocked',type: 'error',hide: sticky});
			}
			break;
			case 'info' : {
				if(title.length===0){title='Information!';}
				new PNotify({title: title,text: message,icon: 'icon-info22',type: 'info',hide: sticky});
			}
			break;
			case 'warning' : {
				if(title.length===0){title='Warning!';}
				new PNotify({title: title,text:message,icon: 'icon-warning',type: 'primary',hide: sticky});
			}
			break;
			case 'success' : {
				if(title.length===0){title='Success!';}
				new PNotify({title: title,text: message,icon: 'icon-checkmark3',type: 'success',hide: sticky});
			}
			break;
			default : {
				if(title.length===0){title='Success!';}
				new PNotify({title: title,text: message,icon: 'icon-checkmark3',type: 'success',hide: sticky});
			}
			break;
		}
	}
	$rootScope.pnotifyDefaultError=function(){
		this.pnotify("Please try again after some time...","warning","Undetermined Error");
	}
    //////////////////////////////////////////////////////////////
});

//////////////////////////////////////////////////////////////////////////////
//////////////////////END MODULE//////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////