'use strict';

var _ = require('lodash');
var nconf = require('nconf');
nconf.argv();

var data = nconf.get('csv') || './report.csv';

// Create the parser
var parser = require('csv-parse')();

var results = {};

// Use the writable stream api
parser.on('readable', function(){
	var cursor;
	var record;
	var chance;
	var odds;
	var meetsCriterion;
	var currency;
	var position;

  while(record = parser.read()){
	  currency = record[0].toLowerCase();
	  position = record[1];
		chance = parseFloat(record[2]) || 0.0;
		odds = parseFloat(record[3]) || 0.0;
		meetsCriterion = record[4] === 'true';

		if(!results[currency]) {
			results[currency] = {
				hold: {
					wager: 0.0,
					total: 0
				},
				long: {
					wager: 0.0,
					total: 0
				},
				short: {
					wager: 0.0,
					total: 0
				}
			};
		}

		if(chance > 0.0 && odds > 0.0) {
			if(meetsCriterion) {
				results[currency][position]['chance'] = (results[currency][position]['chance'] || 0.0) + chance;
				results[currency][position]['odds'] = (results[currency][position]['odds'] || 0.0) + odds;

				results[currency][position]['total'] = (results[currency][position]['total'] || 0) + 1.0;
			}
		}
  }
});
// Catch any error
parser.on('error', function(err){
  console.error(err.message);
});
// When we are done, test that the parsed output matched what expected
parser.on('finish', function(){
	var currencies = _.keys(results);
	var positions = ['hold', 'long', 'short'];
	var currency, position;
	var chance = 0.0;
	var odds = 0.0;
	var wager;
	var netWager;
	var total;

	console.error('results:', JSON.stringify(results, null, '\t'));

	for(var i = 0; i< currencies.length; i++) {
		currency = currencies[i];
		console.error('-------', currency,'-------');

		total = 0.0;
		for(var j=0; j < positions.length; j++) {
			position = positions[j];
			odds = 0.0;

			if(results[currency][position]['total']) {
				chance = results[currency][position]['chance'] / results[currency][position]['total'];
				odds = (results[currency][position]['odds'] / results[currency][position]['total']) - 1.0;
			}

			if(odds > 0.0) {
				wager = chance - ((1.0 - chance) / odds);
				results[currency][position]['wager'] = wager;

				console.error(position, ':', (wager * 100.0) + '%');
			} else {
				results[currency][position]['wager'] = 0.0;
			}

			total = total + results[currency][position]['wager'];
		}

		netWager =  results[currency]['long']['wager'] - results[currency]['short']['wager'];

		console.log(report(netWager, currency));
	}

	console.error('========= REPORT =========');
});

var lineReader = require('readline').createInterface({
  input: require('fs').createReadStream(data)
});

var lineCount = 0;
lineReader.on('line', function (line) {
	if(lineCount) {
		parser.write(line + '\n');
	}

	lineCount++;
});

lineReader.on('close', function() {
	parser.end();
})

function report(netWager, currency) {
	var action = (netWager > 0 ? 'BUY' : 'SELL') + ' ' + currency;

	return (netWager * 100.0) + ' ' +  currency + ' - ' + action;
}

@@ -204,16 +204,35 @@
 ts = %7B%7D;
+%0Avar rankings = %7B%7D;
 %0A%0A// Use
@@ -1559,16 +1559,44 @@
 r total;
+%0A%09var pair;%0A%09var multiplier;
 %0A%0A%09conso
@@ -1730,16 +1730,19 @@
 s%5Bi%5D;%0A%09%09
+// 
 console.
@@ -2242,16 +2242,19 @@
 r;%0A%0A%09%09%09%09
+// 
 console.
@@ -2518,99 +2518,391 @@
 '%5D;%0A
-%0A
 %09%09
-console.log(report(netWager, currency));%0A%09%7D%0A%0A%09console.error('========= REPORT ========='
+multiplier = netWager %3E 0.0 ? 1.0 : -1.0;%0A%0A%09%09pair = getPair(currency);%0A%0A%09%09rankings%5Bpair.base%5D = (rankings%5Bpair.base%5D %7C%7C 0.0) + multiplier;%0A%09%09rankings%5Bpair.other%5D = (rankings%5Bpair.other%5D %7C%7C 0.0) - multiplier;%0A%0A%09%09console.error(currency, ':', netWager);%0A%09%7D%0A%0A%09console.error('========= REPORT =========');%0A%0A%09_.each(_.keys(rankings), function(key) %7B%0A%09%09console.log(rankings%5Bkey%5D, key);%0A%09%7D
 );%0A%7D
@@ -3207,163 +3207,166 @@
 ion 
-report(netWager, currency) %7B%0A%09var action = (netWager %3E 0 ? 'BUY' : 'SELL') + ' ' + currency;%0A%0A%09return (netWager * 100.0) + ' ' +  currency + ' - ' + action
+getPair(currency) %7B%0A%09var base = currency.substring(0, 3);%0A%09var other = currency.substring(3, 6);%0A%0A%09var pair = %7B%0A%09%09base: base,%0A%09%09other: other%0A%09%7D;%0A%0A%09return pair
 ;%0A%7D%0A

order currencies