"use strict";

angular.module('arethusa.core').service('globalSettings', [
  'configurator',
  'plugins',
  '$injector',
  '$rootScope',
  'notifier',
  '$timeout',
  '$injector',
  function(configurator,  plugins, $injector, $rootScope, notifier, $timeout) {
    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('clickAction', 'custom', 'global-click-action');
      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;
    };

    function stateDefaultAction(id) {
      state().toggleSelection(id, 'click');
    }

    this.clickActions = {
      'disabled' : stateDefaultAction
    };


    this.addClickAction = function(name, fn) {
      self.clickActions[name] = fn;
    };

    this.removeClickAction = function(name) {
      delete self.clickActions[name];
      if (self.clickAction === name) self.setClickAction('disabled');
    };

    this.setClickAction = function(name) {
      self.clickAction = name;
      self.clickFn = self.clickActions[self.clickAction];
    };

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

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

    var lazyState;
    function state() {
      if (!lazyState) lazyState = $injector.get('state');
      return lazyState;
    }

    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 setLayout() {
      self.layout = configurator.configurationFor('main').template;
    }

    // When Arethusa is used as widget, it's imperative to wait
    // for this event.
    $rootScope.$on('confLoaded', setLayout);

    this.broadcastLayoutChange = function() {
      var layoutName = self.layouts[self.layout];
      if (layoutName === 'Grid') {
        $timeout(function() {
          notifier.warning('The grid layout is an experimental feature and WILL contain bugs!', 'WARNING');
        }, 1200);
      }
      $rootScope.$broadcast('layoutChange', layoutName);
    };

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

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

    this.init = function() {
      configure();
      self.setClickAction('disabled');
    };

  }
]);

@@ -1604,30 +1604,37 @@
     
-function stateDefaultA
+this.defaultClickAction = fun
 ctio
@@ -1682,32 +1682,33 @@
  'click');%0A    %7D
+;
 %0A%0A    this.click
@@ -1743,19 +1743,24 @@
  : s
-tateD
+elf.d
 efault
+Click
 Acti

Make defaultCickAction public in gS