(function () {
    'use strict';

    var oneKeyApp = angular.module('oneKeyApp');

    oneKeyApp.controller('navigationController', [
        '$scope', '$rootScope', '$timeout', '$modal', '$log', '$http', 'localize', 'notify', 'notifications', 'urls', 'featureToggle', '$mdDialog', 'urls', 'authService',

        function navigationController($scope, $rootScope, $timeout, $modal, $log, $http, localize, notify, notifications, urls, featureToggle, $mdDialog, metUrls, authService) {
            $scope.loading = true;

            $scope.updating = false;
            $scope.sync = false;            // true: means user is on the notification screen.
            $scope.unacknowledged = null;   // tracks the current notifications that have not been acknowledged.
            $scope.seenPlacesBadge = false;
            $scope.load = function () {
                var success = function (result) {
                    $scope.unacknowledged = result;
                    if ($scope.sync)
                    {
                        $scope.loading = false;
                        return;
                    }
                };

                var error = function () {
                    $scope.loading = false;
                };
                
                $http.get(urls('notificationsApi').concat('/count')).success(success);
            };

			$scope.inventoryAddLink = urls('inventoryAdd');
            $scope.$on('update-notifications-count', function (event, arg) {
                $scope.load();
            });
            $scope.$on('seen-places-badge', function () {
                $scope.seenPlacesBadge = true;
            });

            $scope.path = window.location.href;

            $scope.activate = function (currentPath) {
                return currentPath == $scope.path;
            }

            $scope.showMessageNotification = false;
            $scope.display = function () {
                if ($scope.unacknowledged != null && $scope.unacknowledged > 0) {
                    $scope.showMessageNotification = true;
                    return $scope.unacknowledged;
                } else {
                    $scope.showMessageNotification = false;
                    return '';
                }
            };

            var acknowledge = function (id, callback) {
                var success = function (result) {
                    $scope.updating = false;
                    notifications.acknowledged += 1;
                    if (callback) {
                        callback();
                    }
                };

                var error = function () {
                    $scope.updating = false;
                };

                $scope.updating = true;
                notifications.acknowledge({ id: id }, success, error);
            };

            $scope.acknowledge = function (id) {
                acknowledge(id);
            };
			$scope.getAntiForgeryToken = function (token) {

            }

            $scope.showNewFeatures = function (evt) {
                var parent = angular.element(document.querySelector('#onekey-wrapper'));

                $mdDialog.show({
                    templateUrl: metUrls('newFeatureCarousel'),
                    controller: DialogController,
                    parent: parent,
                    targetEvent: evt,
                    clickOutsideToClose: true,
                    fullscreen: true
                });
            };

            function DialogController($scope, $mdDialog) {
                $scope.messages = {
                    whatsNew: localize('messages', 'whatsNew'),
                    wereAddingNewFeatures: localize('messages', 'wereAddingNewFeatures'),
                    geofencingBetaTitle: localize('messages', 'geofencingBetaTitle'),
                    geofencingBetaSubTitle: localize('messages', 'geofencingBetaSubTitle'),
                    geofencingInOutTitle: localize('messages', 'geofencingInOutTitle'),
                    geofencingInOutSubTitle: localize('messages', 'geofencingInOutSubTitle'),
                    geofencingDailyNotificationTitle: localize('messages', 'geofencingDailyNotificationTitle'),
                    geofencingDailyNotificationSubTitle: localize('messages', 'geofencingDailyNotificationSubTitle')
                };
                $scope.hide = function () {
                    $mdDialog.hide();
                };

                $scope.cancel = function () {
                    $mdDialog.cancel();
                };

                $scope.answer = function (answer) {
                    $mdDialog.hide(answer);
                };
            }

            $scope.initialize = function (selected, isAuthenticated) {
                if (authService.isAuthenticated()) {
                    $rootScope.$broadcast('update-notifications-count', {});
                }
                $scope.sync = selected === 'Messages';
				try {
					
                    if (authService.isAuthenticated()) {
                        $scope.load();
                    }  
                } catch (err) {
                    $scope.loading = false;
                }
                $scope.seenPlacesBadge = true;
                
			};
        }
    ]);
}());
