// Checking server statuses
var util = require(__base+'core/util.js');
var discord = require(__base+'core/discord.js');
var config = require(__base+'core/config.js');
var request = require('request');
var download = require('download');
var telnet = require('expect-telnet');
var nodecraft = require('nodecraft-api')('vegeta897', 'AF0Mi-MWVFr-EuJML-u7KHq');

var _commands = {};

// TODO: Use promises or observables

_commands.mumble = function(data) {
    if(!config.mumble || !config.mumble.email || !config.mumble.apiKey) return;
    var url = 'http://api.commandchannel.com/cvp.json?email=' + config.mumble.email + '&apiKey=' + config.mumble.apiKey;
    request(url, function(err, response, body) {
        if(err) {
            console.log(err);
            return data.reply('Error getting Mumble status...');
        }
        body = JSON.parse(body);
        var channels = [];
        var checkChannel = function(channel) {
            if(channel.users.length > 0) {
                var users = '';
                for(var u = 0; u < channel.users.length; u++) {
                    users += (u === 0 ? ' ' : ', ') + channel.users[u].name;
                }
                channels.push({ name: channel.name, users: users });
            }
            if(channel.channels.length > 0) {
                for(var sc = 0; sc < channel.channels.length; sc++) {
                    checkChannel(channel.channels[sc]);
                }
            }
        };
        checkChannel(body.root);
        var embed = { color: 0xDDDDDD };
        if(channels.length === 0) {
            embed.footer = { text: 'Nobody is in Mumble :(' };
            embed.color = 0xAAAAAA;
        } else {
            embed.fields = [];
            channels.sort(function(a, b) { return a.users.length < b.users.length; }); // Sort longest to shortest
            channels.forEach(function(channel, index) {
                var field = { name: channel.name, value: channel.users };
                if(!(channels.length % 2) || index > 0) { // Short channels if even count or not biggest channel
                    field.inline = true;
                }
                embed.fields.push(field);
            });
        }
        discord.bot.sendMessage({
            to: data.channel, message: '**Mumble Status**', embed
        });
    });
};

_commands.minecraft = function(data) {
    // discord.bot.simulateTyping(data.channel);
    // nodecraft.services.stats('967ae4ea-a1a0-4026-8dd7-618b8581b2b3', (err, server) => {
    //     if(err) {
    //         console.log(err);
    //         return data.reply('Error getting Minecraft server status...');
    //     }
    //     if(server.stats.status === 'online') {
    //         var msg = `Minecraft server is **online!** Uptime: ${server.stats.time}`;
    //         for(var i = 0; i < server.stats.players.length; i++) {
    //             if(i === 0) msg += `\nPlayers online: **${server.stats.players[i].username}**`;
    //             else msg += `, **${server.stats.players[i].username}**`
    //         }
    //         data.reply(msg);
    //     } else {
    //         data.reply('Minecraft server is currently offline... @vegeta897');
    //     }
    // });
    if(!config.minecraft || !config.minecraft.ip || !config.minecraft.port) return;
    discord.bot.simulateTyping(data.channel);
    var url = 'http://mcapi.us/server/status?ip=' + config.minecraft.ip + '&port=' + config.minecraft.port;
    request(url, function(err, response, body) {
        if(err) {
            console.log(err);
            return data.reply('Error getting Minecraft server status...');
        }
        body = JSON.parse(body);
        var status = '*Minecraft server is currently offline*';
        if(body.online) {
            status = '**Minecraft** server is **online**  -  ';
            if(body.players.now) {
                status += `**${body.players.now}** people are playing!`;
            } else {
                status += '*Nobody is playing!*';
            }
        }
        data.reply(status);
    });
};

_commands.starbound = function(data) {
    if(!config.starbound || !config.starbound.statusImage) return;
    discord.bot.simulateTyping(data.channel);
    download(config.starbound.statusImage).then(img => {
        discord.bot.uploadFile({
            to: data.channel, file: img, filename: 'sb.png', message: '**Starbound Server Status**'
        });
    });
};

_commands['7d'] = function(data) {
    if(!config['7d'] || !config['7d'].ip || !config['7d'].telnetPort || !config['7d'].telnetPass) return;
    discord.bot.simulateTyping(data.channel);
    telnet(config['7d'].ip + ':' + config['7d'].telnetPort, [
        {expect: 'Please enter password:', send: config['7d'].telnetPass + '\r' },
        {expect: '\u0000\u0000',  send: "gt\r" },
        {expect: /successful/,  send: "\r" },
        {expect: /version/, send: "\r" },
        {expect: /Day /, out: function(output) {
            output = output.split('\n')[1];
            data.reply(`It is **${output.trim()}** on the 7D server`);
        }, send: "exit\r"}
    ], function(err) {
        if (err) data.reply('*Sorry, the 7D server is unavailable.*');
    });
};

module.exports = {
    commands: _commands,
    help: {
        minecraft: ['Get the Minecraft server status (if configured)'],
        mumble: ['Get the Mumble server status (if configured)'],
        starbound: ['Get the Starbound server status (if configured)'],
        '7d': ['Get the 7 Days to Die server status (if configured)']
    }
};
