(function () {
    'use strict';

    var oneKeyApp = angular.module('oneKeyApp');

    oneKeyApp.controller('messagesController', [
        '$filter', '$scope', '$window', '$rootScope', 'urls', 'notifications', 'dates',
        function messagesController($filter, $scope, $window, $rootScope, metUrls, metNotifications, metDates) {
            $scope.notifications = [];
            $scope.isLoading = true;
            $scope.noNotifications = false;
            $scope.unreadNotifications = 0;
            $scope.dateFormat = metDates.date(true);

            $scope.loadNotifications = function() {
                metNotifications.getAll(
                    function (data) {
                        $scope.notifications = data.items;
                        $scope.unreadNotifications = $filter('filter')($scope.notifications, { acknowledgedOn: null }).length;
                        $scope.isLoading = false;
                        $scope.noNotifications = $scope.notifications.length == 0;
                    },
                    function (data) {
                    }
                );
            }
            

            $scope.getFormattedDate = function(utcDate) {
                return metDates.convertUtcToLocal(utcDate, metDates.date(true));
            }

            $scope.getFormattedTime = function (utcDate) {
                return metDates.convertUtcToLocal(utcDate, metDates.shortTime);
            }

            $scope.goToItem = function (itemId) {
                var url = metUrls('inventory').concat('/' + itemId);
                $window.location.href = url;
            }
            
            $scope.acknowledge = function (message) {
                metNotifications.acknowledge(message,
                    function (data) {
                        $rootScope.$broadcast('update-notifications-count', { });
                        message.acknowledgedOn = new Date();
                        $scope.unreadNotifications--;
                    }, function () {
                    }
                );
            }

            $scope.showIcon = function (iconType, notificationType) {
                var show = false;

                switch (iconType) {
                    case "wrench":
                        var serviceTypes = [ 'ServiceAlert', 'ServiceDateAlert', 'ServiceDateWarning', 'ServiceWarning' ];
                        show = serviceTypes.indexOf(notificationType) > -1;
                        break;
                    case "triangle":
                        var serviceTypes = [ 'CoinCellAlert', 'CoinCellWarning' ];
                        show = serviceTypes.indexOf(notificationType) > -1;
                        break;
                    case "location":
                        var serviceTypes = [ 'FoundMissingItem' ];
                        show = serviceTypes.indexOf(notificationType) > -1;
                        break;
                }

                return show;
            }

            $scope.dismiss = function (message) {
                metNotifications.dismiss(message.id,
                    function (data) {
                        $rootScope.$broadcast('update-notifications-count', {});
                        var index = $scope.notifications.indexOf(message);
                        $scope.notifications.splice(index, 1);

                        if (!message.acknowledgedOn) {
                            $scope.unreadNotifications--;
                        }

                        $scope.noNotifications = $scope.notifications.length == 0;

                    }, function () {
                    }
                );
            }

            $scope.initialize = function () {
                $scope.loadNotifications();
            };
        }
    ]);
}());
