'use strict';

let path = require('path'),
  messageMaker = require('../../support/messageMaker'),
  aggregator = require('./aggregator'),
  pagexrayAggregator = require('./pagexrayAggregator'),
  pagexray = require('pagexray'),
  filterRegistry = require('../../support/filterRegistry');

const make = messageMaker('coach').make;

const DEFAULT_METRICS = ['performance.score.*', 'bestpractice.score.*', 'accessibility.score.*'];

const DEFAULT_PAGEXRAY_PAGESUMMARY_METRICS = [];

module.exports = {
  name() {
    return path.basename(__dirname);
  },
  open() {
    filterRegistry.registerFilterForType(DEFAULT_METRICS, 'coach.summary');
    filterRegistry.registerFilterForType(DEFAULT_METRICS, 'coach.pageSummary');
    filterRegistry.registerFilterForType(DEFAULT_PAGEXRAY_PAGESUMMARY_METRICS, 'pagexray.pageSummary');
  },
  processMessage(message, queue) {
    switch (message.type) {
      case 'coach.run':
      {
        if (message.runIndex === 0) {
          // For now, choose the first run to represent the whole page.
          // Later we might want to change the median run (based on some metric) similar to the WPT approach.
          const url = message.url;
          queue.postMessage(make('coach.pageSummary', message.data, {url}));
        }

        aggregator.addToAggregate(message.data);
        break;
      }

      case 'browsertime.har':
      {
        const url = message.url;
        const pageSummary = pagexray.convert(message.data, {
          includeAssets: true
        });

        pagexrayAggregator.addToAggregate(pageSummary, message.url);

        queue.postMessage(make('pagexray.pageSummary', pageSummary, {url}));

        pageSummary.forEach((run, runIndex) => {
          queue.postMessage(make('pagexray.run', run, {url, runIndex}));
        });
        break;
      }

      case 'summarize':
      {
        let summary = aggregator.summarize();
        if (summary && Object.keys(summary).length > 0) {
          queue.postMessage(make('coach.summary', summary));
        }

        let pagexraySummary = pagexrayAggregator.summarize();
        if (pagexraySummary && Object.keys(pagexraySummary).length > 0) {
          queue.postMessage(make('pagexray.summary', pagexraySummary));
        }
        break;
      }
    }
  }
};

@@ -342,24 +342,32 @@
 AULT_METRICS
+_SUMMARY
  = %5B'perform
@@ -431,16 +431,139 @@
 re.*'%5D;%0A
+const DEFAULT_METRICS_PAGESUMMARY = %5B'advice.performance.score','advice.bestpractice.score', 'advice.accessibility.score'%5D;
 %0Aconst D
@@ -736,32 +736,40 @@
 (DEFAULT_METRICS
+_SUMMARY
 , 'coach.summary
@@ -828,16 +828,28 @@
 _METRICS
+_PAGESUMMARY
 , 'coach

send overall scores per page to Graphite