var _ = require('underscore');
var fs = require('fs');
var babyparse = require('babyparse');
// var tax = require('./typicalityTax');

function testPrint() {
  console.log("this is working");
};

function readCSV(filename){
  return babyparse.parse(fs.readFileSync(filename, 'utf8')).data;
};

function writeCSV(jsonCSV, filename){
  fs.writeFileSync(filename, babyparse.unparse(jsonCSV) + '\n');
}

function appendCSV(jsonCSV, filename){
  fs.appendFileSync(filename, babyparse.unparse(jsonCSV) + '\n');
}

var writeERP = function(erp, labels, filename, fixed) {
  var data = _.filter(erp.support().map(
   function(v) {
     var prob = Math.exp(erp.score(v));
     if (prob > 0.0){
      if(v.slice(-1) === ".")
        out = butLast(v);
      else if (v.slice(-1) === "?")
        out = butLast(v).split("Is")[1].toLowerCase();
      else 
        out = v
      return labels.concat([out, String(prob.toFixed(fixed))]);

    } else {
      return [];
    }
  }
  ), function(v) {return v.length > 0;});
  appendCSV(data, filename);
};

var supportWriter = function(s, p, handle) {
  var sLst = _.pairs(s);
  var l = sLst.length;

  for (var i = 0; i < l; i++) {
    fs.writeSync(handle, sLst[i].join(',')+','+p+'\n');
  }
};

// Note this is highly specific to a single type of erp
var bayesianErpWriter = function(erp, filePrefix) {
  var predictiveFile = fs.openSync(filePrefix + "Predictives.csv", 'w');
  fs.writeSync(predictiveFile, ["condition", "TargetColor","TargetType","Dist1Color","Dist1Type","Dist2Color","Dist2Type",
				"value", "prob", "MCMCprob"] + '\n');

  var paramFile = fs.openSync(filePrefix + "Params.csv", 'w');
  fs.writeSync(paramFile, ["parameter", "value", "MCMCprob"] + '\n');

  var supp = erp.support();
  supp.forEach(function(s) {
    supportWriter(s.predictive, Math.exp(erp.score(s)), predictiveFile);
    supportWriter(s.params, Math.exp(erp.score(s)), paramFile);
  });
  fs.closeSync(predictiveFile);
  fs.closeSync(paramFile);
  console.log('writing complete.');
};

var getSubset = function(data, options) {
  var condition = options.condition;
      t_color = options.t_color;
      t_type = options.t_type;
      d1_color = options.d1_color;
      d1_type = options.d1_type;
      d2_color = options.d2_color;
      d2_type = options.d2_type;      
  var cond = function(row) {
    return (row[0] === condition &&
		 row[1] === t_color &&
		 row[2] === t_type &&
     row[3] === d1_color &&
     row[4] === d1_type &&
     row[5] === d2_color &&
     row[6] === d2_type);
  };
  return _.filter(data, cond);
};

var getTypSubset = function(data, obj_features) {
  var cond = function(row) {
    return row[0] === obj_features;
  };
  return _.filter(data, cond);
};

var locParse = function(filename) {
  return babyparse.parse(fs.readFileSync(filename, 'utf8'),
       {header: true,
        skipEmptyLines : true}).data;
};


module.exports = {
  getSubset : getSubset,
  bayesianErpWriter : bayesianErpWriter,
  writeERP : writeERP,
  writeCSV : writeCSV,
  readCSV : readCSV,
  locParse : locParse,
  getTypSubset : getTypSubset
};

@@ -275,16 +275,35 @@
  'utf8')
+,%0A%09%09%09 %7Bheader:true%7D
 ).data;%0A
@@ -2052,22 +2052,25 @@
 n(data, 
-option
+propertie
 s) %7B%0A  v
@@ -2076,471 +2076,42 @@
 var 
-condition = options.condition;%0A      t_color = options.t_color;%0A      t_type = options.t_type;%0A      d1_color = options.d1_color;%0A      d1_type = options.d1_type;%0A      d2_color = options.d2_color;%0A      d2_type = options.d2_type;      %0A  var cond = function(row) %7B%0A    return (row%5B0%5D === condition &&%0A%09%09 row%5B1%5D === t_color &&%0A%09%09 row%5B2%5D === t_type &&%0A     row%5B3%5D === d1_color &&%0A     row%5B4%5D === d1_type &&%0A     row%5B5%5D === d2_color &&%0A     row%5B6%5D === d2_type);%0A  %7D
+matchProps = _.matches(properties)
 ;%0A  
@@ -2124,36 +2124,42 @@
  _.filter(data, 
-cond
+matchProps
 );%0A%7D;%0A%0Avar getTy

clean up getSubset function