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

                $scope.login = function () {
                    $scope.dataLoading = true;
                    AuthenticationService.login($scope.username, $scope.password, function(response) {
                        if(response.status == 200) {
                             $('.title').html($scope.username+'@<b>capture-the-flAGH-2016</b>:~$');
                             $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;
                        }
                    });
                };


            }]);
})();