var starter = angular.module('starter.services', []);

starter.factory('ApiService', ['$http', '$ionicPopup', '$ionicModal', function ($http, $ionicPopup, $ionicModal) {
	return {
		//login
		login: function(username, password){
			console.log('[JCE] [INFO]: atempting to login.');
			//JCE Login shit:
    		var request = "https://mipo.jce.ac.il/index.php/site/login?_dc=1419773997764&formValues=%7B%22form_fields%22%3A%7B%22name%22%3A%22"+username+"%22%2C%22pwd%22%3A%22"+password+"%22%7D%7D&callback=JSON_CALLBACK";

			return $http.get(request).then(function(resp) {

        		if(resp.data.guest == false){
        			window.localStorage['userInfo'] = JSON.stringify(resp.data);
        			console.log("[JCE] [INFO]: Success.");
        			return true;
        		}else{
        			var alertPopup = $ionicPopup.alert({
        				title: 'שם משתמש או סיסמא שגויים',
       					template: 'וודא שוב כי שם המשתמש והסיסמא הינם נכונים.'
     					});
        			console.log("[JCE] [INFO]: Wrong user/pass.");
        			return false;
        		}

			}, function(err) {
				var alertPopup = $ionicPopup.alert({
        				title: 'שגיאת רשת',
       					template: 'בדוק את החיבור לנתונים'
     					});
				console.error('[JCE] [FATAL]: Network Error!', err);
    			return false;
			})
			return false;
		}, 
		update: function(){
			var username = window.localStorage['username'];
			var password = window.localStorage['password'];
			var notes = window.localStorage['notes'] || null;
			var grades = window.localStorage['grades'] || null;

    		var request = "https://mipo.jce.ac.il/index.php/site/login?_dc=1419773997764&formValues=%7B%22form_fields%22%3A%7B%22name%22%3A%22"+username+"%22%2C%22pwd%22%3A%22"+password+"%22%7D%7D";

			return $http.get(request).then(function(resp) {

        		if(resp.data.guest == false){
        			//Logged in
        			//
        			//Get Notes:
        			$http.get("https://mipo.jce.ac.il/index.php/site/studentMessages").then(function(resp) {
    				console.log('Success', resp);
    				console.log("[JCE] [INFO] : in update - Got JCE News!");
    				if(notes === null){
    					window.localStorage['newNotes'] = "true";
    					window.localStorage['notes'] = JSON.stringify(resp.data);
    				}else if(notes === JSON.stringify(resp.data)){
    					console.log("[JCE] [INFO] : in update - no new Notes");
    				}else{
    					window.localStorage['newNotes'] = "true";
    					window.localStorage['notes'] = JSON.stringify(resp.data);
    				}
    				
    				}, function(err) {
    					console.error('[JCE] [INFO] : in update - can\'t get Notes', err);
    				});

    				//Get Grades
    				$http.get("https://mipo.jce.ac.il/index.php/site/studentGrades").then(function(resp) {
    				console.log('Success', resp);
    				console.log("[JCE] [INFO] : in update - Got Grades!");
    				if(grades === null){
    					window.localStorage['newGrades'] = "true";
    					window.localStorage['grades'] = JSON.stringify(resp.data);
    				}else if(grades === JSON.stringify(resp.data)){
    					console.log("[JCE] [INFO] : in update - no new Grades");
    				}else{
    					window.localStorage['newGrades'] = "true";
    					window.localStorage['grades'] = JSON.stringify(resp.data);
    				}
    				
    				}, function(err) {
    					console.error('[JCE] [INFO] : in update - can\'t get Grades', err);
    				});

    				//Get Calendar
    				//https://mipo.jce.ac.il/index.php/site/timetable
        			$http.get("https://mipo.jce.ac.il/index.php/site/timetable").then(function(resp) {
    					console.log('Success', resp);
    					console.log("[JCE] [INFO] : in update - Got Calendar!");
    					window.localStorage['calendar'] = JSON.stringify(resp.data);
    					
    				
    				}, function(err) {
    					console.error('[JCE] [INFO] : in update - can\'t get Calendar', err);
    				});
        			
        		}else{
        			window.localStorage['loggedIn'] = false;

        			return false;
        		}

			}, function(err) {
				window.localStorage['loggedIn'] = false;	
    			return false;
			})
			return false;

		}

	};
}]);