EXPORTED_SYMBOLS = ["module"];
var module = function (application) {
var log = function (message) {
application.logger.debug("[VK requests] " + message);
}
;
var Requests = function () {

}
;
Requests.prototype = {
constructor: Requests,
init: function () {
var requests = this;
var mailman = application.mailman;
mailman.addSubscriber({
subject: "please-update",
subscriber: {
receive: function ({body: target}) {
switch (target) {
case "all":
requests.getMessages();
requests.getFriendRequests();
break;
case "messages":
requests.getMessages();
break;
case "friends":
requests.getFriendRequests();
break;
default:
log("Unknown target for update: \"" + target + "\".");
}

}
}});
mailman.addSubscriber({
subject: "please-get-profiles",
subscriber: {
receive: function ({body: uids}) {
var params = {
};
if (null !== uids)
{
params.uids = uids;
}

requests.send("profiles",params);
}
}});
}
,
getMessages: function () {
var requests = this;
var mailman = application.mailman;
requests.send("messages",function (response) {
if (0 === response.total)
{
mailman.send({
subject: "new-messages",
body: response});
return;
}

var uids = [];
var messages = response.data;
messages.forEach(function (message) {
if (message.uid)
uids.push(message.uid);
}
);
requests.send("profiles",{
uids: uids.join(",")},function (profiles) {
messages.forEach(function (message) {
profiles.some(function (profile) {
if (profile.uid === message.uid)
{
message._profile = profile;
return true;
}

}
);
}
);
mailman.send({
subject: "new-messages",
body: response});
}
);
}
);
}
,
getFriendRequests: function () {
var requests = this;
var mailman = application.mailman;
requests.send("friends",function (list) {
if (0 === list.total)
{
mailman.send({
subject: "new-friends",
body: list});
return;
}

var uids = list.data;
requests.send("profiles",{
uids: uids},function (profiles) {
var users = [];
uids.forEach(function (uid) {
var user = {
uid: uid,
_profile: {
}};
profiles.some(function (profile) {
if (user.uid === profile.uid)
{
user._profile = profile;
return true;
}

}
);
users.push(user);
}
);
list.data = users;
var mailman = application.mailman;
mailman.send({
subject: "new-friends",
body: list});
}
);
}
);
}
,
send: function (name, _additionalParams, _callback) {
var requestConfig = this.config.requests[name];
var {method: method, params: params, emit: emit, filter: filter} = requestConfig;
var additionalParams = _additionalParams;
var callback = _callback;
if ("function" == typeof additionalParams)
{
callback = additionalParams;
additionalParams = null;
}

for(let paramName in additionalParams) {
if (null === params)
{
params = {
};
}

params[paramName] = additionalParams[paramName];
}

var vkAPI = application.vkAPI;
var mailman = application.mailman;
var requests = this;
vkAPI.request(method,params,function (data) {
if ("undefined" != typeof data.error)
{
log(JSON.stringify(data));
return;
}

var response = data.response;
if ("function" == typeof filter)
{
response = filter.call(requestConfig,response);
}

if ("undefined" != typeof emit)
{
mailman.send({
subject: emit,
body: response});
}

if ("function" == typeof callback)
callback(response);
}
,function () {
log("Request \"" + name + "\" failed.");
mailman.send({
subject: "request-failed",
body: name});
}
);
}
,
config: {
"requests": {
"messages": {
"method": "messages.get",
"params": {
"filters": 1},
filter: function (data) {
var messages = {
total: 0,
received: 0,
data: null};
if (0 !== data)
{
messages.total = data[0];
messages.data = data.slice(1);
messages.received = messages.data.length;
}

return messages;
}
},
"friends": {
"method": "friends.getRequests",
"params": null,
filter: function (data) {
var requests = {
total: 0,
received: 0,
data: null};
if ("undefined" != typeof data.length)
{
requests.data = data;
requests.total = requests.data.length;
requests.received = requests.data.length;
}

return requests;
}
},
"profiles": {
"method": "getProfiles",
"params": {
"fields": "online,photo,sex"},
"emit": "profiles"}}}};
return Requests;
}
;
