var ConfigModel = require('./data/config-model');
var deepInsights = require('cartodb-deep-insights.js');
var LayerStyleCollection = require('./embed/style-collection');
var EmbedIntegrations = require('./embed/embed-integrations');
var MetricsTracker = require('./components/metrics/metrics-tracker');
var WebGLMetrics = require('./components/metrics/webgl-metrics');

var _ = require('underscore');
var URLStateHelper = require('cartodb-deep-insights.js/src/api/url-helper.js');
var vizJSON = window.vizJSON;
var cartoLogoOption = _.findWhere(vizJSON.overlays, {type: 'logo'});
var dashboardOpts = {
  no_cdn: false,
  cartodb_logo: cartoLogoOption !== undefined,
  renderMenu: vizJSON.options.dashboard_menu,
  share_urls: true,
  authToken: window.authTokens,
  layerSelectorEnabled: true,
  mapzenApiKey: window.mapzenApiKey
};
var stateJSON = window.stateJSON;
var layersData = window.layersData;
var configModel = new ConfigModel({ base_url: window.baseURL });

var stateFromURL = URLStateHelper.getStateFromCurrentURL();
if (stateFromURL && !_.isEmpty(stateFromURL)) {
  _.extend(dashboardOpts, {
    state: stateFromURL
  });
} else if (stateJSON && stateJSON !== '{}') {
  _.extend(dashboardOpts, {
    state: stateJSON
  });
}

var layerStyleCollection = new LayerStyleCollection();
layerStyleCollection.resetByLayersData(layersData);

MetricsTracker.init({
  userId: '00000000-0000-0000-0000-000000000000',
  visId: vizJSON.id,
  configModel: configModel
});

deepInsights.createDashboard('#dashboard', vizJSON, dashboardOpts, function (error, dashboard) {
  if (error) {
    console.error('Dashboard has some errors:', error);
  }

  var map = dashboard.getMap().map;
  var scrollwheel = vizJSON.options.scrollwheel;
  var method = scrollwheel ? 'enableScrollWheel' : 'disableScrollWheel';
  map && map[method] && map[method]();

  var embedIntegrations = new EmbedIntegrations({
    deepInsightsDashboard: dashboard,
    layerStyleCollection: layerStyleCollection
  });

  if (Math.random() < 0.5) {
    MetricsTracker.track('WebGL stats', WebGLMetrics.getWebGLStats());
  }

  window.embedIntegrations = embedIntegrations;
});

window.layerStyleCollection = layerStyleCollection;

var ConfigModel = require('./data/config-model');
var deepInsights = require('cartodb-deep-insights.js');
var LayerStyleCollection = require('./embed/style-collection');
var EmbedIntegrations = require('./embed/embed-integrations');
var MetricsTracker = require('./components/metrics/metrics-tracker');
var WebGLMetrics = require('./components/metrics/webgl-metrics');

var _ = require('underscore');
var URLStateHelper = require('cartodb-deep-insights.js/src/api/url-helper.js');
var vizJSON = window.vizJSON;
var cartoLogoOption = _.findWhere(vizJSON.overlays, {type: 'logo'});
var dashboardOpts = {
  no_cdn: false,
  cartodb_logo: cartoLogoOption !== undefined,
  renderMenu: vizJSON.options.dashboard_menu,
  share_urls: true,
  authToken: window.authTokens,
  layerSelectorEnabled: true,
  mapzenApiKey: window.mapzenApiKey
};
var stateJSON = window.stateJSON;
var layersData = window.layersData;
var configModel = new ConfigModel({ base_url: window.baseURL });

var stateFromURL = URLStateHelper.getStateFromCurrentURL();
if (stateFromURL && !_.isEmpty(stateFromURL)) {
  _.extend(dashboardOpts, {
    state: stateFromURL
  });
} else if (stateJSON && stateJSON !== '{}') {
  _.extend(dashboardOpts, {
    state: stateJSON
  });
}

var layerStyleCollection = new LayerStyleCollection();
layerStyleCollection.resetByLayersData(layersData);

var ANONYMOUS_USER_ID = '00000000-0000-0000-0000-000000000000';
MetricsTracker.init({
  userId: ANONYMOUS_USER_ID,
  visId: vizJSON.id,
  configModel: configModel
});

deepInsights.createDashboard('#dashboard', vizJSON, dashboardOpts, function (error, dashboard) {
  if (error) {
    console.error('Dashboard has some errors:', error);
  }

  var map = dashboard.getMap().map;
  var scrollwheel = vizJSON.options.scrollwheel;
  var method = scrollwheel ? 'enableScrollWheel' : 'disableScrollWheel';
  map && map[method] && map[method]();

  var embedIntegrations = new EmbedIntegrations({
    deepInsightsDashboard: dashboard,
    layerStyleCollection: layerStyleCollection
  });

  if (Math.random() < 0.5) {
    MetricsTracker.track('WebGL stats', WebGLMetrics.getWebGLStats());
  }

  window.embedIntegrations = embedIntegrations;
});

window.layerStyleCollection = layerStyleCollection;

Use const for ANONYMOUS_USER_ID