/** 
  * Represents Tinderbox tree.
  * Argument is complex object from Tinderbox JSON feed
  */
function Tree(td) {
  this.td = td;
}

/** 
  * Find and report all platforms reporting into this tree.
  * Returns an array of strings
  */
Tree.prototype.getPlatforms = function() {
  var platforms = [];
  for (buildname in this.td.build_name_index) {
    p = buildname.split(' ');
    platforms[p[0]] = 1;
  }
  return platforms;
}

/** 
  * Find and report all builders reporting into this tree.
  * Returns an array of Build objects 
  */
Tree.prototype.getBuilds = function() {
  builds = [];
  buildtable = this.td.build_table;
  for (row in buildtable) {
    for (col in buildtable[row]) {
      var buildData = buildtable[row][col];
      var name = buildData.buildname;
      if (name == undefined) {
        continue;
      }
      var prevTime = buildData.previousbuildtime;
      var errParser = buildData.errorparser;
      var status = buildData.buildstatus;
      var url = buildData.binaryurl;
      var endTime = buildData.endtime;
      var startTime = buildData.buildtime;
      var logFile = buildData.logfile;
      var checkins = [];
      var tests = [];
      scrapeTable = this.td.scrape;
      for (var log in scrapeTable) {
        if (log == logFile) {
          for (e in scrapeTable[log]){
            test = new Object();
            // FIXME better error handling here
            try {
              testUrl = scrapeTable[log][e];
              text = testUrl.split('>')[1].split('<')[0];
              if (text.indexOf(':') == -1) {
                continue;
              }
              testName = text.split(':')[0];
              testValue = text.split(':')[1];
              test.name = testName;
              test.value = testValue.replace('ms','');
              test.url = testValue;
              tests.push(test);
            } catch(e) {}
          }
        }
      }
      for (var e in this.td.build_time_times) {
        time = this.td.build_time_times[e];
        if (time == startTime) {
          peep = this.td.who_list[e];
          if (peep != 'undef') {
            for (var p in peep) {
              checkins.push(p);
            }
          }
        }
      }

      builds.push(new Build(name, prevTime, errParser, status, url,
                            endTime, startTime, logFile, tests, checkins));
    }
  }
  return builds;
}

/** 
  * Represents a single build report to Tinderbox.
  * Arguments: 
  *  name - full builder name, platform first
  *  prevTime - start time of previous build
  *  errParser - format for error parser, windows or unix
  *  status - success, building, busted, or testfailed
  *  url - if this is a nightly, FTP URL this build was uploaded to 
  *  endTime - time the build completed
  *  startTime - time the build started
  *  logFile - HTTP URL to the log for this build
  *  tests - array of hashes, test results from the scraped log data
  *  checkins - bonsai checkins that correpond to this build
  */
function Build(name, prevTime, errParser, status, url, endTime, startTime,
               logFile, tests, checkins) {
  this.name = name;
  this.platform = this.name.split(' ')[0];
  this.prevTime = prevTime;
  this.errParser = errParser;
  this.status = status;
  this.url = url;
  this.endTime = endTime;
  this.startTime = startTime;
  this.logFile = logFile;
  this.tests = tests;
  this.checkins = checkins;
 }

/** 
  * Tinderbox name, platform first.
  * Returns string
  */
Build.prototype.getName = function() {
  return this.name;
}
/** 
  * Platform name, first field of name.
  * Returns string
  */
Build.prototype.getPlatform = function() {
  return this.platform;
}
/** 
  * Previous build start time.
  * Returns string
  */
Build.prototype.getPrevTime = function() {
  return this.prevTime;
}
/** 
  * Error parser format, windows or unix.
  * Returns string
  */
Build.prototype.getErrParser = function() {
  return this.errParser;
}
/** 
  * Build status - success, building, busted, or testfailed
  * Returns string
  */
Build.prototype.getStatus = function() {
  return this.status;
}
/** 
  * FTP URL this build was uploaded to, if this is a nightly.
  * Returns string
  */
Build.prototype.getUrl = function() {
  return this.url;
}
/** 
  * time the build finished
  * Returns string
  */
Build.prototype.getEndTime = function() {
  return this.endTime;
}
/** 
  * time the build started
  * Returns string
  */
Build.prototype.getStartTime = function() {
  return this.startTime;
}
/** 
  * time it took to build
  * Returns string
  */
Build.prototype.getElapsedTime = function() {
  var diff = this.endTime - this.startTime;
  return diff;
}
 /**
  * HTTP URL to logfile
  * Returns string
  */
Build.prototype.getLogFile = function() {
  return this.logFile;
}
 /**
  * Tests derived from scraped log data
  * Returns array of hashes
  */
Build.prototype.getTests = function() {
  return this.tests;
}
 /**
  * Checkins that went into this build
  * Returns array
  */
Build.prototype.getCheckins = function() {
  return this.checkins;
}
