app.controller('loginController', function ($scope, $location, $window, $rootScope, loginFactory, notificationFactory, destaquesFactory, httpRequestFactory) {

    /*$scope.user = {
        Login: '',
        Password:''
    };*/

    
    $('.carousel').carousel({
        interval: 8000 //changes the speed
    });

    $scope.submit = function () {
        
        if ($scope.user == null)
        {
            $scope.user = {
                Login : $('[name=username]').val(),
                Password : $('[name=password]').val()
            };
        }

        var getSuccessCallback = function (auth, status, data) {
            

            if (auth.Autenticado == true) {
                
                loginFactory.validCredentials(auth.PK_Utilizador,auth.EncToken);
                $window.sessionStorage.setItem('username.cibe.inovar', $scope.user.Login);
                $window.sessionStorage.setItem('tipout.cibe.inovar', auth.TipoUt);
                $window.sessionStorage.setItem('CibeAtk', auth.Atk);
                $window.sessionStorage.setItem('CibeUltimaEntradaStr', auth.UltimaEntradaStr);
                $window.sessionStorage.setItem('CibeUsernameAlt', auth.UsernameAlt);
                $window.sessionStorage.setItem('usaProgContabilidade', JSON.stringify(auth.UsaProgContabilidade));
                $rootScope.$broadcast('event:perfisRequired', auth.Perfis);

            }
            else {
                if (auth.Msg=="")
                    auth.Msg="Utilizador ou Password inválido";
                
                notificationFactory.error(auth.Msg);
                loginFactory.clearCredentials($scope.user.Login, $scope.user.Password);
                $scope.user.Password = '';
            }
        };

        var errorCallback = function (data, status, headers, config) {
            notificationFactory.error(data.ExceptionMessage);
        };

        loginFactory.setCredentials($scope.user.Login, $scope.user.Password).
            success(getSuccessCallback).error(errorCallback);
    };

    $scope.encerrarSessao = function () {
        loginFactory.encerrarSessao();
    };

    $scope.carregarNoticias = function () {
        var getSuccessCallback = function (data, status) {
            $scope.noticias = data;

        };


        var errorCallback = function (data, status, headers, config) {
            //notificationFactory.error(data);
        };

        noticiasFactory.getNoticias().success(getSuccessCallback).error(errorCallback);
    };

    //carregar imagens do login
    $scope.imagebkgloginPage = function () {
        //carregar imagens pela escola
        //$scope.images = ["../app/images/foto2.png", "../app/images/slide2.png", "../app/images/foto3.png"];
        $scope.images = ["../app/images/slide2.jpg"];
    }

    $scope.correTarefasArranque = function () {

        var getSuccessCallback = function (data, status) {
            
        };

        var errorCallback = function (data, status, headers, config) {
            
        };

        var dados = {};
        httpRequestFactory.postData('../api/tarefas/arranque', dados).success(getSuccessCallback).error(errorCallback);

    };
});