app.factory('Training', ['Group',
    function(Group, Util) {

        function Training(name) {
            this.name = name;
            this.groups = [];
        }

        Training.prototype.init = function(json) {
            var raw = JSON.parse(json);
            this.name = raw.name;
            loadGroups(this.groups, raw.groups);
            return this;
        };

        Training.prototype.getTime = function() {
            var time = 0;
            _.each(this.groups, function(group) {
                time += group.getTime();
            });
            return time;
        };

        function loadGroups(to, from) {
            _.each(from, function(rawGroup) {
                to.push(new Group().init(rawGroup));
            });
        }

        return Training;
    }
]);
