var Promise = require('bluebird');
var regression = require('regression');
var queries = require('./queries.js');
var utils = require('./utilities.js');

module.exports.evalAlg = function(userInput, dataType) {
	var databaseSize = 10000;
  var avgCutoff = 500;
  var avgIterations = 3;
  var currentInputSize = 500;
  var stepFactor = 1.5;
  var result = [];
  var runtime;

  return new Promise(function(resolve, reject) {
    return queries.getData(dataType)
    .then(function(response) {
      var data = response[0].array;

      console.log('eval -data length is ' + data.length);

      while (currentInputSize < databaseSize) {
        if (currentInputSize <= avgCutoff) {
          runtime = utils.runTimeAverage(userInput, data.slice(0, currentInputSize), avgIterations);
        } else {
          runtime = utils.getRunTime(userInput, data.slice(0, currentInputSize));
        }
        result.push(runtime);
        currentInputSize *= stepFactor;
      }

      resolve(result);
    });
  });
};

module.exports.getJSONCoords = function(data) {
  console.log('eval -getting json coordinates from eval data');

  var coords = [];

  for (var i = 0; i < data.length; i++) {
    coords.push({x_axis: data[i][0], y_axis: data[i][1]});
  }

  return JSON.stringify(coords);
};

module.exports.runRegression = function(data, order) {
	order = order || '2+';
	var equation;
	var result;

	/* TODO: use results of bigO to run the right type of regression
	switch statement for order/regression type
	2+ - power y = ax^b
	1 - linear y = ax + b
	ln - logarithmic y = a + b ln x
	default - linearThroughOrigin y = mx
	*/
	
	switch (order) {
		case ('2+'): result = regression('power', data);
			equation = 'y = ' + result.equation[0] + 'x^' + result.equation[1];
			break;
		case ('1'): result = regression('linear', data);
			equation = 'y = ' + result.equation[0] + 'x + ' + result.equation[1];
			break;
		case ('ln'): result = regression('logarithmic', data);
			equation = 'y = ' + result.equation[0] + ' + ' + result.equation[1] + 'ln(x)';
			break;
		default: result = regression('linearThroughOrigin', data);
			equation = 'y = ' + result.equation[0] + 'x';
	}

	console.log('Equation from regression: ' + equation);

  return equation;
}





@@ -204,17 +204,18 @@
 Type) %7B%0A
-%09
+  
 var data
@@ -1336,17 +1336,18 @@
 rder) %7B%0A
-%09
+  
 order = 
@@ -1361,17 +1361,18 @@
 %7C '2+';%0A
-%09
+  
 var equa
@@ -1377,17 +1377,18 @@
 uation;%0A
-%09
+  
 var resu
@@ -1392,17 +1392,18 @@
 esult;%0A%0A
-%09
+  
 /* TODO:
@@ -1459,17 +1459,18 @@
 ression%0A
-%09
+  
 switch s
@@ -1504,17 +1504,18 @@
 on type%0A
-%09
+  
 2+ - pow
@@ -1526,17 +1526,18 @@
  = ax%5Eb%0A
-%09
+  
 1 - line
@@ -1550,17 +1550,18 @@
  ax + b%0A
-%09
+  
 ln - log
@@ -1584,17 +1584,18 @@
  b ln x%0A
-%09
+  
 default 
@@ -1627,15 +1627,18 @@
  mx%0A
-%09*/%0A%09%0A%09
+  */%0A  %0A  
 swit
@@ -1650,18 +1650,20 @@
 rder) %7B%0A
-%09%09
+    
 case ('2
@@ -1695,35 +1695,38 @@
 'power', data);%0A
-%09%09%09
+      
 equation = 'y = 
@@ -1773,36 +1773,41 @@
 quation%5B1%5D;%0A
-%09%09%09
+      
 break;%0A
-%09%09
+    
 case ('1'): 
@@ -1835,35 +1835,38 @@
 linear', data);%0A
-%09%09%09
+      
 equation = 'y = 
@@ -1919,28 +1919,33 @@
 ion%5B1%5D;%0A
-%09%09%09
+      
 break;%0A
-%09%09
+    
 case ('l
@@ -1983,35 +1983,38 @@
 ithmic', data);%0A
-%09%09%09
+      
 equation = 'y = 
@@ -2080,20 +2080,25 @@
 )';%0A
-%09%09%09
+      
 break;%0A
-%09%09
+    
 defa
@@ -2156,11 +2156,14 @@
 a);%0A
-%09%09%09
+      
 equa
@@ -2208,13 +2208,15 @@
 x';%0A
-%09%7D%0A%0A%09
+  %7D%0A%0A  
 cons
@@ -2287,12 +2287,8 @@
 tion;%0A%7D%0A
-%0A%0A%0A%0A

fix white spaces and tab indents