"use strict";

angular.module('arethusa.core').service('globalSettings', [
  'configurator',
  'plugins',
  '$injector',
  function(configurator,  plugins, $injector) {
    var self = this;

    self.settings = {};
    self.colorizers = { disabled: true };


    var confKeys = [
      "alwaysDeselect",
      "colorizer"
    ];

    self.defaultConf = {
      alwaysDeselect: false,
      colorizer: 'morph'
    };

    function configure() {
      self.conf = configurator.configurationFor('main').globalSettings || {};
      configurator.delegateConf(self, confKeys, true); // true makes them sticky

      defineSettings();
    }

    function Conf(property, type, directive, label) {
      this.property = property;
      this.label = label || "globalSettings." + property;
      this.type = type || 'checkbox';
      this.directive = directive;
    }

    function defineSettings() {
      self.defineSetting('alwaysDeselect');
      self.defineSetting('keyboardMappings');
      self.defineSetting('colorizer', 'custom', 'colorizer-setting');
      self.defineSetting('layout', 'custom', 'layout-setting');
    }

    this.defineSetting = function(property, type, directive, label) {
      self.settings[property] = new Conf(property, type, directive, label);
    };

    this.removeSetting = function(setting) {
      delete self.settings[setting];
    };

    this.toggle = function() {
      self.active = !self.active;
    };

    this.addColorizer = function(pluginName) {
      self.colorizers[pluginName] = true;
    };

    this.isColorizer = function(pluginName) {
      return self.colorizer === pluginName;
    };

    function state() {
      return $injector.get('state');
    }

    this.applyColorizer = function() {
      if (self.colorizer === 'disabled') {
        state().unapplyStylings();
      } else {
        // Check if the colorizer is really present
        if (self.colorizers[self.colorizer]) {
          plugins.get(self.colorizer).applyStyling();
        }
      }
    };

    this.colorMaps = function() {
      return arethusaUtil.inject({}, self.colorizers, function(memo, name, _) {
        if (name !== 'disabled') {
          memo[name] = plugins.get(name).colorMap();
        }
      });
    };

    function confTemplate() {
      return configurator.configurationFor('main').template;
    }

    this.layout = confTemplate();

    this.layouts = {
      'Sidepanel' : 'templates/main_with_sidepanel.html',
      'Grid' : 'templates/main_grid.html'
    };

    this.addLayout = function(name, url) {
      self.layout[name] = url;
    };

    this.init = function() {
      configure();
    };

  }
]);

@@ -114,16 +114,32 @@
 ector',%0A
+  '$rootScope',%0A
   functi
@@ -174,16 +174,28 @@
 injector
+, $rootScope
 ) %7B%0A    
@@ -2279,37 +2279,41 @@
 ion 
-confTemplate() %7B%0A      return
+setLayout() %7B%0A      self.layout =
  con
@@ -2372,35 +2372,133 @@
     
-this.layout = confTemplate(
+// When Arethusa is used as widget, it's imperative to wait%0A    // for this event.%0A    $rootScope.$on('confLoaded', setLayout
 );%0A%0A

Fix initial layout assignment in globalSettings