if( typeof( api ) === "undefined" ){ api = {}; api.ns = "api"; }

api.Group = function(user_dictionary){
  api.User.call(this, user_dictionary); // call parent constructor first
  var attributes = [
    'moderators',
    'banned',
    'members',
    'description',
    'privacy'
  ];
  lu.absorb(this, user_dictionary, attributes);
};
api.Group.construct = function( user_dictionary ){ return new api.Group(user_dictionary); };

api.Group.prototype = new api.User();
api.Group.prototype.constructor = api.Group; // sets the prototype.constructor property to the correct value


/**
 * Get members of a group
 * @param puid Encrypted user ID of the group
 * @param on_success Callback, gets relations and total_count.
 */
api.Group.get_members = function( puid, count, on_success) {
  if( typeof( on_success ) == "undefined" ) {on_remove = function(){};}

  var params = {
      'xaction' : 'members',
      'puid' : puid
  };

  if( typeof( count ) != "undefined" ) {
    params['count'] = count;
  }

  AppBase.post(
    '/groups',
    params,
    function(result) {
      api.Group._on_get_members( on_success, result );
    }
  );
  return false;
}; api.Group._on_get_members = function( callback, result ){ callback( result['members'], result['total_count'] ); };


api.Group.create_many = function( group_dictionaries ){
  var groups = [];
  $.each(group_dictionaries, function(which_group_dictionary, each_group_dictionary){
    groups.push(new api.Group(each_group_dictionary));
  });
  return groups;
};


// channels: channels to submit to the group
// options may include:
//   pcids: pickled ids of channels to also submit to the group
//   nxcids: encrypted ids of channels to also submit to the group
//   on_failure: callback for if the request fails
//   on_success: callback for if the request succeeds
//   [callback]: deprecated - also a success callback
api.Group.prototype.submit_channels = function( channels, options ){
  options = lu.defaulted(options, {});
  if( options.callback && ! options.on_success ){ options.on_success = options.callback; }
  if( channels ){ channels = lu.pluralized(channels); }
  var on_success = function(result){
    if( result && result['error'] === 'group_removed' ){
      bu.relocate('/profile?puid='+this.puid);
    }
    if( options.on_success ){ options.on_success(result); }
  };

  var parameters = {
    'xaction' : 'submit_channels',
    'puid' : this.puid
  };
  nxcids = [];
  pcids = [];
  if( options.nxcids ){ nxcids = options.nxcids; }
  if( options.pcids  ){ pcids  = options.pcids ; }
  if( channels ){
    $.each(channels, function(which, channel){
      if( channel.nxcid ){
        nxcids.push(channel.nxcid);
      }else if( channel.pcid ){
        pcids.push(channel.pcid);
      }
    });
  }
  if( nxcids.length ){ parameters.nxcids = nxcids; }
  if( pcids.length  ){ parameters.pcids  = pcids;  }
  if( options.ref_override  ){ parameters.ref_override  = options.ref_override;  }

  AppBase.post(
    '/groups',
    parameters,
    on_success
  );
};
