var PluginInstance = require(__core + "PluginInstance.js");
var rrd = new PluginInstance();

var request = require("request");
var plugAPI = require("plugapi");

rrd.init = function() {
    rrd.statsTimer = setInterval(function () {
        this.save_stats();
    }, 30 * 1000);
    this.save_stats();

    this.plug = this.manager.getPlugin("plug").plugin.plug; //TODO: implement better method
};

rrd.events.onUnload = function () {
    clearInterval(rrd.statsTimer);
};

rrd.events.plug_userJoin = function (user) {
    this.save_stats();
};

rrd.events.plug_userLeave = function (user) {
    this.save_stats();
};

rrd.events.plug_advance = function (track) {
    this.save_stats();
};

rrd.save_stats = function () {
    clearInterval(rrd.statsTimer);
    rrd.statsTimer = setInterval(function () {
        rrd.save_stats();
    }, 30 * 1000);

    if (config.url !== null
        && typeof config.url !== "undefined"
        && config.url.length > 0) {
        var users = this.plug.getUsers();
        //console.log(users);
        var data = {};
        data.djs = this.plug.getWaitList().length + (typeof this.plug.getDJ() === 'undefined' ? 0 : 1);
        data.listeners = users.length;
        data.guests = this.plug.getGuests();
        data.user = 0;
        data.residentDJ = 0;
        data.bouncer = 0;
        data.manager = 0;
        data.coHost = 0;
        data.host = 0;
        data.brandAmbassador = 0;
        data.admin = 0;
        var levels = {};
        levels.count = 0;
        levels.sum = 0;
        for (var i = 0; i < users.length; ++i) {
            var rawrank = users[i].role;
            if (users[i].gRole === plugAPI.GLOBAL_ROLES.ADMIN) {
                ++data.admin;
            } else if (parseInt(users[i].gRole,10) > 1) {
                ++data.brandAmbassador;
            } else if (rawrank === plugAPI.ROOM_ROLE.NONE) {
                ++data.user;
            } else if (rawrank === plugAPI.ROOM_ROLE.RESIDENTDJ) {
                ++data.residentDJ;
            } else if (rawrank === plugAPI.ROOM_ROLE.BOUNCER) {
                ++data.bouncer;
            } else if (rawrank === plugAPI.ROOM_ROLE.MANAGER) {
                ++data.manager;
            } else if (rawrank === plugAPI.ROOM_ROLE.COHOST) {
                ++data.coHost;
            } else if (rawrank === plugAPI.ROOM_ROLE.HOST) {
                ++data.host;
            } else {
                ++data.user;
                console.log(users[i]);
            }
            ++levels.count;
            levels.sum = levels.sum + users[i].level;
            data.avgLevel = levels.sum / levels.count;
        }

        request.post({
            url: this.config.url,
            headers: {
                'Content-Type': 'application/json'
            },
            body: data
        }, function (err, httpResponse, body) {
            if (error || response.statusCode !== 200) {
                console.error("rrd error:" + body);
            }
        });
    }
};

module.exports = rrd;

@@ -175,24 +175,117 @@
 unction() %7B%0A
+    this.plug = this.manager.getPlugin(%22plug%22).plugin.plug; //TODO: implement better method%0A%0A
     rrd.stat
@@ -391,101 +391,8 @@
 s();
-%0A%0A    this.plug = this.manager.getPlugin(%22plug%22).plugin.plug; //TODO: implement better method
 %0A%7D;%0A
@@ -851,16 +851,21 @@
     if (
+this.
 config.u
@@ -894,16 +894,21 @@
  typeof 
+this.
 config.u
@@ -937,16 +937,21 @@
      && 
+this.
 config.u
@@ -2544,22 +2544,10 @@
 sum 
-= levels.sum 
 +
+=
  use
@@ -2559,24 +2559,30 @@
 .level;%0A    
+    %7D%0A
         data
@@ -2619,26 +2619,16 @@
 s.count;
-%0A        %7D
 %0A%0A      
@@ -2783,18 +2783,20 @@
     
-body
+json
 : 
+%5B
 data
+%5D
 %0A   
@@ -2820,15 +2820,13 @@
 (err
-, httpR
+or, r
 espo
@@ -2937,17 +2937,34 @@
 error:%22 
-+
+, error, response,
  body);%0A

Fix refactored rrdstats