var assert = require('assert');
var tempoiq = require('../lib/tempoiq');
var creds = {};

if (process.env.SNIPPET) {
  creds = require('./snippet-credentials.json');
}

/*
 * A function block that never gets executed. For snippets that we don't
 * want to test
 */
var snippetsUntested = function() {
    // snippet-begin create-client
    var tempoiq = require('tempoiq');
    var client = tempoiq.Client(key, secret, host);
    // snippet-end
}

var getClient = function() {
    var client = tempoiq.Client(creds.key, creds.secret, creds.hostname);
    return client;
}

var initialize = function(done_cb) {
  this.timeout(30000);
  console.log("Starting data initialization");
  var exec = require('child_process').exec;
  exec('python ./test/example-data/initialize.py -n ' + creds.hostname +
               ' -k ' + creds.key + ' -s ' + creds.secret,
      function(err, stdout, stderr) {
        if (err) {
          console.log("error! stdout:" + stdout.toString());
          console.log("stderr: " + stderr.toString());
        }
        done_cb(err);
      } 
  );
}

describe("Example code snippet tests", function() {
  if (!process.env.SNIPPET) {
    // skip em
    return;
  }

  before(initialize);

  it("create-device", function(done) {
    var client = getClient();

    // snippet-begin create-device
    client.createDevice(new tempoiq.Device("thermostat.7", 
      {
        name: "Beta thermostat",
        attributes: {
          type: "thermostat",
          customer: "Internal Dev"
        },
        sensors: [
          new tempoiq.Sensor("temperature", {
            name: "Temperature",
            attributes: {unit: "celsius"}
          }),
          new tempoiq.Sensor("humidity", {
            name: "Relative humidity"
          })
        ]
      }),
      function(err, device) {
        if (err) throw err;
        // Successfully created device
        done();     // snippet-ignore
      }
    );
    // snippet-end
  });

  it('read-data', function(done) {
    var client = getClient();
    // snippet-begin read-data
    var selection = {
      devices: {
        attributes: {
          type: "thermostat",
          region: "north"
        }
      },
      sensors: {
        key: "humidity"
      }
    };
    var start = "2015-01-01T00:00:00Z";
    var end = "2015-01-01T01:00:00Z";

    client.read(selection, start, end, null, function(err, data) {
      data.forEach(function(row) {
        var timestamp = row.ts;
        var values = [];        // List of sensor values at a timestamp
        for (var device in row.values) {
            for (var sensor in row.values[device]) {
                values.push(row.values[device][sensor]);
            }
        }
        assert(values.length > 0);  // snippet-ignore
      });
      assert(data.length > 0);    // snippet-ignore
      done(err);                  // snippet-ignore
    });
    // snippet-end
  });

  it("single-point", function(done) {
    var client = getClient();

    // snippet-begin single-point
    var ts = new Date("2015-01-10T00:00:00Z");
    var selection = {
      devices: { key: "thermostat.1" },
      sensors: { key: "temperature" }
    };

    client.single(selection, "before", ts, null, function(err, data) {
      if (err) {
        // handle error
      } else {
        if (data.length == 0) {
          // No point found
          err = "No point found!"       // snippet-ignore  Be sure to test the code path with a found point
        } else {
          var row = data[0];
          var point_timestamp = row.ts;
          var point_value = row.value('thermostat.1', 'temperature');
          assert.equal(point_timestamp.getFullYear(), 2015);     // snippet-ignore
          // do something
        }
      }
      done(err);                           // snippet-ignore
    });
    // snippet-end
  });
});


@@ -166,287 +166,8 @@
 %0A%7D%0A%0A
-/*%0A * A function block that never gets executed. For snippets that we don't%0A * want to test%0A */%0Avar snippetsUntested = function() %7B%0A    // snippet-begin create-client%0A    var tempoiq = require('tempoiq');%0A    var client = tempoiq.Client(key, secret, host);%0A    // snippet-end%0A%7D%0A%0A
 var 
@@ -1093,9 +1093,9 @@
 tat.
-7
+0
 %22, %0A
@@ -1121,14 +1121,14 @@
 e: %22
-Beta t
+Test T
 herm
@@ -1171,61 +1171,18 @@
     
-type: %22thermostat%22,%0A          customer: %22Internal Dev
+model: %22v1
 %22%0A  
@@ -1255,193 +1255,50 @@
 ure%22
-, %7B%0A            name: %22Temperature%22,%0A            attributes: %7Bunit: %22celsius%22%7D%0A          %7D),%0A          new tempoiq.Sensor(%22humidity%22, %7B%0A            name: %22Relative humidity%22%0A          %7D
+),%0A          new tempoiq.Sensor(%22humidity%22
 )%0A  
@@ -1506,16 +1506,27 @@
 ead-data
+-one-device
 ', funct
@@ -1597,16 +1597,27 @@
 ead-data
+-one-device
 %0A    var
@@ -1660,136 +1660,26 @@
     
-attributes: %7B%0A          type: %22thermostat%22,%0A          region: %22north%22%0A        %7D%0A      %7D,%0A      sensors: %7B%0A        key: %22humidity
+key: %22thermostat.0
 %22%0A  

Update snippet code, move untested snippets to docs repo