(function(){
    angular.module('ctfApp').controller('LoginController',
        ['$scope', '$rootScope', '$location', 'AuthenticationService', '$window', 'NavbarService',
            function ($scope, $rootScope, $location, AuthenticationService, $window, NavbarService) {
                // 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 = "http://"+$scope.username+":"+$scope.password+"@localhost:8080/api/v1/auth";
                        } else {
                            Materialize.toast('Błąd logowania! Spróbuj jeszcze raz.', 5000 ,'toast-error');
                            $scope.dataLoading = false;
                        }
                    });
                };


            }]);
})();