/**
 * Created by tom on 7/30/15.
 */

var dbInit=require("../config/dbInit");
var sctid=require("../model/sctid");
var security = require("./../blogic/Security");

module.exports.getStats = function getStats(username, callback){
    dbInit.getDB(function (err, pdb, model) {
        if (err)
            throw err;
        else{
            var result = {
                namespaces:{},
                schemes: 2,
                users: 0
            }, adminU = false, users = [];
            var added = function (userToAdd){
                var found = false;
                users.forEach(function(userAdded){
                    if (userAdded == userToAdd)
                        found = true;
                });
                return found;
            };
            security.admins.forEach(function(admin){
                if (admin == username)
                    adminU = true;
                if (!added(admin))
                    users.push(admin);
            });
            security.users.forEach(function(user){
                if (!added(user))
                    users.push(user);
            });
            result.users = users.length;
            if (adminU){
                model.schemeIdBase.count({}, function (err, schemesCount) {
                    if (err)
                        callback(err, null);
                    else{
                        result.schemes = schemesCount;
                        model.namespace.find({}, function (err, namespaceResult) {
                            if (err)
                                callback(err, null);
                            else{
                                result.namespaces.total = namespaceResult.length;
                                var total = namespaceResult.length, done = 0;
                                if (total > 0){
                                    namespaceResult.forEach(function(namespaceR){
                                        sctid.count({namespace: namespaceR.namespace}, function (err, namespaceCount){
                                            if (err)
                                                callback(err, null);
                                            else{
                                                done++;
                                                result.namespaces[namespaceR.organizationName + " (" + namespaceR.namespace + ")"] = namespaceCount;
                                                if (total == done){
                                                    callback(null, result);
                                                }
                                            }
                                        });
                                    });
                                }else
                                    callback(null, result);
                            }
                        });
                    }
                });
            }else{
                var otherGroups = [username], namespacesFromGroup = [];
                security.getGroups(username, function(err, resultG) {
                    if (!err && resultG && resultG.length) {
                        resultG.forEach(function(loopGroup){
                            if (loopGroup.substr(0, loopGroup.indexOf("-")) == "namespace")
                                namespacesFromGroup.push(loopGroup.substr(loopGroup.indexOf("-") + 1));
                            else otherGroups.push(loopGroup);
                        });
                    }
                    model.permissionsScheme.count({username: otherGroups}, function (err, schemesCount) {
                        if (err)
                            callback(err, null);
                        else{
                            result.schemes = schemesCount;
                            model.permissionsNamespace.find({username: otherGroups}, function (err, namespaceResult) {
                                if (err)
                                    callback(err, null);
                                else{
                                    result.namespaces.total = namespaceResult.length;
                                    if (namespacesFromGroup.length){
                                        namespacesFromGroup.forEach(function (namespLoop) {
                                            namespaceResult.push({namespace: namespLoop});
                                        });
                                    }
                                    var total = namespaceResult.length, done = 0;
                                    if (total > 0){
                                        namespaceResult.forEach(function(namespaceR){
                                            sctid.count({namespace: namespaceR.namespace}, function (err, namespaceCount){
                                                if (err)
                                                    callback(err, null);
                                                else{
                                                    done++;
                                                    result.namespaces[namespaceR.organizationName + " (" + namespaceR.namespace + ")"] = namespaceCount;
                                                    if (total == done){
                                                        callback(null, result);
                                                    }
                                                }
                                            });
                                        });
                                    }else
                                        callback(null, result);
                                }
                            });
                        }
                    });
                });
            }
        }
    });
};
@@ -4079,39 +4079,36 @@
                 
-result.
+if (
 namespaces.total
@@ -4105,40 +4105,26 @@
 aces
-.total = namespaceResult
+FromGroup
 .length
-;
+)%7B
 %0A   
@@ -4148,36 +4148,36 @@
                 
-if (
+    
 namespacesFromGr
@@ -4172,39 +4172,62 @@
 spacesFromGroup.
-length)
+forEach(function (namespLoop) 
 %7B%0A              
@@ -4256,60 +4256,376 @@
     
-namespacesFromGroup.forEach(function (namespLoop) %7B%0A
+    var found = false;%0A                                            namespaceResult.forEach(function(namesp)%7B%0A                                                if (namesp.namespace == namespLoop)%0A                                                     found = true;%0A                                            %7D);%0A                                            if (!found)%0A    
     
@@ -4785,32 +4785,118 @@
               %7D%0A
+                                    result.namespaces.total = namespaceResult.length;%0A
                 
@@ -5522,89 +5522,328 @@
     
-result.namespaces%5BnamespaceR.organizationName + %22 (%22 + namespaceR.namespace + %22)%22
+var name = namespaceR.namespace;%0A                                                    if (namespaceR.organizationName)%0A                                                        name = namespaceR.organizationName + %22 (%22 + namespaceR.namespace + %22)%22;%0A                                                    result.namespaces%5Bname
 %5D = 

Test new group permissions