import app from 'ampersand-app';
import cookies from 'js-cookie';
import EmailDialogView from '../views/email-opt-in-dialog-view';


function installAddon(view, eventCategory, callback) {
  const downloadUrl = '/static/addon/addon.xpi';

  view.query('[data-hook=install]').classList.add('state-change');
  view.query('.default-btn-msg').classList.add('no-display');
  view.query('.progress-btn-msg').classList.remove('no-display');

  app.sendToGA('event', {
    eventCategory: eventCategory,
    eventAction: 'button click',
    eventLabel: 'Install the Add-on',
    outboundURL: downloadUrl
  });

  // Wait for the add-on to be installed.
  // TODO: Should we have a timeout here, give up after a few intervals? If
  // user cancels add-on install, this will never stop spinning.
  const interval = setInterval(() => {
    if (!window.navigator.testpilotAddon) { return; }
    clearInterval(interval);
    cookies.set('first-run', 'true');
    app.me.fetch().then(() => {
      callback();
    }).catch(() => {
      // HACK (for Issue #1075): Timed out while waiting for the initial sync
      // message, so just reload the page to stop waiting.
      window.location.reload();
    });
  }, 1000);
}


function postInstallModal(view) {
  if (cookies.get('first-run')) {
    cookies.remove('first-run');
    view.renderSubview(new EmailDialogView({}), 'body');
  }
}

export { installAddon as default, postInstallModal };

@@ -130,110 +130,715 @@
 ;%0A%0A%0A
-function installAddon(view, eventCategory, callback) %7B%0A  const downloadUrl = '/static/addon/addon.xpi'
+const MOZADDONMANAGER_ALLOWED_HOSTNAMES = %5B%0A  'testpilot.firefox.com',%0A  'testpilot.stage.mozaws.net',%0A  'testpilot.dev.mozaws.net'%0A%5D;%0A%0A%0Afunction mozAddonManagerInstall(url) %7B%0A  return navigator.mozAddonManager.createInstall(%7B url %7D).then(install =%3E %7B%0A    return new Promise((resolve, reject) =%3E %7B%0A      install.addEventListener('onInstallEnded', () =%3E resolve());%0A      install.addEventListener('onInstallFailed', () =%3E reject());%0A      install.install();%0A    %7D);%0A  %7D);%0A%7D%0A%0Afunction installAddon(view, eventCategory, callback) %7B%0A  const %7B protocol, hostname, port %7D = window.location;%0A  const path = '/static/addon/addon.xpi';%0A  const downloadUrl = %60$%7Bprotocol%7D//$%7Bhostname%7D$%7Bport ? ':' + port : ''%7D$%7Bpath%7D%60
 ;%0A%0A 
@@ -1038,29 +1038,179 @@
 %0A%0A  
-app.sendToGA('e
+const useMozAddonManager = (%0A    navigator.mozAddonManager &&%0A    protocol === 'https:' &&%0A    MOZADDONMANAGER_ALLOWED_HOSTNAMES.includes(hostname)%0A  );%0A%0A  const gaE
 vent
-',
+ =
  %7B%0A 
@@ -1311,22 +1311,233 @@
  Add-on'
-,%0A    
+%0A  %7D;%0A%0A  if (useMozAddonManager) %7B%0A    app.sendToGA('event', gaEvent);%0A    mozAddonManagerInstall(downloadUrl).then(() =%3E %7B%0A      console.log('Installed extension via mozAddonManager');%0A    %7D);%0A  %7D else %7B%0A    gaEvent.
 outbound
@@ -1539,17 +1539,18 @@
 boundURL
-:
+ =
  downloa
@@ -1553,22 +1553,57 @@
 nloadUrl
+;
 %0A  
-%7D);
+  app.sendToGA('event', gaEvent);%0A  %7D
 %0A%0A  // W

Use mozAddonManager to install Test Pilot extension when available.