//
//   Copyright 2009-2014 Ilkka Oksanen <iao@iki.fi>
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing,
//   software distributed under the License is distributed on an "AS
//   IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
//   express or implied.  See the License for the specific language
//   governing permissions and limitations under the License.
//

'use strict';

var wrapper = require('co-redis'),
    redis = wrapper(require('redis').createClient()),
    outbox = require('../server/lib/outbox.js'),
    windowHelper = require('../server/lib/windows'),
    networkList = require('./networks');

// TDB Consider options:
//
// timestamp
// type
// rememberurl
// hidden

exports.broadcast = function *(userId, network, nick, cat, text) {
    var windowIds = yield windowHelper.getWindowIdsForNetwork(userId, network);

    for (var i = 0; i < windowIds.length; i++) {
        yield processMessage(userId, windowIds[i], nick, cat, text, false);
    }
};

exports.send = function *(userId, network, group, nick, cat, text) {
    var windowId = yield windowHelper.getWindowId(userId, network, group);
    yield processMessage(userId, windowId, nick, cat, text, false);
};

exports.save = function *(userId, windowId, nick, cat, text) {
    yield processMessage(userId, windowId, nick, cat, text, true);
};

exports.sendNicks = function *(userId) {
    var command = {
        id: 'NICK'
    };

    var redisParams = [ 'user:' + userId ];
    var networks = [];

    for (var network in networkList) { /* jshint -W089 */
        redisParams.push('currentNick:' + network);
        networks.push(network);
    }

    var nicks = yield redis.hmget.apply(redis, redisParams);

    for (var i = 0; i < networks.length; i++) {
        if (nicks[i] !== null) {
            command[networks[i]] = nicks[i];
        }
    }

    yield outbox.queue(userId, command);
};

function *processMessage(userId, windowId, nick, cat, text, saveOnly) {
    if (!windowId) {
        return;
    }

    var command = JSON.stringify({
        id: 'ADDTEXT',
        windowId: windowId,
        nick: nick,
        body: text,
        cat: cat,
        ts: 209,
        type: 0
    });

    yield redis.lpush('windowmsgs:' + userId + ':' + windowId, command);

    if (!saveOnly) {
        yield outbox.queue(userId, command);
    }
}

@@ -650,59 +650,16 @@
 var 
-wrapper = require('co-redis'),%0A    redis = wrapper(
+redis = 
 requ
@@ -663,16 +663,18 @@
 equire('
+./
 redis').
@@ -687,17 +687,16 @@
 Client()
-)
 ,%0A    ou
@@ -1118,38 +1118,39 @@
    yield process
-Messag
+TextLin
 e(userId, window
@@ -1172,23 +1172,16 @@
 at, text
-, false
 );%0A    %7D
@@ -1338,38 +1338,39 @@
    yield process
-Messag
+TextLin
 e(userId, window
@@ -1392,15 +1392,8 @@
 text
-, false
 );%0A%7D
@@ -1404,19 +1404,29 @@
 xports.s
-ave
+endByWindowId
  = funct
@@ -1481,30 +1481,31 @@
 ield process
-Messag
+TextLin
 e(userId, wi
@@ -1531,14 +1531,8 @@
 text
-, true
 );%0A%7D
@@ -1571,16 +1571,27 @@
 *(userId
+, sessionId
 ) %7B%0A    
@@ -2051,24 +2051,74 @@
    %7D%0A    %7D%0A%0A
+    sessionId = sessionId; // TBD Send to queue()%0A
     yield ou
@@ -2171,14 +2171,15 @@
 cess
-Messag
+TextLin
 e(us
@@ -2213,18 +2213,8 @@
 text
-, saveOnly
 ) %7B%0A
@@ -2463,136 +2463,65 @@
 dis.
-lpush('windowmsgs:' + userId + ':' + windowId, command);%0A%0A    if (!saveOnly) %7B%0A        yield outbox.queue(userId, command);%0A    %7D
+run('processTextLine', %5B%5D, %5B userId, windowId, command %5D);
 %0A%7D%0A

Add sendByWindowId method.