"use strict";

let express = require('express'),
    http = require('http'),
    steamCommunity = require('steamcommunity');

let steamId = steamCommunity.SteamID;

const tests = {
    'steamID' : 'STEAM_0:0:61441014',
    'steamID3' : '[U:1:122882028]',
    'steamID64' : '76561198083147756',
    'customURL' : 'daerdemandt',
    'name' : 'Daerdemandt',
    'profile' : 'http://steamcommunity.com/profiles/76561198083147756'
};


let app = express();

let urlToSteamId = function (url) {};

let stringToSteamId = function(string, cb) {
    try {
        let id = new steamId(string);
        cb(null, {'id64':id.toString()});
    } catch(error) {
        if (error.stack.startsWith('Error: Unknown SteamID input format')) {
            cb('Unsupported format, but we are working on it'); //TODO: support logins, nicknames and URLs
        } else {
            cb(error);
        }
    };
};


app.get('/api/arbitraryStringToSteamId', function(req, res, next) {
    let ret = function(error, data) {
        if (error) {
            res.status(500).send({'error' : error})
        } else {
            res.send(data)
        }
    };
    stringToSteamId(decodeURIComponent(req.query.string.trim()), ret);
});

app.get('/', function(req, res, next) {
    res.sendFile( '/static/' + 'index.html');
});

function processConvertQuery(request, response)
{
	response.status(501).send("STUB!");
}


app.post('/api/convert', processConvertQuery);

http.createServer(app).listen(process.env.PORT || 8080, function() {
    console.log('Listening on port ' + (process.env.PORT || 8080));
});

@@ -74,17 +74,18 @@
 '),%0A    
-s
+CS
 teamComm
@@ -128,19 +128,31 @@
 let 
-s
+S
 team
-Id = s
+Community = new CS
 team
@@ -164,17 +164,13 @@
 nity
-.SteamID;
+();%0A%0A
 %0A%0Aco
@@ -547,42 +547,171 @@
     
-try %7B%0A        let id = new s
+const notFound = 'Error: The specified profile could not be found.';%0A    let tryAsId = function () %7B%0A        try %7B%0A            let id = new CSteamCommunity.S
 teamI
-d
+D
 (str
@@ -728,46 +728,57 @@
     
-cb(null, %7B'id64':id.toString()%7D
+    SteamCommunity.getSteamUser(id, cb
 );%0A    
+    
 %7D ca
@@ -782,32 +782,36 @@
  catch(error) %7B%0A
+    
         if (erro
@@ -883,16 +883,20 @@
         
+    
 cb('Unsu
@@ -959,34 +959,201 @@
 ort 
-logins, nicknames and URLs
+URLs%0A            %7D else %7B%0A                cb(error);%0A            %7D%0A        %7D;%0A    %7D;%0A%0A    SteamCommunity.getSteamUser(string, function(err, result) %7B%0A        if (!err) %7B%0A            cb(result);
 %0A   
@@ -1174,34 +1174,102 @@
             
-cb
+if 
 (err
-or);
+.stack.startsWith(notFound)) %7B%0A                tryAsId();%0A            %7D
 %0A        %7D%0A 
@@ -1272,16 +1272,17 @@
  %7D%0A    %7D
+)
 ;%0A%7D;%0A%0A%0Aa

Support nicknames too