'use strict';

let merge = require('lodash.merge'),
  path = require('path'),
  Promise = require('bluebird'),
  browsertime = require('browsertime'),
  log = require('intel').getLogger('sitespeedio.plugin.browsertime'),
  coach = require('webcoach');

const browserScripts = browsertime.browserScripts;

const defaultBrowsertimeOptions = {
  statistics: true
};

const chromeIphoneEmulationOptions = {
  mobileEmulation: { deviceName: 'Google Nexus 5' }
};

const iphone6UserAgent =
  'Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS X) AppleWebKit/536.26 ' +
  '(KHTML, like Gecko) Version/6.0 Mobile/10B329 Safari/8536.25';

function parseUserScripts(scripts) {
  if (!Array.isArray(scripts)) scripts = [scripts];

  return Promise.reduce(
    scripts,
    (results, script) =>
      browserScripts
        .findAndParseScripts(path.resolve(script), 'custom')
        .then(scripts => merge(results, scripts)),
    {}
  );
}

function addCoachScripts(scripts) {
  return Promise.join(scripts, coach.getDomAdvice(), (scripts, advice) => {
    scripts.coach = {
      coachAdvice: advice
    };
    return scripts;
  });
}

module.exports = {
  analyzeUrl(url, options) {
    const btOptions = merge({}, defaultBrowsertimeOptions, options);
    merge(btOptions, { verbose: options.verbose });

    // set mobile options
    if (options.mobile) {
      btOptions.viewPort = '360x640';
      if (btOptions.browser === 'chrome') {
        btOptions.chrome = merge(
          {},
          btOptions.chrome,
          chromeIphoneEmulationOptions
        );
      } else {
        btOptions.userAgent = iphone6UserAgent;
      }
    }

    const scriptCategories = browserScripts.allScriptCategories;
    let scriptsByCategory = browserScripts.getScriptsForCategories(
      scriptCategories
    );

    if (btOptions.script) {
      const userScripts = parseUserScripts(btOptions.script);
      scriptsByCategory = Promise.join(
        scriptsByCategory,
        userScripts,
        (scriptsByCategory, userScripts) =>
          merge(scriptsByCategory, userScripts)
      );
    }

    if (btOptions.coach) {
      scriptsByCategory = addCoachScripts(scriptsByCategory);
    }

    let engine = new browsertime.Engine(btOptions);
    log.info(
      'Starting %s for analysing %s %s time(s)',
      btOptions.browser,
      url,
      btOptions.iterations
    );
    return engine
      .start()
      .then(() => engine.run(url, scriptsByCategory))
      .finally(() => engine.stop());
  }
};

@@ -416,16 +416,20 @@
 ation: %7B
+%0A   
  deviceN
@@ -438,23 +438,19 @@
 e: '
-Google Nexus 5'
+iPhone 6'%0A 
  %7D%0A%7D

rollback to iphone 6