(function(){
    angular.module('ctfApp').controller('TasksController', ['$scope', '$http', 'APIProvider', 'AppSettings', function($scope, $http, APIProvider, AppSettings) {
        $scope.apiAddress = AppSettings.getApiAddress();
        APIProvider.getTasks(function(data){
            // console.log(data);

            $.each(data, function(key){
                data[key].short_description = data[key].description.split(" ").slice(0,6).join(" ").concat("...");
                data[key].random = Math.random();
            });

            APIProvider.getMyCompletedLevelsList(function(completed){
                $.each(data, function(key, task){
                    if (completed.indexOf(task.level) !== -1){
                        data[key].completed = true;
                    }
                    else {
                        data[key].completed = false;
                    }
                });
            });

            $scope.tasks = data;

        });
    }]);
})();