var NotificationsModule = angular.module('notificationsModule', []);

// ------------------------------------------------------------------------
//
//	RESOURCES BELOW -->
//
// ------------------------------------------------------------------------

NotificationsModule.factory('Notification', ['apiUrl', '$resource', 'ResourceMethods', 'ResourcePrototype', function (apiUrl, $resource, methods, _prototype) {
	var Notification = $resource(apiUrl('notifications/:notificationId'), { notificationId: '@id' }, methods);
	angular.extend(Notification.prototype, _prototype);
	return Notification;
}]);

NotificationsModule.factory('NotificationSetting', ['apiUrl', '$resource', 'ResourceMethods', 'ResourcePrototype', function (apiUrl, $resource, methods, _prototype) {
	var Setting = $resource(apiUrl('notifications/settings/:settingId'), { settingId: '@id' }, methods);
	angular.extend(Setting.prototype, _prototype);
	return Setting;
}]);
NotificationsModule.controller("NotificationsMainCtrl", [
	"$scope",
	"ErrorsHandler",
	"Notification",
	"NotificationSetting",
	"Messenger",
	"$paginator",
	function (
		$scope,
		ErrorsHandler,
		Notification,
		NotificationSetting,
		Messenger,
		$paginator
	) {
		/**
		 * A list of notifications.
		 *
		 * @var array
		 */
		$scope.notifications = [];

		/**
		 * Paginator object for notifications
		 *
		 * @type object
		 */
		$paginator = angular.copy($paginator);
		$scope.paginator = $paginator;
		$paginator.setOnPageChange(function(page) {
			$scope.load(page);
		});

		/**
		 * Form data
		 *
		 * @type object
		 */
		$scope.data = {};

		/**
		 * Loads notifications for the specified page.
		 *
		 * @return void
		 */
		$scope.load = function (page) {
			if (!page) page = $scope.paginator.getCurrentPage();

			Notification.get(
				{ page: page, limit: $scope.paginator.getLimit() },
				function (res) {
					$scope.notifications = res.data;
					
					// Update paginator with response data using built-in method
					$scope.paginator.setFromResponse(res);
				},
				ErrorsHandler
			);
		};

		/**
		 * Mark all notifications as read for the current user.
		 *
		 * @return void
		 */
		$scope.clearAll = function () {
			if (!confirm('Are you sure you want to mark all notifications as read?')) {
				return;
			}

			Notification.get(
				{ clearAll: true },
				function (res) {
					// Reload current page to reflect read status
					$scope.load($scope.paginator.getCurrentPage());
					Messenger("All notifications have been marked as read.");
				},
				ErrorsHandler
			);
		};

		/**
		 * Clear all notifications from sidebar (mark as read)
		 *
		 * @return void
		 */
		$scope.clearAllNotifications = function () {
			Notification.get(
				{ clearAll: true },
				function (res) {
					// Clear notifications from sidebar display
					if (typeof $scope.$parent !== 'undefined' && $scope.$parent.notifications) {
						$scope.$parent.notifications = [];
						$scope.$parent.unread = 0;
					}
					Messenger("All notifications marked as read.");
				},
				ErrorsHandler
			);
		};

		$scope.loadSettings = function () {
			NotificationSetting.get(function (res) {
				// Populate the form data
				_.each(res.data, function (el) {
					$scope.data[el.notification] = {
						alert: el.alert,
						email: el.email
					};
				});
			}, ErrorsHandler);
		};

		/**
		 * Update notification settings.
		 *
		 * @return void
		 */
		$scope.update = function () {
			NotificationSetting.save(
				$scope.data,
				function (res) {
					Messenger("I have successfully updated your settings.");
				},
				ErrorsHandler
			);
		};

		$scope.redirect = function (notification) {
			switch (notification.related_type) {
				case "repair":
					return (window.location.href =
						"/tasks/repairs/" + notification.related_id);
				case "support":
					return (window.location.href =
						"/tasks/support/" + notification.related_id);
				case "onsite":
					return (window.location.href =
						"/tasks/onsite/" + notification.related_id);
				case "order":
					return (window.location.href =
						"/tasks/orders/" + notification.related_id);
				case "message":
					return (window.location.href =
						"/messages/" + notification.related_id);
			}
		};

		// Initialize
		$scope.load();
		$scope.loadSettings();
	}
]);


/**
 * Stats Module
 *
 */
