"use strict";

var _      = require('underscore');
var influx = require('influx');

module.exports = function( options ) {
  var seneca = this
  var plugin = 'collector'

  seneca.add({role: plugin, ping: true}, ping);

  function ping(args, done) {
    done(null, {now:Date.now()});
  };

  var client = influx({
    //cluster configuration
    hosts : [
      {
        host : options.host,
        port : options.port //optional. default 8086
      }
    ],
    // or single-host configuration
    host : options.host,
    port : options.port,// optional, default 8086
    username : options.username,
    password : options.password,
    database : options.database
  });

  seneca.add({role:plugin, cmd:'send'}, function(args,done){
    client.writePoint('actions', args.point, args.options, function(response) {
      done(null, {data:'received'});
    });

  });

  seneca.add({role:plugin, cmd: 'get'}, function(args, done) {
    var query = '';
    if(args.fieldName && args.seriesName) {
      query = 'SELECT ' + args.fieldName + ' FROM ' + args.seriesName;
    } else {
      query = "SELECT * FROM " + args.seriesName + " WHERE pattern ='"+args.pattern+"'";
    }

    client.query(query, function(err,response) {
    var parsedResponse = parseInfluxData(response);
            done(null,parsedResponse);
        });
    });

  function parseInfluxData(data) {
    var columns = data[0].columns;
    var points  = data[0].points;
    var patternIndex = columns.indexOf("pattern");

    var result = [];
    var count = 1;
    var countedRoles = [];

    for(var i = 0; i<points.length; i++) {

      var containsRole = _.contains(countedRoles,points[i][patternIndex]);

      if(!containsRole) {
        result.push({pattern:points[i][patternIndex],
                     count:count
                    });
        countedRoles.push(points[i][patternIndex]);
      } else {

        for(var j = 0; j < result.length; j++) {
          if(result[j].pattern === points[i][patternIndex]) {
            result[j].count++;
          }
        }
    }

    }
    return result;
  }

  return {
    name: plugin
  }
}

@@ -1363,24 +1363,52 @@
 ata(data) %7B%0A
+    if(data.length %3E 0) %7B%0A  
     var colu
@@ -1430,24 +1430,26 @@
 olumns;%0A    
+ 
+ 
 var points  
@@ -1466,16 +1466,18 @@
 points;%0A
+  
     var 
@@ -1516,24 +1516,26 @@
 pattern%22);%0A%0A
+  
     var resu
@@ -1543,24 +1543,26 @@
 t = %5B%5D;%0A    
+  
 var count = 
@@ -1564,16 +1564,18 @@
 nt = 1;%0A
+  
     var 
@@ -1594,24 +1594,26 @@
  = %5B%5D;%0A%0A    
+  
 for(var i = 
@@ -1636,24 +1636,26 @@
 th; i++) %7B%0A%0A
+  
       var co
@@ -1724,16 +1724,18 @@
 %0A%0A      
+ 
+ 
 if(!cont
@@ -1742,24 +1742,26 @@
 ainsRole) %7B%0A
+  
         resu
@@ -1802,16 +1802,18 @@
 Index%5D,%0A
+  
         
@@ -1853,28 +1853,32 @@
             
+  
 %7D);%0A
+  
         coun
@@ -1919,24 +1919,26 @@
 ex%5D);%0A      
+ 
+ 
 %7D else %7B%0A%0A  
@@ -1931,24 +1931,26 @@
   %7D else %7B%0A%0A
+  
         for(
@@ -1996,16 +1996,18 @@
         
+ 
+ 
 if(resul
@@ -2058,24 +2058,26 @@
             
+  
 result%5Bj%5D.co
@@ -2089,26 +2089,30 @@
 ;%0A          
-%7D%0A
+  %7D%0A  
         %7D%0A  
@@ -2117,17 +2117,23 @@
     
+  
 %7D%0A%0A    
+ 
+ 
 %7D%0A
+  
     
@@ -2147,16 +2147,22 @@
 result;%0A
+    %7D%0A
   %7D%0A%0A  r

update collector