'use strict';

angular.module('courseComparator.courses', ['ngRoute', 'LocalStorageModule'])

    .config(['localStorageServiceProvider', function (localStorageServiceProvider) {
        localStorageServiceProvider.setPrefix('uocc');
    }])

    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/disciplines/:disciplineId', {
            templateUrl: 'courses/courses.html',
            controller: 'CoursesCtrl'
        });
    }])

    .controller('CoursesCtrl', ['$scope', '$routeParams', '$http', 'localStorageService', function ($scope, $routeParams, $http, localStorageService) {
        var interestsInStore = localStorageService.get('interests');

        $scope.interests = interestsInStore || [];

        $scope.$watch('interests', function () {
            localStorageService.set('interests', $scope.interests);

            $scope.courses = _($scope.courses).each(function(element, index) {
                var isInterest = false;

                if (_($scope.interests).findWhere(element)) {
                    isInterest = true;
                }

                _.extend(element, {isInterest: isInterest});
            })
        }, true);

        $scope.disciplineId = $routeParams.disciplineId;

        $http.get('data/' + $scope.disciplineId + '.json').success(function (data) {
            $scope.courses = _(data).each(function(element, index) {
                var isInterest = false;

                if (_($scope.interests).findWhere(element)) {
                    isInterest = true;
                }

                _.extend(element, {isInterest: isInterest});
            });
        });

        $scope.orderProp = ['code'];

        $scope.addInterest = function(courseCode, e) {
            e.preventDefault();

            var course = _($scope.courses).findWhere({code: courseCode});

            course['isInterest'] = true;

            $scope.interests = _($scope.interests).push(course);
        };

        $scope.removeInterest = function(courseCode, e) {
            e.preventDefault();

            var course = _($scope.interests).findWhere({code: courseCode});

            $scope.interests = _($scope.interests).without(course);
        }
    }]);
@@ -1170,16 +1170,17 @@
       %7D)
+;
 %0A       
@@ -1423,211 +1423,64 @@
     
-var isInterest = false;%0A%0A                if (_($scope.interests).findWhere(element)) %7B%0A                    isInterest = true;%0A                %7D%0A%0A                _.extend(element, %7BisInterest: isInterest%7D
+_.extend(element, _($scope.interests).findWhere(element)
 );%0A 

clean up a tad