angular.module('app.login').service('LoginService', ['$q', '$localStorage', 'BackendService', function($q, $localStorage, BackendService) {
        var token;
        var $storage = $localStorage.$default();
        var disabled = false;
        
        this.login = function(password){
            var deferred = $q.defer();
            var data = { password: password };
            
            BackendService.getPromise('/main/login', data, 'POST').then(function(result){
                if(result.success){
                    token = result.data.token;
                    
                    saveToken();
                    
                    BackendService.setToken(token);
                    deferred.resolve({success: true});
                }
                else {
                    deferred.resolve({
                        success: false,
                        data: result.data
                    });
                }
            });
            
            return deferred.promise;
        }
        
        this.checkRemoteAccess = function(){
            var url = "main/checkaccess";
            return BackendService.getPromise(url, null);
        }
        
        this.loadSessionInfo = function(){
            token = $storage.token;
            if(token){
                BackendService.setToken(token);
            }
        }();
        
        this.logout = function(){
            token = null;
            $storage.token = null;
        }
        
        function saveToken(){
            $storage.token = token;
        }
        
        this.getToken = function(){
            return token;
        }
        
        this.setDisabled = function(dis){
            disabled = dis;
        }
        
        this.isDisabled = function(){
            return disabled;
        }
        
    }]);
        