var glimpse = require('glimpse'),
    _users = {},
    _userSelected = null;

function usersChanged() {
    glimpse.emit('shell.request.user.detail.changed', {
            allUsers: _users,
            selectedUserId: _userSelected
        });
}

(function() {
    function userClear() {
        _users[_userSelected].selected = false;
        _userSelected = null;

        usersChanged();
    }

    glimpse.on('shell.request.user.clear.selected', userClear);
})();

(function() {
    function userSwitch(payload) {
        var userId = payload.userId,
            oldUserId = _userSelected,
            user = _users[userId];

        if (oldUserId) {
            var oldUser = _users[oldUserId];
            if (oldUser) {
                oldUser.selected = false;
            }
        }
        if (user) {
            user.selected = true;
        }

        _userSelected = userId;

        usersChanged();
    }

    glimpse.on('shell.request.user.selected', userSwitch);
})();

(function() {
    // TODO: Need to update to make sure it can work with out of order/old
    //       requests coming in.
    // TODO: Timeouts should probably come from config
    // TODO: Should probably be abstracted out into its own module
    var manageRequest = (function() {
            function removeRequest(user, request) {
                var index = user.latestRequests.indexOf(request);
                if (index > -1) {
                    user.latestRequests.splice(index, 1);
                }

                usersChanged();
            }

            return function(user, rawRequest) {
                if (rawRequest) {
                    var request = {
                            id: rawRequest.id,
                            uri: rawRequest.uri
                        };

                    user.latestRequests.unshift(request);

                    setTimeout(function() { removeRequest(user, request); }, 5000);
                }
            };
        })(),
        manageOnline = (function() {
            function setOffline(user) {
                user.online = false;

                usersChanged();
            }

            return function(user, rawRequest) {
                user.lastActive = rawRequest.dateTime;
                user.online = true;

                if (user.onlineCallback) {
                    clearTimeout(user.onlineCallback);
                }

                user.onlineCallback = setTimeout(function() { setOffline(user); }, 12000);
            };
        })();

    function createUser(rawUser) {
        return {
                details: rawUser,
                latestRequests: [],
                lastActive: null,
                online: true,
                selected: false
            };
    }

    function dataFound(payload) {
        // TODO: This needs to be cleaned up bit messy atm but will do
        var rawRequests = payload.newRequests;
        for (var i = rawRequests.length - 1; i >= 0; i--) {
            var rawRequest = rawRequests[i],
                rawUser = rawRequest.user,
                user = _users[rawUser.id];

            if (user === undefined) {
                user = createUser(rawUser);
                _users[rawUser.id] = user;
            }

            manageRequest(user, rawRequest);
            manageOnline(user, rawRequest);
        }

        usersChanged();
    }

    // External data coming in
    glimpse.on('data.user.detail.found', dataFound);
})();

@@ -80,17 +80,23 @@
 unction 
-u
+notifyU
 sersChan
@@ -273,24 +273,24 @@
 unction 
-userClea
+clearUse
 r() %7B%0A  
@@ -366,33 +366,39 @@
  null;%0A%0A        
-u
+notifyU
 sersChanged();%0A 
@@ -459,16 +459,16 @@
 d', 
-userClea
+clearUse
 r);%0A
@@ -501,26 +501,26 @@
 unction 
-u
 se
-rSwitch
+lectUser
 (payload
@@ -896,33 +896,39 @@
 serId;%0A%0A        
-u
+notifyU
 sersChanged();%0A 
@@ -983,18 +983,18 @@
 d', 
-u
 se
-rSwitch
+lectUser
 );%0A%7D
@@ -1518,33 +1518,39 @@
                 
-u
+notifyU
 sersChanged();%0A 
@@ -2117,33 +2117,39 @@
                 
-u
+notifyU
 sersChanged();%0A 
@@ -2793,25 +2793,25 @@
 unction 
-dataFound
+foundUser
 (payload
@@ -3372,17 +3372,23 @@
         
-u
+notifyU
 sersChan
@@ -3477,17 +3477,17 @@
 d', 
-dataFound
+foundUser
 );%0A%7D

Refactor user store to to bring it in line with new naming outlines