(function () {
	angular.module('vipHeader', ['toDoModule'])
		.directive('vipHeader', function (ToDoService, ProfileService, reviewStudentAppService, ProjectService) {
			return {
				templateUrl: 'features/header/headerTemplate.html',
				restrict: 'E',
				scope: {
					count: '='
				},

				controllerAs: 'header',
				controller: function ($rootScope) {
					var vm = this;
					vm.currentSem;

					$rootScope.$on('$viewContentLoaded', function () {
						console.log("Page refreshed");
						$rootScope.$broadcast('refresh');

						//rpach023 getting semesters and changing array into a single string.
						ProjectService.getCurrentSemester().then(function (data) {
							if (data) {
								vm.currentSem = data;
							}

							vm.currentSem = vm.currentSem.join(", ");

						})

					});
					// //rpach023

					// ProjectService.getCurrentSemester().then(function (data) {
					// 	if (data) {
					// 		vm.currentSemesters = data;
					// 	}
					// 	vm.currentSem = vm.currentSem.join(", ");
					// });

					// User Story: FA18 | VIP-124, tezew001 (impersonation banner)
					ProfileService.isImpersonatedProfile().then(function (data) {
						if (data) {
							vm.is_impersonated = true;
						}
					});

					ProfileService.loadProfile().then(function (data) {
						if (data) {
							vm.data = data;
							vm.current_user = data.firstName;
							vm.user_type = data.userType;
							if (data.selectedSemester) {
								reviewStudentAppService.getTerm(data.selectedSemester).then(function (term) {
									vm.selectedSemester = term.name;
								})
							}
							var id = data._id;
							vm.logged_in = true;

							vm.count = 0;
							ToDoService.loadMyToDo(vm.data)
								.then(function (data) {
									for (i = 0; i < data.data.length; i++) {
										if (data.data[i].read) {
											continue;
										} else {
											vm.count++;
										}
									}
								});

							$rootScope.$on('refresh', function () {
								console.log("View refreshed");
								vm.count = 0;
								ToDoService.loadMyToDo(vm.data)
									.then(function (data) {
										vm.count = 0
										for (i = 0; i < data.data.length; i++) {
											if (data.data[i].read) {
												continue;
											} else {

												vm.count++;
											}
										}
									});
								if (!$rootScope.$$phase) {
									$rootScope.$apply();
								}
							});

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