(function () {
    'use strict';
 
    angular
      angular.module('Monitour.pages.login')
        .factory('AuthenticationService', AuthenticationService);
 
    function AuthenticationService($http, $localStorage, $translate) {
      var service = {};
 
        service.Login = Login;
        service.Logout = Logout;
 
        return service;
   
        function Login(username, password, callback) {
		var domain  = 'https://api.callio.com.au/public/';
		var path = 'login';
		var baseurl = domain + path;
		var datastring =  { email: username, password: password };
	
            $http.post(baseurl, datastring).then(function successCallback(response)  {
					
					console.log(response.data);
                    // login successful if there's a token in the response
                    if (response.data.access_token) {
						
						
                        // store username and token in local storage to keep user logged in between page refreshes
                        $localStorage.currentUser = response.data.access_token;

                        $localStorage.lang = response.data.language;
						$localStorage.currentUserid = response.data.id;

                        //$translate.use($localStorage.lang);
                        // add jwt token to auth header for all requests made by the $http service
                        $http.defaults.headers.common.Authorization = response.data.access_token;
 
                        // execute callback with true to indicate successful login
                        callback(response);
                    } else {
                        // execute callback with false to indicate failed login
                        callback(response);
                    }
                });
        }
 
        function Logout() {
            // remove user from local storage and clear http auth header
            delete $localStorage.currentUser; 
			 $localStorage.currentOwner;
            $localStorage.lang;
            $localStorage.currentUserid;
             $http.defaults.headers.common.Authorization = '';
			 $localStorage.currentUserCompany;
		     $localStorage.currentOwnerid;
		     $localStorage.currentClient ;
		     $localStorage.currentfirstname;
		     $localStorage.currentlastname;
		     $localStorage.url;
			 $localStorage.lang;
   
        }
    }
})();