'use strict';

angular.module('app.login').controller('LoginController', function ($scope, $rootScope, $stateParams, $state, LoginService, AppService, usSpinnerService) {
    console.log("Login controller initialized.");
    
    //- Functions --------------------------------------------------------------
    
    function init(){
        $('#main-load-screen', window.parent.document).fadeOut('fast');

        $scope.showActionSpinner();

        //request is unsuccessful if remote access is disabled
        AppService.getRemoteAccessSettings(function(settings){
            $scope.hideActionSpinner();
            $scope.settingsLoaded = true;

            if(!settings){
                LoginService.setDisabled(true);
            }
            else {
                LoginService.setDisabled(!settings.allow);
                $scope.accessDisabled = LoginService.isDisabled();
                if(settings.allow){
                    $scope.login(true);
                }
            }
        });
    }

    $scope.login = function(dontShowError){
        $scope.showActionSpinner();

        LoginService.login($scope.credentials.password).then(function(result){
            $scope.hideActionSpinner();
            
            if(result.success){
                console.log("Logged in!"); 
                window.location.replace(window.location.origin);
            }
            else {
                if(!dontShowError){
                    $rootScope.showError("Login error", result.data.error);
                }
            }
        });
    }
    
    $scope.logout = function(){
        LoginService.logout();
    }
    
    //spinner functions
    $scope.showActionSpinner = function() {
        if (!$scope.spinneractive) {
            usSpinnerService.spin('spinner-login');
        }
    };

    $scope.hideActionSpinner = function() {
        if ($scope.spinneractive) {
            usSpinnerService.stop('spinner-login');
        }
    };

    $rootScope.showSuccess = function(title, message){
        if($scope.accessDisabled) return;

        $scope.errorDialog.title = title;
        $scope.errorDialog.message = message;
        $scope.errorDialog.isHtmlContent = true;

        try { $scope.$digest(); } catch(err){}
        showPopup("#errorDialog");
    }

    $rootScope.showError = function(title, message){
        if($scope.accessDisabled) return;

        $scope.errorDialog.title = title;
        $scope.errorDialog.message = message;
        $scope.errorDialog.isHtmlContent = true;

        try { $scope.$digest(); } catch(err){}
        showPopup("#errorDialog");
    }
    
    //- Event Handlers --------------------------------------------------------------
            
    $scope.$on('us-spinner:spin', function(event, key) {
        $scope.spinneractive = true;
    });

    $scope.$on('us-spinner:stop', function(event, key) {
        $scope.spinneractive = false;
    });
    
    //- Initialization --------------------------------------------------------------

    $scope.credentials = {};
    $scope.errorDialog = {};
    $scope.accessDisabled = true;
    $scope.settingsLoaded = false;
        
    $(".modal-backdrop").remove();

    init();

});