(function(){

    'use strict';

    var app = angular.module('ctfApp', ['ui.router', 'ncy-angular-breadcrumb']);
    app.config(function($stateProvider, $urlRouterProvider, $breadcrumbProvider) {

        $urlRouterProvider.otherwise('/home');

        $stateProvider

            .state('home', {
                url: '/home',
                templateUrl: '/page/home.html'
            })

            .state('tasks', {
                url: '/tasks',
                templateUrl: '/page/tasks.html'
            })

            .state('task', {
                url: '/task/:taskLevel',
                templateUrl: '/page/task.html'
            })

            .state('scores', {
                url: '/scores',
                templateUrl: '/page/scoresheet.html'
            });

    });

    app.factory('AppSettings', function() {
        return {
            apiAddress: "http://localhost:8080"
        }
    });

    app.factory('APIProvider', ['$http', 'AppSettings', function($http, AppSettings){
        var tasks, teams, scores;
        var getTasks = function(callback){
            $http.get(AppSettings.apiAddress + '/tasks').
                success(function(data) {
                    tasks = data;
                    if (callback) callback(data);
                });
        };
        var getScores = function(){};
        var getTaskById = function(id, callback){
            var result;
            if (!tasks) {
                getTasks(function(data){
                    result = getTaskById(id, callback)
                });
            }
            else {
                result = tasks.filter(function(task){
                    return task.level == id;
                })[0];
            }
            callback(result);
        };
        return {
            getScores: getScores,
            getTasks: getTasks,
            getTaskById: getTaskById
        };
    }]);

    app.controller('TasksController', ['$scope', '$http', 'APIProvider', 'AppSettings', function($scope, $http, APIProvider, AppSettings) {
        $scope.apiAddress = AppSettings.apiAddress;
        APIProvider.getTasks(function(data){
            console.log(data);
            $scope.tasks = data;
        });
    }]);

    app.controller('TaskController', ['$scope', '$http', '$stateParams', 'APIProvider', 'AppSettings', function($scope, $http, $stateParams, APIProvider, AppSettings) {
        $scope.apiAddress = AppSettings.apiAddress;
        $scope.selectedTaskId = $stateParams.taskLevel;
        console.log('SELECTED TASK ID', $scope.selectedTaskId);
        APIProvider.getTaskById($scope.selectedTaskId, function(data){
            console.log(data);
            $scope.task = data;
        });
    }]);

    app.controller('ScoreboardController', ['$scope', '$http', 'AppSettings', function($scope, $http, AppSettings) {
        var mock = { "nazwa zadania": ["lista","druzyn","ktora","rozwiazala"], "drugie zadanie": ["inna","druzyn","ktora","go"] };
        var calculatePerTeam = function(data) {
            var teams = {};
            for (var key in data) {
                if (data.hasOwnProperty(key)) {
                    var task = data[key];
                    for(var i = 0; i < task.length; i++){
                        if (!teams[task[i]]){
                            teams[task[i]] = 1;
                        }
                        else {
                            teams[task[i]] += 1;
                        }
                    }
                }
            }
            return teams;
        };

        function viewGraph(){
            $('.column').css('height','0');
            console.log($scope.teamsScores);
            setTimeout(function(){
                    $('.column').each(function(){
                        $(this).animate({width: 150*$(this).attr('data-score')}, 1500);
                    });
                }
            );
        }


        $http.get(AppSettings.apiAddress + '/solutions/all').
            success(function(data) {
                console.log(data);
                if ($.isEmptyObject(data)){
                    data = mock; // Delete!
                }
                $scope.teamsScores = calculatePerTeam(data);
                console.log($scope.teamsScores);
                viewGraph()
            });



    }]);

})();


