import config from 'travis/config/environment';

class Tracer {
  constructor() {
    this.requestStack = [];
  }

  enable() {
    window.localStorage['apiTrace'] = 'true';
  }

  disable() {
    delete window.localStorage['apiTrace'];
  }

  isEnabled() {
    return window.localStorage['apiTrace'] === 'true';
  }

  slowest() {
    let requestStack = this.requestStack.slice();
    requestStack = requestStack.filter(req => req.duration);
    requestStack.sort(function(a, b) {
      return b.duration - a.duration;
    });
    return requestStack.slice(0, 5);
  }

  format(requests) {
    requests.forEach(req => {
      let requestId = req.requestId;
      if (config.apiTraceEndpoint) {
        requestId = `${config.apiTraceEndpoint}${requestId}`;
      }
      // eslint-disable-next-line
      console.log(`${req.method} ${req.url} ${req.status} ${req.duration}ms\n${requestId}`);
    })
  }

  showSlowest() {
    TravisTracer.format(TravisTracer.slowest())
  }

  // xhr interceptor based on the symfony profiler
  install() {
    let tracer = this;

    let proxied = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function (method, url, async, user, pass) {
      let self = this;

      if (url.startsWith(config.apiEndpoint)) {
        let stackElement = {
          loading: true,
          error: false,
          url: url.substr(config.apiEndpoint.length),
          method: method,
          start: new Date()
        };

        tracer.requestStack.push(stackElement);

        this.addEventListener('readystatechange', () => {
          if (self.readyState == 4) {
            stackElement.status = self.status;
            stackElement.duration = new Date() - stackElement.start;
            stackElement.loading = false;
            stackElement.error = self.status < 200 || self.status >= 400;
            stackElement.requestId = self.getResponseHeader('X-Request-ID');
          }
        }, false);
      }

      proxied.call(this, ...arguments);

      if (url.startsWith(config.apiEndpoint)) {
        this.setRequestHeader('Trace', 'true');
      }
    };
  }
}

export function initialize(app) {
  if (typeof window !== 'undefined') {
    window.TravisTracer = new Tracer();
    if (window.TravisTracer.isEnabled()) {
      window.TravisTracer.install();
    }
    return window.TravisTracer;
  }
}

export default {
  name: 'tracer',
  initialize,
};

@@ -462,37 +462,17 @@
 ort(
-function(a, b) %7B%0A      return
+(a, b) =%3E
  b.d
@@ -491,23 +491,16 @@
 duration
-;%0A    %7D
 );%0A    r
@@ -868,68 +868,108 @@
   %7D)
-%0A  %7D%0A%0A  showSlowest() %7B%0A    TravisTracer.format(TravisTracer
+;%0A  %7D%0A%0A  showAll() %7B%0A    this.format(this.requestStack);%0A  %7D%0A%0A  showSlowest() %7B%0A    this.format(this
 .slo
@@ -975,16 +975,17 @@
 owest())
+;
 %0A  %7D%0A%0A  

make eslint happy, add showAll()