'use strict';


/*global nodeca, _*/


// 3rd-party
var Store = require('nlib').Settings.Store;
var async = require('nlib').Vendor.Async;


////////////////////////////////////////////////////////////////////////////////


// Helper to fetch usergroups by IDs
//
function fetchUserGroups(ids, callback) {
  nodeca.models.users.UserGroup.find().where('_id').in(ids).exec(callback);
}


// Memoized version of fetchUserGroups helper
//
var fetchUserGroupsCached = nodeca.components.memoizee(fetchUserGroups, {
  // momoizee options. revalidate cache after 30 sec
  async:  true,
  maxAge: 30000
});


////////////////////////////////////////////////////////////////////////////////


var UserGroupStore = new Store({
  get: function (key, params, options, callback) {
    var func = options.skipCache ? fetchUserGroups : fetchUserGroupsCached;

    func(params.usergroup_ids, function (err, grps) {
      if (err) {
        callback(err);
        return;
      }

      var values = grps.map(function (grp) {
        var value = grp.settings && grp.settings[key];

        // map to the list of settings key value
        return undefined === value ? null : value;
      }).filter(function (value) {
        // leave only those who have ones
        return !!value;
      });

      // push default value
      values.push({ value: UserGroupStore.getDefaultValue(key) });

      callback(null, Store.mergeValues(values));
    });
  },
  set: function (values, params, callback) {
    fetchUserGroups(params.usergroup_ids, function (err, groups) {
      if (err) {
        callback(err);
        return;
      }

      // leave only those params, that we know about
      values = _.pick(values || {}, UserGroupStore.keys);

      // set values for each usergroup
      async.forEach(groups, function (group, next) {
        if (!group.settings) {
          group.settings = {};
        }

        _.each(values, function (opts, key) {
          group.settings[key] = { value: opts.value, force: !!opts.value };
        });

        group.markModified('settings');
        group.save(next);
      }, callback);
    });
  },
  params: {
    usergroup_ids: {
      type: 'array',
      required: true,
      minItems: 1
    }
  }
});


////////////////////////////////////////////////////////////////////////////////


module.exports = UserGroupStore;

@@ -272,23 +272,27 @@
  fetchUs
-e
 rGr
-oup
+pSetting
 s(ids, c
@@ -340,16 +340,45 @@
 p.find()
+%0A    .select('settings')%0A    
 .where('
@@ -390,16 +390,21 @@
 .in(ids)
+%0A    
 .exec(ca
@@ -472,31 +472,35 @@
 %0Avar fetchUs
-e
 rGr
-oup
+pSetting
 sCached = no
@@ -531,23 +531,27 @@
 (fetchUs
-e
 rGr
-oup
+pSetting
 s, %7B%0A  /
@@ -847,23 +847,27 @@
  fetchUs
-e
 rGr
-oup
+pSetting
 s : fetc
@@ -869,23 +869,27 @@
  fetchUs
-e
 rGr
-oup
+pSetting
 sCached;
@@ -1032,16 +1032,31 @@
 s = 
-grps.map
+%5B%5D;%0A%0A      grps.forEach
 (fun
@@ -1081,20 +1081,12 @@
     
-var value = 
+if (
 grp.
@@ -1114,18 +1114,19 @@
 ngs%5Bkey%5D
-;%0A
+) %7B
 %0A       
@@ -1130,38 +1130,34 @@
     
-// map to the list of 
+  values.push(grp.
 settings
  key
@@ -1156,18 +1156,15 @@
 ings
- key value
+%5Bkey%5D);
 %0A   
@@ -1172,151 +1172,9 @@
     
-return undefined === value ? null : value;%0A      %7D).filter(function (value) %7B%0A        // leave only those who have ones%0A        return !!value;
+%7D
 %0A   
@@ -1395,23 +1395,27 @@
  fetchUs
-e
 rGr
-oup
+pSetting
 s(params
@@ -1447,18 +1447,16 @@
 (err, gr
-ou
 ps) %7B%0A  
@@ -1691,18 +1691,16 @@
 rEach(gr
-ou
 ps, func
@@ -1707,18 +1707,16 @@
 tion (gr
-ou
 p, next)
@@ -1730,17 +1730,10 @@
     
-if (!grou
+gr
 p.se
@@ -1738,34 +1738,21 @@
 settings
-) %7B%0A        
  
+=
  gr
-ou
 p.settin
@@ -1758,23 +1758,14 @@
 ngs 
-=
+%7C%7C
  %7B%7D;
-%0A        %7D
 %0A%0A  
@@ -1820,18 +1820,16 @@
       gr
-ou
 p.settin
@@ -1839,16 +1839,28 @@
 key%5D = %7B
+%0A           
  value: 
@@ -1870,16 +1870,28 @@
 s.value,
+%0A           
  force: 
@@ -1902,16 +1902,26 @@
 ts.value
+%0A         
  %7D;%0A    
@@ -1939,18 +1939,16 @@
       gr
-ou
 p.markMo
@@ -1977,18 +1977,16 @@
       gr
-ou
 p.save(n

Improve userstore