(function(){
    angular.module('ctfApp').controller('LoginController',
        ['$scope', '$rootScope', '$location', 'AuthenticationService', '$window', 'NavbarService', 'AppSettings',
            function ($scope, $rootScope, $location, AuthenticationService, $window, NavbarService, AppSettings) {
                // reset login status
                AuthenticationService.clearCredentials();

                $scope.login = function () {
                    $scope.dataLoading = true;
                    AuthenticationService.login($scope.username, $scope.password, function(response) {
                        if(response.status == 200) {
                             NavbarService.setUsername($scope.username);
                             $location.path('/');
                             AuthenticationService.setCredentials($scope.username, $scope.password, response.team);
                             // redirect to additional external auth point
                             $window.location.href = AppSettings.getApiAddressWithCredentials($scope.username, $scope.password) + '/auth';
                        } else {
                            Materialize.toast('Błąd logowania! Spróbuj jeszcze raz.', 5000 ,'toast-error');
                            $scope.dataLoading = false;
                        }
                    });
                };


            }]);
})();
