'use strict';

var co = require('co');
var psn = require('../psn');
var md = require('../markdown');
var moment = require('moment-timezone');

function exec(cmd) {

    var msg = cmd.msg;
    var bot = cmd.bot;
    var name = cmd.args[0];
    var now = moment();

    // mentions take precedent
    if (msg.mentions.length > 0) {
        name = msg.mentions[0].username;
    } 

    if (!name) {
        return bot.sendMessage(msg, "did you forget something?");
    }

    var gamer = psn.lookup(name);
    if(!gamer) {
        return bot.sendMessage(msg, "Sorry, could not find **"+md.escape(name)+"**");
    }

    var localtime;
    // figure out the Localtime
    if (gamer.tz && moment.tz.zone(gamer.tz)) {
        localtime = " Localtime: " + now.tz(gamer.tz).format("HH:mm (Z z)");
    } else {
        localtime = "  Timezone: " + gamer.tz;
    }

    return bot.sendMessage(msg, "```ruby\n"+
        "Discord ID: @" + gamer.discord + "\n"+
        "       PSN: " + gamer.psn + "\n"+
        "     Games: " +  gamer.games.join(", ") + "\n" +
        localtime + "```");

}

module.exports = {
    desc: 'Lookup PSN name for a discord account',
    name: 'psn',
    usage: '`psn <@discord-id>`',
    alias: [],
    exec: exec
};

@@ -255,16 +255,46 @@
 oment();
+%0A    var joinedAt = 'Unknown';
 %0A%0A    //
@@ -875,24 +875,261 @@
 .tz;%0A    %7D%0A%0A
+    // get joined at timestamp%0A    var user = bot.users.get(%22username%22, gamer.discord);%0A    if(user) %7B%0A        var detailsOf = msg.channel.server.detailsOfUser(user);%0A        joinedAt = new Date(detailsOf.joinedAt).toUTCString();%0A    %7D%0A%0A
     return b
@@ -1310,16 +1310,59 @@
  %22%5Cn%22 +%0A
+        %22 Joined At: %22 + joinedAt + %22%5Cn%22 +%0A
         

Fix for enhancement #6 Add 'Joined At' timestamp to psn output