/**
 * Browsertime (http://www.browsertime.com)
 * Copyright (c) 2014, Peter Hedenskog, Tobias Lidskog
 * and other contributors
 * Released under the Apache 2.0 License
 */
var firstPaint = function() {

  var t = window.performance.timing;
  var firstPaint = -1;
  if (window.performance.timing.msFirstPaint) {
    firstPaint = window.performance.timing.msFirstPaint - t.navigationStart;
  } else if (window.chrome && window.chrome.loadTimes) {
    firstPaint = window.chrome.loadTimes().firstPaintTime;

    if (firstPaint !== undefined && firstPaint > 0) {
      firstPaint = (firstPaint * 1000) - t.navigationStart;
    }
  }

  return {
    firstPaint: firstPaint
  };
};
module.exports = firstPaint;
/******************************************************************************
Copyright (c) 2014, Google Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of the <ORGANIZATION> nor the names of its contributors
    may be used to endorse or promote products derived from this software
    without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/

// We are using the same calculation for first paint as the Speed Index RUM

var GetFirstPaint = function() {
  var firstPaint = -1;
  var navStart = window.performance.timing.navigationStart;
  // If the browser supports a first paint event, just use what the browser reports
  if ('msFirstPaint' in window.performance.timing)
    firstPaint = window.performance.timing.msFirstPaint - navStart;
  if ('chrome' in window && 'loadTimes' in window.chrome) {
    var chromeTimes = window.chrome.loadTimes();
    if ('firstPaintTime' in chromeTimes && chromeTimes.firstPaintTime > 0) {
      var startTime = chromeTimes.startLoadTime;
      if ('requestTime' in chromeTimes)
        startTime = chromeTimes.requestTime;
      if (chromeTimes.firstPaintTime >= startTime)
        firstPaint = (chromeTimes.firstPaintTime - startTime) * 1000.0;
    }
  }
  // For browsers that don't support first-paint or where we get insane values,
  // use the time of the last non-async script or css from the head.
  if (firstPaint === undefined || firstPaint < 0 || firstPaint > 120000) {
    firstPaint = window.performance.timing.responseStart - navStart;
    var headURLs = {};
    var headElements = window.document.getElementsByTagName('head')[0].children;
    for (var i = 0; i < headElements.length; i++) {
      var el = headElements[i];
      if (el.tagName == 'SCRIPT' && el.src && !el.async)
        headURLs[el.src] = true;
      if (el.tagName == 'LINK' && el.rel == 'stylesheet' && el.href)
        headURLs[el.href] = true;
    }
    var requests = window.performance.getEntriesByType("resource");
    var doneCritical = false;
    for (var j = 0; j < requests.length; j++) {
      if (!doneCritical &&
        headURLs[requests[j].name] &&
        (requests[j].initiatorType == 'script' || requests[j].initiatorType == 'link')) {
        var requestEnd = requests[j].responseEnd;
        if (firstPaint === undefined || requestEnd > firstPaint)
          firstPaint = requestEnd;
      } else {
        doneCritical = true;
      }
    }
  }
  return {
    firstPaint: Math.max(firstPaint, 0)
  }
};

module.exports = GetFirstPaint;
use WPT defintion of first paint