import irc from 'irc';
import request from 'request';
import config from '../config/config';

// IRC Client
let client = new irc.Client(config.server.address, config.bot.nick, {
    userName: config.bot.userName,
    realName: config.bot.realName,
    port: config.server.port,
    debug: true,
    channels: config.server.channels
});

// Listen for messages
client.addListener('message', function(nick, to, text, message) {
    if (text.startsWith('!') || to === client.nick) {
        processCommand(text, nick, to);
    } else {
        console.log('not a command');
    }
});

/**
 * Processes incomming commands
 *
 * @param {string} text - message text
 * @param {string} nick - nick of the sender
 * @param {string} to - message recipient
 */
function processCommand(text, nick, to) {
    let channelsUrl = 'http://diamondclub.tv/api/channelsv2.php';
    let cmd = text;

    let replyTo = to;
    if (to === client.nick) {
        replyTo = nick;
    } else {
        cmd = cmd.slice(1).trim();
    }

    switch (cmd) {
        case 'now':
            getUrlContents(channelsUrl, function(response) {
                let replyMsg = 'Nothing is live';
                let channels = JSON.parse(response).assignedchannels;

                if (channels.length !== null && channels.length > 0) {
                    replyMsg = '';
                    for (var i = 0; i < channels.length; i++) {
                        let ch = channels[i];
                        replyMsg += `\nChannel ${ch.channel}: ${ch.friendlyalias}`;
                    }
                }
                client.notice(replyTo, replyMsg);
            });
            break;
        case 'next':
            getGoogleCalendar('a5jeb9t5etasrbl6dt5htkv4to@group.calendar.google.com', function(events) {
                let replyMsg = `Next Scheduled Show: ${events[0].summary} - ${events[0].start.dateTime}`;
                client.notice(replyTo, replyMsg);
            });
            break;
        case 'schedule':
            getGoogleCalendar('a5jeb9t5etasrbl6dt5htkv4to@group.calendar.google.com', function(events) {
                let replyMsg = 'Scheduled Shows for the Next 48 hours:';
                for (let i = 0; i < events.length; i++) {
                    replyMsg += `\n${events[i].summary} - ${events[i].start.dateTime}`;
                }
                client.notice(replyTo, replyMsg);
            });
            break;
        default:
            console.log('default');
    }
}

/**
 * Callback for handling url response
 *
 * @callback urlCallback
 * @param {string} body
 */

/**
 * Gets contents of a URL
 *
 * @param {string} url - url to getDate
 * @param {urlCallback} callback - callback to run
 */
function getUrlContents(url, callback) {
    request(url, function(error, response, body) {
        if (!error && response.statusCode === 200) {
            if (body === null) {
                console.error(`Error: ${response}`);
            } else {
                callback(body);
            }
        } else {
            console.error(`Error: ${error}`);
        }
    });
}

/**
 * Callback for handling google calendar items result
 *
 * @callback calendarEventsCallback
 * @param {JSON[]} entities
 */

/**
 * Gets google calendar items
 *
 * @param {string} id - google calendar id
 * @param {calendarEventsCallback} callback - callback to run
 */
function getGoogleCalendar(id, callback) {
    let now = new Date();
    let later = new Date();
    later.setDate(later.getDate() + 2);

    let url = `https://www.googleapis.com/calendar/v3/calendars/${id}/events` +
        `?key=${config.google.apiKey}&singleEvents=true&orderBy=startTime` +
        `&timeMin=${now.toISOString()}&timeMax=${later.toISOString()}`;
    getUrlContents(url, function(response) {
        let result = JSON.parse(response);
        callback(result.items);
    });
}

@@ -2492,94 +2492,209 @@
 %0A * 
-Callback for handling url response%0A *%0A * @callback urlCallback%0A * @param %7Bstring%7D body
+Responds to commands%0A * @param %7Bstring%7D replyTo - message target%0A * @param %7Bstring%7D replyMsg - message to send%0A */%0A/**%0A * Gets DCTV live channels%0A * @param %7BdctvLiveChannels%7D callback - callback to run
 %0A */
@@ -2725,19 +2725,16 @@
 f a URL%0A
- *%0A
  * @para
@@ -2780,23 +2780,23 @@
 am %7BurlC
-allback
+ontents
 %7D callba
@@ -3206,138 +3206,8 @@
 %0A%7D%0A%0A
-/**%0A * Callback for handling google calendar items result%0A *%0A * @callback calendarEventsCallback%0A * @param %7BJSON%5B%5D%7D entities%0A */%0A%0A
 /**%0A
@@ -3236,19 +3236,16 @@
 r items%0A
- *%0A
  * @para
@@ -3308,16 +3308,8 @@
 ents
-Callback
 %7D ca
@@ -3824,18 +3824,377 @@
 items);%0A    %7D);%0A
-
 %7D%0A
+%0A// Additional documentation%0A%0A/**%0A * Callback for handling url response%0A * @callback urlContents%0A * @param %7Bstring%7D body%0A */%0A%0A/**%0A * Callback for handling google calendar items result%0A * @callback calendarEvents%0A * @param %7BObject%5B%5D%7D entities%0A */%0A%0A/**%0A * Callback for handling DCTV live channels%0A * @callback dctvLiveChannels%0A * @param %7BObject%5B%5D%7D channels%0A */%0A

update documentation