/**
 * Module Name: Github 
 * Description: Retrieves interesting Github information
 */
var request = require('request'),
    _ = require('underscore')._;

var github = function(dbot) {
    var commands = {
        '~repocount': function(event) {
        // TODO: add handling for non existent user
            var reqUrl = "https://api.github.com/users/" + event.params[1] + "/repos";
            request(reqUrl, function(error, response, body) {
            var result = JSON.parse(body);
			event.reply(event.params[1] + " has " + result.length + " public repositories.");
        //        event.reply(dbot.t('repocount',{'user': event.params[1], 'count' : result.length}));
            });
        },
        '~repo': function(event) {
            var repo = "";
            if (typeof event.params[1] == 'undefined') {
            //    repo = dbot.config.github.defaultrepo;
                repo = "reality/depressionbot";
            } else {
                repo = event.params[1];
            }

            var reqUrl = "https://api.github.com/";
            reqUrl += "repos/" + repo;

            request(reqUrl, function(error, response, body) {
                var data = JSON.parse(body);
                var str = "";
                str += data["name"] + " is a ";
                if (data["fork"] == true) {
                    str += "forked ";
                }
                str += data["language"];
                str += " repo with ";
                str += data["open_issues"] + " unresolved issues. ";
                str += "[" + data["forks"] + "F " + data["watchers"] + "W]";
                
                event.reply(str);
            });
        },
        '~milestone': function(event) {
            var repo = "reality/depressionbot";
            var reqUrl = "https://api.github.com/repos/";
            reqUrl += repo + "/milestones";

            request(reqUrl, function(error, response, body) {
                var data = JSON.parse(body);
                for (var section in data) {
                    var milestone = data[section];
                    if (milestone["title"] == event.params[1]){
                        var str = "Milestone " + milestone["title"];
                        var progress = milestone["closed_issues"] / (milestone["open_issues"] + milestone["closed_issues"]);
                        progress = Math.round(progress*100);
                        var bar = "[";
                        for (var i = 1; i < 10; i++) {
                            if  ((progress / (i*10)) > 1) {
                                bar += "█";
                            } else {
                                bar += " ";
                            }
                        }
                        bar += "]";
                        str += " is " + bar + progress + "% complete";
                        str += dbot.api.shortenURI("https://github.com/" + repo + "/issues?milestone=" + milestone["number"]);
                    //    str += " https://github.com/" + repo + "/issues?milestone=" + milestone["number"];
                        event.reply(str);
                        break;
                    }
                }
           });
       }, 
    };
    this.commands = commands;

    this.on = 'PRIVMSG';
};

exports.fetch = function(dbot) {
    return new github(dbot);
};

@@ -2834,16 +2834,17 @@
 plete%22;%0A
+%0A
         
@@ -2863,41 +2863,95 @@
     
-str += dbot.api.shortenURI(
+// in here 'cos I can't get the API working%0A                        var longurl = 
 %22http
-s
 ://g
@@ -3013,16 +3013,240 @@
 number%22%5D
+;%0A                        request(%7Bmethod: 'POST', uri: 'http://git.io', form:%7Burl: longurl%7D%7D, function(error, response, body)%7B%0A                            event.reply(response.headers%5B%22location%22%5D);%0A                        %7D
 );%0A     

move back from api as interim measure