var mongoose = require('mongoose'),
    extend = require('mongoose-schema-extend'),
    log = require('./log'),
    env = process.env.NODE_ENV || 'development',   
    config = require('../../config/config')[env],
    util = require('util'),
    md = require('node-markdown').Markdown;

var NamesLogSchema = log.LogSchema.extend({
    nicks: [String]
});

function irssi(namesLog, markdown) {
    markdown = markdown || false;

    var usersChannelPrint = '[Users %s]';
    if (markdown) usersChannel = '[Users **%s**]';
    var usersChannel = util.format(usersChannelPrint, namesLog.channel);

    var users = '';
    var numOps = 0;
    var numHalfops = 0;
    var numVoices = 0;
    var numNormal = 0;
    var numTotal = 0;
    namesLog.nicks.forEach(function(nick) {
	var access = nick.charAt(0);
	switch(access) {
	case '@':
	    numOps++;
	    users += util.format('[%s] ', nick);
	    break;
	case '%':
	    numHalfops++;
	    users += util.format('[%s] ', nick);
	    break;
	case '+':
	    numVoices++;
	    users += util.format('[%s] ', nick);
	    break;
	default:
	    numNormal++;
	    users += util.format('[ %s] ', nick);
	};
	numTotal++;
    });

    var countPrint = '-!- %s: %s: Total of %d nicks [%d ops, %d halfops, %d voices, %d normal]';
    if (markdown) countPrint = '-!- **%s**: **%s**: Total of **%d** nicks [**%d** ops, **%d** halfops, **%d** voices, **%d** normal]';
    var count = util.format(countPrint, config.irc.nick, namesLog.channel, numTotal, numOps, numHalfops, numVoices, numNormal);
    
    return util.format('%s\n%s\n%s', usersChannel, users, count);
};

NamesLogSchema.virtual('irssi').get(function() {
    return irssi(this);
});

NamesLogSchema.virtual('irssi_markdown').get(function() {
    return md(irssi(this, true), true, 'strong|em');
});

mongoose.model('NamesLog', NamesLogSchema);

@@ -1525,22 +1525,110 @@
    %0A    
-return
+var result = util.format('%25s%5Cn%25s%5Cn%25s', usersChannel, users, count);%0A    if (markdown) result =
  util.fo
@@ -1627,36 +1627,40 @@
  util.format('%25s
+  
 %5Cn%25s
+  
 %5Cn%25s', usersChan
@@ -1679,16 +1679,35 @@
 count);%0A
+    return result;%0A
 %7D;%0A%0AName
@@ -1887,16 +1887,19 @@
 trong%7Cem
+%7Cbr
 ');%0A%7D);%0A

fix nameslog newlines in markdown