/**
 * Created by alo on 7/15/15.
 */
var AtlassianCrowd = require('atlassian-crowd');

var options = {
    "crowd": {
        "base": process.env.crowd_url
    },
    "application": {
        "name": process.env.crowd_app_name,
        "password": process.env.crowd_app_password
    }
};

var authenticationCache = {};
module.exports.admins = [];
module.exports.users = [];

var crowd = new AtlassianCrowd(options);

//crowd.ping(function (err, res) {
//    if(err) {
//        throw err;
//    }
//    else {
//        console.log(res)
//    }
//});

module.exports.createSession = function createSession (username, password, callback) {
    crowd.session.create(username, password, function (err, token) {
        if(err) {
            callback(err);
        } else {
            callback(null, token);
        }
    });
};

module.exports.authenticate = function authenticate (token, callback) {
    if (authenticationCache[token]) {
        var now = Date.now();
        var ago = (Date.now() - authenticationCache[token].timestamp);
        if (ago < (60000*60)) {
            callback(null, authenticationCache[token].res);
        } else {
            delete authenticationCache[token];
            crowd.session.authenticate(token, function (err, res) {
                if(err) {
                    callback(err);
                } else {
                    authenticationCache[token] = {
                        timestamp: now,
                        res: res
                    };
                    callback(null, res);
                }
            });
        }
    } else {
        crowd.session.authenticate(token, function (err, res) {
            if(err) {
                callback(err);
            } else {
                authenticationCache[token] = {
                    timestamp: now,
                    res: res
                };
                callback(null, res);
            }
        });
    }
};

module.exports.destroySession = function destroySession (token, callback) {
    crowd.session.destroy(token, function (err) {
        if(err) {
            callback(err);
        } else {
            callback(null);
        }
    });
};

module.exports.findUser = function findUser (username, callback) {
    crowd.user.find(username, function(err, res) {
        if(err) {
            callback(err);
        } else {
            callback(null, res);
        }
    });
};

module.exports.getGroups = function getGroups (username, callback) {
    crowd.user.groups(username, function(err, res) {
        if(err) {
            callback(err);
        } else {
            callback(null, res);
        }
    });
};

module.exports.getGroupUsers = function getGroupUsers (groupName, callback) {
    crowd.groups.directmembers(groupName, function(err, res) {
        if(err) {
            callback(err);
        } else {
            if (groupName == "component-identifier-service-admin") {
                module.exports.admins = res;
            } else if (groupName == "component-identifier-service-user") {
                module.exports.users = res;
            }
            callback(null, res);
        }
    });
};

module.exports.addMember = function addMember (username, groupName, callback) {
    crowd.groups.addmember(username, groupName, function(err, res) {
        if(err) {
            callback(err);
        } else {
            updateGroupCache();
            callback(null, {});
        }
    });
};

module.exports.removeMember = function removeMember (username, groupName, callback) {
    crowd.groups.removemember(username, groupName, function(err, res) {
        if(err) {
            callback(err);
        } else {
            updateGroupCache();
            callback(null, {});
        }
    });
};

module.exports.allUsers = function allUsers (callback) {
    crowd.search('user', '', function (err, res) {
        if(err) {
            throw err;
        }
        else {
            callback(null, res);
        }
    });
};

module.exports.searchUsers = function searchUsers (searchString, callback) {
    crowd.search('user', 'name="' + searchString + '*"', function (err, res) {
        if(err) {
            throw err;
        }
        else {
            callback(null, res);
        }
    });
};

var updateGroupCache = function() {
    module.exports.getGroupUsers("component-identifier-service-admin", function(err,res) {
        // cache only
    });
    module.exports.getGroupUsers("component-identifier-service-user", function(err,res) {
        // cache only
    });
};
updateGroupCache();

@@ -93,24 +93,25 @@
 options = %7B%0A
+%09
     %22crowd%22:
@@ -113,23 +113,27 @@
 owd%22: %7B%0A
+%09
        
+ //
  %22base%22:
@@ -159,15 +159,82 @@
 url%0A
+%09           %22base%22: %22https://dev-crowd.ihtsdotools.org/crowd/%22%0A%09 
     %7D,%0A
+%09 
     
@@ -250,24 +250,25 @@
 ion%22: %7B%0A
+%09
         
 %22name%22: 
@@ -259,16 +259,19 @@
         
+ //
 %22name%22: 
@@ -298,24 +298,25 @@
 p_name,%0A
+%09
         
 %22passwor
@@ -307,16 +307,19 @@
         
+ //
 %22passwor
@@ -353,16 +353,93 @@
 assword%0A
+%09         %22name%22: %22component-id-service%22,%0A%09         %22password%22: %22snomedid%22%0A%09 
     %7D%0A%7D;

revert to old embedded vars